[英]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
显示Toast
, doInBackground
可以,但必须使用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.