简体   繁体   English

非常基本的android代码发布错误。 java.lang.RuntimeException:执行doInBackground()时发生错误

[英]Very basic android code posts error. java.lang.RuntimeException: An error occured while executing doInBackground()

I made the program as simple as possible. 我使程序尽可能简单。 But still the logcat says that an error occurred while execution doInBackground(). 但是, logcat仍然说执行doInBackground()时发生了错误。

Here is my code 这是我的代码

package com.example.basicasynctask;

import android.support.v7.app.AppCompatActivity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    new myFunc().execute();
}

private class myFunc extends AsyncTask<String,Void,Void> {

@Override
protected void onPreExecute() {
    super.onPreExecute();
    Toast.makeText(getApplicationContext(), "preExecute", Toast.LENGTH_SHORT).show();

}

@Override
protected Void doInBackground(String... arg0) {
     Toast.makeText(getApplicationContext(), "inBackground", Toast.LENGTH_SHORT).show();
         return null;
}

@Override
protected void onPostExecute(Void result) {
    super.onPostExecute(result);
    Toast.makeText(getApplicationContext(), "postExecute", Toast.LENGTH_SHORT).show();

}
}
}

If you want to show a Toast in doInBackground you can but you have to run it on a UI thread with runOnUIThread() . 如果要在doInBackground显示ToastdoInBackground可以,但必须使用runOnUIThread()在UI线程上运行它。

If you can wait the end of the doInBackground process you will be able to show the Toast message in the onPostExecute method without running it on a UI thread 如果您可以等待doInBackground进程结束,则可以在onPostExecute方法中显示Toast消息,而无需在UI线程上运行它

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.lang.RuntimeException:执行doInBackground()时发生错误 - java.lang.RuntimeException:An error occured while executing doInBackground() java.lang.RuntimeException:执行doInBackground()URI时发生错误 - java.lang.RuntimeException: An error occured while executing doInBackground() URI 如何解决错误java.lang.RuntimeException:执行doInBackground()时发生错误? - how to solve the error java.lang.RuntimeException: An error occured while executing doInBackground()? 致命异常:AsyncTask#1 java.lang.RuntimeException:在执行doInBackground()并将图像发送到服务器时发生错误 - FATAL EXCEPTION: AsyncTask #1 java.lang.RuntimeException: An error occured while executing doInBackground(), while sending an image to the Server java.lang.RuntimeException:在执行doInBackground()致命错误时发生错误:AsyncTask#2 - java.lang.RuntimeException: An error occured while executing doInBackground() FATAL EXCEPTION: AsyncTask #2 E / AndroidRuntime:致命例外:AsyncTask#1 java.lang.RuntimeException:执行doInBackground()时发生错误 - E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1 java.lang.RuntimeException: An error occured while executing doInBackground() 如何解决此问题:执行doInBackground()时发生致命异常Asynctask#1 java.lang.RuntimeException错误 - how to solve this : Fatal Exception Asynctask #1 java.lang.RuntimeException error occured while executing doInBackground() 致命异常:AsyncTask#1:java.lang.RuntimeException:执行doInBackground()时发生错误 - FATAL EXCEPTION: AsyncTask #1:java.lang.RuntimeException: An error occured while executing doInBackground() java.lang.RuntimeException:执行doInBackground()时发生错误 - java.lang.RuntimeException: An error occurred while executing doInBackground() 致命异常:asynctask#1 java.lang.runtimeException:执行doinbackground时发生错误 - fatal exception : asynctask #1 java.lang.runtimeException: an error occured while excuting doinbackground
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM