java Android: async task 执行 doInBackground() 时发生错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31871828/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android: async task An error occured while executing doInBackground()
提问by Gsurha
before use Async Tasks my code is work. So i have done some changes in my code and now progress bar is showing up but getting the below errors.
在使用异步任务之前,我的代码是有效的。所以我对我的代码做了一些更改,现在显示进度条,但出现以下错误。
Log cat:
日志猫:
08-07 06:43:15.875: E/AndroidRuntime(1734): FATAL EXCEPTION: AsyncTask #1
08-07 06:43:15.875: E/AndroidRuntime(1734): java.lang.RuntimeException: An error occured while executing doInBackground()
08-07 06:43:15.875: E/AndroidRuntime(1734): at android.os.AsyncTask.done(AsyncTask.java:299)
08-07 06:43:15.875: E/AndroidRuntime(1734): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
08-07 06:43:15.875: E/AndroidRuntime(1734): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
08-07 06:43:15.875: E/AndroidRuntime(1734): ... 18 more
mycode:
我的代码:
protected String doInBackground(String... args) {
final EditText eKey = (EditText) findViewById(R.id.edtxt_key);
inputs = eKey.getText().toString();
if (inputs.matches("")) {
} else {
keys = url + inputs;
JSONParser jParser = new JSONParser();
JSONObject json = jParser.getJSONFromUrl(keys);
try {
satu = json.getJSONObject(TAG_1);
String two = satu.getString(TAG_2);
String three = satu.getString(TAG_3);
if (two.matches("") | three.matches("")) {
AlertDialog.Builder builder = new AertDialog.Builder(MainActivity.this);
builder.setMessage("not found!").setCancelable(false).setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
eKey.setText("");
status.setText("Key not found!");
}
});
AlertDialog alert = builder.create();
alert.show();
} else {
Intent x = new Intent(MainActivity.this, result.class);
x.putExtra("bac", two);
x.putExtra("cab", three);
startActivity(x);
eKey.setText("");
keys = "";
inputs = "";
}
} catch (JSONException e) {
e.printStackTrace();
}
}
return null;
}
What is going wrong? Thanks in advance.
出了什么问题?提前致谢。
回答by zkminusck
alert.show();
and
和
eKey.setText("");
cannot be performed in background task. Pass a return value from
不能在后台任务中执行。传递一个返回值
doInBackground()
doInBackground()
to
到
onPostExecute()
后执行()
method and perform UI tasks there.
方法并在那里执行 UI 任务。
回答by Jayaprakash
You should not keep any UI related element inside doInBackground()
你不应该在里面保留任何 UI 相关的元素 doInBackground()
For example,
If you are downloading any staff from network, at that time you want to update UI , you can use onProgressUpdate()
to update UI and implement publishProgress() @doInBackground().
例如,如果您正在从网络下载任何人员,此时您想更新UI,您可以使用onProgressUpdate()
更新UI并实现publishProgress() @doInBackground().
Otherwise,
AsynTask is running in separate worker thread to execute staff behind UI using doInBackground()
method (We can show loading dialog when Asyntask running in background). Once work is done, we can update UI using onPostExecute()
.
否则,AsynTask 将在单独的工作线程中运行,以使用doInBackground()
方法在 UI 后面执行人员(当 Asyntask 在后台运行时,我们可以显示加载对话框)。工作完成后,我们可以使用onPostExecute()
.
If you need more clarification on AsynTask, sure u can shot your questions...
如果您需要有关 AsynTask 的更多说明,请确保您可以提出您的问题...
Happy coding :)
快乐编码:)
回答by Gigi
AsynTask class provides three main methods :
AsynTask 类提供了三个主要方法:
- onPreExecute()
- doInBackground()
- onPostExecute()
- 预执行()
- doInBackground()
- 后执行()
the two methods onPreExecute(), onPostExecute()are executed in the same thread of the class which contains the UI.
onPreExecute()和onPostExecute()这两个方法在包含 UI 的类的同一线程中执行。
The method doInBackground()is executed on a different thread. As we cannot refer to UI from a process that is running on a different thread, we can :
doInBackground()方法在不同的线程上执行。由于我们不能从运行在不同线程上的进程引用 UI,我们可以:
- move our code (which refers to UI) to the onPostExecuted() method
- use handlers(a way to refer to UI also from a different thread if we need)
- 将我们的代码(指的是 UI)移动到onPostExecuted() 方法
- 使用处理程序(如果需要,也可以从不同的线程中引用 UI)
Bye :)
再见 :)