gpt4 book ai didi

java - AlertDialog.Builder finish() 导致 NPE

转载 作者:行者123 更新时间:2023-12-01 15:40:35 25 4
gpt4 key购买 nike

我正在尝试制作一个应用程序,单击列表项即可启动显示下载进度的进度栏。由于某种原因,列表项阻止进度条显示。代码如下:

final CharSequence[] items = { "View Information", "Lock", "Pin",
"Delete", "Ban User", "Ban IP", "Download" };

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Options");
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
Toast.makeText(getApplicationContext(), items[item],
Toast.LENGTH_SHORT).show();
switch (item) {
case 0:
Intent i = new Intent(TestActivity.this, ActivityTwo.class);
i.putExtra("filename", files.get(position).getFilename());
i.putExtra("orig_filename", files.get(position)
.getOriginalFilename());
i.putExtra("date", files.get(position).getDate());
i.putExtra("downloads", files.get(position).getDownloads());
i.putExtra("uploader_ip", files.get(position)
.getUploaderIP());
i.putExtra("username", files.get(position).getUsername());
i.putExtra("locked", files.get(position).isLocked());
i.putExtra("pinned", files.get(position).isPinned());
startActivityForResult(i, REQUEST_CODE);
case 6:
downloadFile(files.get(position).getFilename(), files.get(position).getOriginalFilename());
}
}

});
AlertDialog alert = builder.create();
alert.show();

以及控制进度条的三个方法。

private ProgressDialog progressDialog;
public void startProgress() {
progressDialog = new ProgressDialog(TestActivity.this);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setMessage("Loading...");
progressDialog.setCancelable(false);
Log.d("Download", String.valueOf(progressDialog.getMax()));
progressDialog.show();
}

public void updateProgress(int currentSize, int totalSize) {
Log.d("Download", String.valueOf((currentSize / totalSize) * 100));
progressDialog.setProgress(((currentSize / totalSize) * 100));
}

public void closeProgress() {
progressDialog.hide();
}

启动进度条的下载文件。

if(alertDialog != null) {
alertDialog.dismiss();
}
startProgress();

// now, read through the input buffer and write the contents to the
// file
while ((bufferLength = inputStream.read(buffer)) > 0) {
// add the data in the buffer to the file in the file output
// stream (the file on the sd card
fileOutput.write(buffer, 0, bufferLength);
// add up the size so we know how much is downloaded
downloadedSize += bufferLength;
// this is where you would do something to report the prgress,
// like this maybe
updateProgress(downloadedSize, totalSize);
}

closeProgress();

异常(exception)情况如下:

11-12 20:17:18.407: E/AndroidRuntime(4728): java.lang.NullPointerException
11-12 20:17:18.407: E/AndroidRuntime(4728): at com.evandarwin.android.TestActivity.downloadFile(TestActivity.java:244)
11-12 20:17:18.407: E/AndroidRuntime(4728): at com.evandarwin.android.TestActivity$1.onClick(TestActivity.java:143)
11-12 20:17:18.407: E/AndroidRuntime(4728): at com.android.internal.app.AlertController$AlertParams$3.onItemClick(AlertController.java:873)
11-12 20:17:18.407: E/AndroidRuntime(4728): at android.widget.AdapterView.performItemClick(AdapterView.java:284)
11-12 20:17:18.407: E/AndroidRuntime(4728): at android.widget.ListView.performItemClick(ListView.java:3513)
11-12 20:17:18.407: E/AndroidRuntime(4728): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1812)
11-12 20:17:18.407: E/AndroidRuntime(4728): at android.os.Handler.handleCallback(Handler.java:587)
11-12 20:17:18.407: E/AndroidRuntime(4728): at android.os.Handler.dispatchMessage(Handler.java:92)
11-12 20:17:18.407: E/AndroidRuntime(4728): at android.os.Looper.loop(Looper.java:130)
11-12 20:17:18.407: E/AndroidRuntime(4728): at android.app.ActivityThread.main(ActivityThread.java:3683)
11-12 20:17:18.407: E/AndroidRuntime(4728): at java.lang.reflect.Method.invokeNative(Native Method)
11-12 20:17:18.407: E/AndroidRuntime(4728): at java.lang.reflect.Method.invoke(Method.java:507)
11-12 20:17:18.407: E/AndroidRuntime(4728): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)
11-12 20:17:18.407: E/AndroidRuntime(4728): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:647)
11-12 20:17:18.407: E/AndroidRuntime(4728): at dalvik.system.NativeStart.main(Native Method)

最佳答案

您还没有输入完整的信息。它发生在 downloadFile() 中。在 case 0 末尾添加 break 可能会有所帮助。(在 case 6 末尾添加 break也是)

关于java - AlertDialog.Builder finish() 导致 NPE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8109352/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com