我开始ProgressDialog
在我Activity
是应该继续纺纱直到被叫IntentService
完成,但是当我试图拒绝接受ProgressDialog
来自内部onReceive
,实例为null,我得到了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);
}
}
}
这有什么问题,当被调用的服务完成后,在调用活动中执行某项操作的正确方法是什么?
编辑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)
我有几个问题。
1)我遵循一个示例,并LocalBroadcastManager
用于注册和发送广播。但LocalBroadcastManager
仅适用于同一应用程序中的内容。我有两个单独的应用程序。因此,例如...
代替
LocalBroadcastManager.getInstance(this).registerReceiver(broadcastReceiver,new IntentFilter("abc.def"));
我需要
registerReceiver(broadcastReceiver,new IntentFilter("abc.def"));
2)我的两个应用程序是基于包含所描述功能的相同基类构建的。因此,应用B获得了自己的sendBroadcast()
应用,但应用B没有启动ProgressDialog
应用A。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句