私のアプリには、アクティビティ (MainActivity.java) とクラス (非アクティビティ クラス - SubClass.java) があります。サブクラスには ProgressDialog があります。MainActivity から SubClass を呼び出します。
私の質問は、アクティビティ クラス (MainActivity.java) の非アクティビティ クラス (SubClass.java) から ProgressDialog を使用する方法です。
setProgress() およびパッケージ宣言で Null Pointer Exception Error が発生します。
私は次のことをしました
1) MainActivity で、コンストラクターを使用してサブクラスのオブジェクトを作成します。
SubClass pro = new SubClass(MainActivity.this);
pro.call_fun();
2) サブクラス: コーディング全体。
package com.progresss; // Line No : 1 ; Shows Null Pointer Exception
public class SubClass {
Context context = null;
public static final int DIALOG_DOWNLOAD_PROGRESS = 0;
private NotificationManager notificationManager;
private Notification notification;
private int progress_val = 0;
private String fileURL = "http://xxxxx.com/folder/sampleproject.apk";
private String destination = null;
private ProgressDialog downloadProgressDialog;
public SubClass(MainActivity progress_DialiogActivity) {
this.context = progress_DialiogActivity;
}
public void call_fun(){
new DownloadFileAsync().execute(fileURL);
}
// Updated APK File Download Task.
class DownloadFileAsync extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
((Activity) context).showDialog(DIALOG_DOWNLOAD_PROGRESS);
// Notification Coding
...........
notification.contentIntent = contentIntent;
// -------------------
notificationManager =(NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notification);
}
@Override
protected String doInBackground(String... aurl) {
try {
// .......
} catch (Exception e) {
Log.e("Error Report Download Manager", e.getMessage());
}
return null;
}
protected void onProgressUpdate(String... progress) {
progress_val = Integer.parseInt(progress[0]);
.......
downloadProgressDialog.setProgress(Integer.parseInt(progress[0])); // Line No : 100 ; Shows Null Pointer Exception
}
@Override
protected void onPostExecute(String unused) {
notificationManager.cancel(0);
// ((Activity) context).dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
((Activity) context).removeDialog(DIALOG_DOWNLOAD_PROGRESS);
Log.e("Download", "Stop");
// Notification Coding
notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
.........
notificationManager.notify(0, notification);
// Open The Install App Activity(Intent) Here.
........
}
} // Async Task Class End
//our progress bar settings
protected Dialog onCreateDialog(int id) {
switch (id) {
case DIALOG_DOWNLOAD_PROGRESS: //we set this to 0
downloadProgressDialog = new ProgressDialog(context);
downloadProgressDialog.setMessage("Downloading file...");
downloadProgressDialog.setMax(100);
downloadProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
downloadProgressDialog.setCancelable(false);
downloadProgressDialog.show();
return downloadProgressDialog;
default:
return null;
}
}
}
これは LogCat エラー メッセージです。
FATAL EXCEPTION: main
java.lang.NullPointerException
at com.progresss.SubClass$DownloadFileAsync.onProgressUpdate(SubClass.java:100)
at com.progresss.SubClass$DownloadFileAsync.onProgressUpdate(SubClass.java:1)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:432)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
プログレス ダイアログ コードは、アクティビティ クラスでうまく機能します。ProgressDialog が正しく初期化されていない可能性があると思います。私を助けてください。