0
我試圖做一個應用程序,點擊一個列表項目開始進度條顯示下載進度。出於某種原因,列表項目正在停止顯示進度條。這裏的代碼:AlertDialog.Builder完成()結果NPE
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()'的代碼。 – dmon