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

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();


这是例外:

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()中。在break末尾添加case 0可能有助于解决问题。(也将break添加在case 6末尾)

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

10-10 19:22