从广播接收的onReceive中取消主要活动启动的ProgressDialog

时间:2014-08-22 23:28:48

标签: android nullpointerexception broadcastreceiver progressdialog

我在我的ProgressDialog中开始Activity我应该继续旋转直到被叫IntentService完成,但当我尝试从内部解除ProgressDialogonReceive,实例为空,我得到NullPointerException

public class myActivity extends Activity {

    ProgressDialog progressDialog = null;

    @Override
    protected void onCreate(Bundle stateBundle) {
        super.onCreate(stateBundle);
        myBroadcastReceiver broadcastReceiver = new myBroadcastReceiver();
        LocalBroadcastManager.getInstance(this).registerReceiver(broadcastReceiver,new IntentFilter("abc.def"));
    }

    ....

    public void runService() {
        Intent myIntent = new Intent("com.example.service");
        myIntent.addCategory(Intent.CATEGORY_DEFAULT);
        try {
            progressDialog = ProgressDialog.show(this, "Please wait ...", "Running Service.", true);
            progressDialog.setCancelable(false);
            startService(myIntent);     
        } catch (ActivityNotFoundException e) {
            Toast.makeText(getApplicationContext(), "Service app not found.", Toast.LENGTH_LONG).show();
        }
    }

    public void nextStep(Intent intent) {
       //do something when process is complete.
    }

    ....

    private class myBroadcastReceiver extends BroadcastReceiver
    {

        // Prevents instantiation
        private void myBroadcastReceiver() {
        }

        @Override
        public void onReceive(Context context, Intent intent) {
            progressDialog.dismiss();   //<--this is where i get the exception
            nextStep(intent);
        }

    }
}

这有什么问题,当它调用的服务完成时,在调用Activity中做某事的正确方法是什么?

编辑1:这是日志猫:

08-22 17:01:24.497: E/AndroidRuntime(11525): FATAL EXCEPTION: main
08-22 17:01:24.497: E/AndroidRuntime(11525): java.lang.NullPointerException
08-22 17:01:24.497: E/AndroidRuntime(11525):    at com.example.library.myCoreActivity$myCoreBroadcastReceiver.onReceive(myCoreActivity.java:169)
08-22 17:01:24.497: E/AndroidRuntime(11525):    at android.support.v4.content.LocalBroadcastManager.executePendingBroadcasts(LocalBroadcastManager.java:297)
08-22 17:01:24.497: E/AndroidRuntime(11525):    at android.support.v4.content.LocalBroadcastManager.access$000(LocalBroadcastManager.java:46)
08-22 17:01:24.497: E/AndroidRuntime(11525):    at android.support.v4.content.LocalBroadcastManager$1.handleMessage(LocalBroadcastManager.java:116)
08-22 17:01:24.497: E/AndroidRuntime(11525):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-22 17:01:24.497: E/AndroidRuntime(11525):    at android.os.Looper.loop(Looper.java:123)
08-22 17:01:24.497: E/AndroidRuntime(11525):    at android.app.ActivityThread.main(ActivityThread.java:4627)
08-22 17:01:24.497: E/AndroidRuntime(11525):    at java.lang.reflect.Method.invokeNative(Native Method)
08-22 17:01:24.497: E/AndroidRuntime(11525):    at java.lang.reflect.Method.invoke(Method.java:521)
08-22 17:01:24.497: E/AndroidRuntime(11525):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
08-22 17:01:24.497: E/AndroidRuntime(11525):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
08-22 17:01:24.497: E/AndroidRuntime(11525):    at dalvik.system.NativeStart.main(Native Method)

3 个答案:

答案 0 :(得分:0)

出于某种原因,似乎onReceive之前调用runService。有时在Android上,广播接收器可以在注册后立即调用。见this question

答案 1 :(得分:0)

请在以下链接中查阅我的答案..

你必须使用接口...它将充当事件监听器......如果你的广播接收器中发生任何事情,你可以在你的活动中触发接口实现

请参阅帖子

Call a method in MainActivity.java on event that happens in a library file

答案 2 :(得分:0)

我有几个问题。

1)我正在关注一个例子并使用LocalBroadcastManager来注册和发送广播。但LocalBroadcastManager仅适用于同一应用中的内容。我有两个独立的应用程序。所以,例如......

而不是

LocalBroadcastManager.getInstance(this).registerReceiver(broadcastReceiver,new IntentFilter("abc.def"));

我需要

registerReceiver(broadcastReceiver,new IntentFilter("abc.def"));

2)我的两个应用程序构建在包含所述功能的同一基类上。因此,应用B获得了自己的sendBroadcast(),但应用B尚未启动{A}}应用A。