从使用自定义程序对话框的异步任务更新 TextView [英] Updating TextView from Async Task which use custom program dialog
问题描述
在我的一个应用程序中,我有一个需要执行一些后台任务的场景.为此,我正在使用异步任务.我也在使用自定义进度对话框.下面是自定义进度对话框的布局
In one of my app, I have a scenario where I need to do some background task. For doing that I am using Async Task. Also I am using custom progress dialog. Below is the layout of the custom progress dialog
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_vertical|center_horizontal"
android:orientation="vertical" >
<ProgressBar
android:layout_width="60dp"
android:layout_height="60dp"
android:indeterminateDrawable="@drawable/progressloader"
android:layout_gravity="center"/>
<TextView
android:id="@+id/progressMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/black"
android:textSize="18sp"
android:text="Please wait...." />
</LinearLayout>
一切正常,但是当我尝试将文本设置为 TextView 时,我收到了 java NullPointerException.
Everything works fine but when I try to set text to TextView then I am getting java NullPointerException.
异步任务代码
private class InitialSetup extends AsyncTask<String, Integer, Long> {
ProgressDialog dialog = new ProgressDialog(getParent(),R.style.progressdialog);
@Override
protected void onPreExecute() {
dialog.show();
dialog.setContentView(R.layout.progressbar);
}
@Override
protected Long doInBackground(String... urls) {
// txtView.setText("Testing"); here I am getting the error
fetchDetails();
return 0;
}
@Override
protected void onPostExecute(Long result) {
if (this.dialog.isShowing()) {
this.dialog.dismiss();
}
populateUI(getApplicationContext());
}
}
主活动
public class SummaryActivity extends Activity {
final TextView txtView = (TextView)findbyid(R.id.progressMessage);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.accountsummary);
new InitialSetup().execute("");
}
}
推荐答案
如果我没理解错的话,你要设置文本的TextView
可以在xml文件progressbar.xml中找到(即 R.layout.progressbar
).一旦设置了内容视图(使用setContentView()
),就可以获得这个TextView
.在您的代码中,您在此调用之前设置了它,而 mussharapp 的代码,他提前调用了它.也就是说,他在不包含 TextView
的 setContentView(R.layout.accountsummary)
调用之后调用它.因此,变量 txtView
将为 NULL,您将得到 NullPointerException
.
If I understand correctly, your TextView
of which you want to set the text can be found in the xml file progressbar.xml (i.e. R.layout.progressbar
). This TextView
can be obtained once the content view has been set (using setContentView()
). In your code you set it before this call is been and the code of mussharapp, he is calling it to early. Namely, he calls it after the setContentView(R.layout.accountsummary)
call which does not contain the TextView
. Consequently, the variable txtView
will be NULL and you will get a NullPointerException
.
你应该做的是:
- 在调用
setContentView
后,在onPreExecute
中设置变量txtView. - 基于 Paresh Mayani 的解释:使用 runOnUiThread 方法.
- Set the variable txtView in
onPreExecute
, aftersetContentView
is called. - Based on Paresh Mayani's explanation: Use the runOnUiThread method.
代码如下:
private class InitialSetup extends AsyncTask<String, Integer, Long> {
ProgressDialog dialog = new ProgressDialog(getParent(),R.style.progressdialog);
// The variable is moved here, we only need it here while displaying the
// progress dialog.
TextView txtView;
@Override
protected void onPreExecute() {
dialog.show();
dialog.setContentView(R.layout.progressbar);
// Set the variable txtView here, after setContentView on the dialog
// has been called! use dialog.findViewById().
txtView = dialog.findViewById(R.id.progressMessage);
}
@Override
protected Long doInBackground(String... urls) {
// Already suggested by Paresh Mayani:
// Use the runOnUiThread method.
// See his explanation.
runOnUiThread(new Runnable() {
@Override
public void run() {
txtView.setText("Testing");
}
});
fetchDetails();
return 0;
}
@Override
protected void onPostExecute(Long result) {
if (this.dialog.isShowing()) {
this.dialog.dismiss();
}
populateUI(getApplicationContext());
}
}
这篇关于从使用自定义程序对话框的异步任务更新 TextView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!