Android AsyncTask中的对话框TextView始终为空
我的AsyncTask中有一个对话框来显示一些图像的加载。对话框中有一个TextView,需要为每个加载的图像更新它,但它总是返回NullPointerException 谁能帮帮我吗 以下是AsyncTask类的代码:Android AsyncTask中的对话框TextView始终为空,android,android-asynctask,nullpointerexception,textview,Android,Android Asynctask,Nullpointerexception,Textview,我的AsyncTask中有一个对话框来显示一些图像的加载。对话框中有一个TextView,需要为每个加载的图像更新它,但它总是返回NullPointerException 谁能帮帮我吗 以下是AsyncTask类的代码: private class addCartRealms extends AsyncTask<Void, Integer, Void> { AlertDialog.Builder mBuilder = new AlertDialog.Builder(
private class addCartRealms extends AsyncTask<Void, Integer, Void> {
AlertDialog.Builder mBuilder = new AlertDialog.Builder(mainMenu.this);
AlertDialog dialog;
View mView = getLayoutInflater().inflate(R.layout.download_loading_dialog, null);
TextView numberP = findViewById(R.id.loading_number);
ProgressBar downloadProgress = findViewById(R.id.download_progress_bar);
@Override
protected Void doInBackground(Void... params) {
final OkHttpClient client = new OkHttpClient();
Realm myRealm = Realm.getDefaultInstance();
for (int i = 0; i < downloadedCardList.size(); i++) {
publishProgress(i);
myRealm.beginTransaction();
//ADDING CARDS TO REALM
Card card = myRealm.createObject(Card.class);
card.setDankness(downloadedCardList.get(i).getDankness());
card.setExpansion(downloadedCardList.get(i).getExpansion());
card.setId(downloadedCardList.get(i).getId());
card.setImgpath(downloadedCardList.get(i).getImgpath());
card.setLitness(downloadedCardList.get(i).getLitness());
card.setName(downloadedCardList.get(i).getName());
card.setRarity(downloadedCardList.get(i).getRarity());
card.setValue(downloadedCardList.get(i).getValue());
Request request = new Request.Builder()
.url(downloadedCardList.get(i).getImgpath())
.build();
Response response = null;
Bitmap image = null;
try {
response = client.newCall(request).execute();
} catch (IOException e) {
e.printStackTrace();
}
if (response.isSuccessful()) {
image = BitmapFactory.decodeStream(response.body().byteStream());
ByteArrayOutputStream stream = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
card.setCardImage(byteArray);
}
myRealm.commitTransaction();
//cardImgUrl.add(new cardImagesAndLink(downloadedCardList.get(i).getImgpath(), downloadedCardList.get(i).getId()));
}
return null;
}
@Override
protected void onPostExecute(Void result) {
dialog.dismiss();
Toast.makeText(mainMenu.this, "Download completato, scaricate " + downloadedCardList.size() + " carte.", Toast.LENGTH_LONG).show();
}
@Override
protected void onPreExecute() {
mBuilder.setView(mView);
dialog = mBuilder.create();
dialog.show();
}
@Override
protected void onProgressUpdate(Integer... values) {
numberP.setText(values[0] + " / " + downloadedCardList.size()); // HERE IS THE ERROR
}
}
私有类addCartRealms扩展异步任务{
AlertDialog.Builder mBuilder=新建AlertDialog.Builder(主菜单.this);
警报对话框;
View mView=GetLayoutFlater().充气(R.layout.download\u loading\u对话框,空);
TextView numberP=findviewbyd(R.id.loading\u编号);
ProgressBar downloadProgress=findviewbyd(R.id.download\u progress\u bar);
@凌驾
受保护的Void doInBackground(Void…参数){
最终OkHttpClient客户端=新的OkHttpClient();
Realm myRealm=Realm.getDefaultInstance();
对于(int i=0;i
错误是这样的:
尝试在空对象引用上调用虚拟方法“void android.widget.TextView.setText(java.lang.CharSequence)”,而不是
TextView numberP = findViewById(R.id.loading_number);
您应该使用对话框的视图
TextView numberP = mView.findViewById(R.id.loading_number);
如果ProgressBar视图包含在对话框布局中,而不是
TextView numberP = findViewById(R.id.loading_number);
您应该使用对话框的视图
TextView numberP = mView.findViewById(R.id.loading_number);
如果ProgressBar视图包含在对话框布局中,则可能会对其执行相同的操作。使用以下对话框
findViewById
:
TextView numberP = mView.findViewById(R.id.loading_number);
ProgressBar downloadProgress = mView.findViewById(R.id.download_progress_bar);
使用如下对话框
findViewById
:
TextView numberP = mView.findViewById(R.id.loading_number);
ProgressBar downloadProgress = mView.findViewById(R.id.download_progress_bar);