Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/213.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用android上非活动类的ProgressDialog_Android_Android Asynctask_Android Progressbar - Fatal编程技术网

如何使用android上非活动类的ProgressDialog

如何使用android上非活动类的ProgressDialog,android,android-asynctask,android-progressbar,Android,Android Asynctask,Android Progressbar,在我的应用程序中,我有一个活动(MainActivity.java)和一个类(非活动类-SubClass.java)。子类有一个ProgressDialog。我将从MainActivity调用子类 我的问题是如何从活动类(MainActivity.java)的非活动类(SubClass.java)使用ProgressDialog 我在setProgress()和包声明处收到空指针异常错误。 我做了以下事情 1) 在MainActivity中,我使用构造函数为子类创建对象 SubClass

在我的应用程序中,我有一个活动(MainActivity.java)和一个类(非活动类-SubClass.java)。子类有一个ProgressDialog。我将从MainActivity调用子类

我的问题是如何从活动类(MainActivity.java)的非活动类(SubClass.java)使用ProgressDialog

我在setProgress()和包声明处收到空指针异常错误。

我做了以下事情

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;
    }
  }
}

进度对话框代码在活动类中运行良好。我猜ProgressDialog可能没有正确初始化。请帮帮我

onCreateDialog
未执行。请将代码移动到
onPreExecute
方法


onCreateDialog
是回调方法,它将仅在活动中调用,在您的子类中它只是一个方法。

我遇到了同样的问题。在我将onCreateDialog移动到onPreExecute之后,它就不起作用了

我将
pro.call\u-fun()
更改为
pro.call\u-fun(MainActivity.this)
,然后 在
pro.call\u fun(MainActivity.this)
中创建
downloadProgressDialog=newprogressDialog(上下文)


它对我有效。

是否检查是否调用了onCreateDialog?没有调用onCreateDialog。我在onCreateDialog的开头添加了Log.e()。它没有显示日志。在哪里调用了onCreateDialog??甚至你的代码设计也很糟糕…@Mohit Sharma,实际上我需要在非活动类中完成这个过程。现在我重新排列了代码。您的意思是将onCreateDialog代码移动到OnPreExecute方法吗?您应该将onCreateDialog中编写的代码移动到OnPreExecute方法吗
    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)