我的AsyncTask中有一个Dialog来显示一些图像的加载。在DIalog中,有一个TextView需要为每个加载的图像更新,但它总是返回NullPointerException。
有人可以帮帮我吗?
这是AsyncTask类的代码:
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
}
}
错误就是这个:
尝试在空对象引用上调用虚方法'void android.widget.TextView.setText(java.lang.CharSequence)'
答案 0 :(得分:1)
而不是
header .highlight, header .current a {
color: #e8491d;
font-weight: bold;
}
header a:link {
text-decoration: none;
}
header a:visited {
text-decoration: none;
}
header a:active {
text-decoration: none;
}
header a:hover {
color: #fff;
font-weight: bold;
text-decoration: none;
}
您应该使用对话视图
TextView numberP = findViewById(R.id.loading_number);
如果ProgressBar视图包含在对话框布局
中,可能也会这样做答案 1 :(得分:1)
使用对话框func saveMixToDisk() {
do {
AudioKit.output = offlineRender
for track in tracks {
let trackSampler = AKMIDISampler()
try trackSampler.loadWav(track.instrument.fileName)
trackSampler.connect(to: mixer)
let sequencer = AKSequencer(filename: track.midi.fileName)
sequencer.setTempo(Double(tempo))
sequencer.setRate(rate)
sequencer.setGlobalMIDIOutput(trackSampler.midiIn)
sequencer.enableLooping()
offlineSequencers.append(sequencer)
trackSampler >>> offlineRender
}
AudioKit.start()
let docs = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let destinationAudioFile = docs.appendingPathComponent("\(UUID().uuidString).m4a")
offlineRender.internalRenderEnabled = false
for sequencer in offlineSequencers {
sequencer.play()
}
try offlineRender.renderToURL(destinationAudioFile, seconds: offlineSequencers.first!.length.seconds)
for sequencer in offlineSequencers {
sequencer.stop()
}
offlineRender.internalRenderEnabled = true
} catch let error {
print(error)
}
}
,如下所示:
findViewById