IllegalStateException:前台调度只能在启用时启用 您的活动已恢复
和
IllegalStateException:您必须禁用 在您的活动仍然恢复时进行前台调度
尝试在我的BaseActivity中启动/停止NFC前台调度时,我遇到了这些异常。
我的所有活动都扩展了BaseActivity,但我只在显示第三个活动后才得到这些例外。
以下是Android Monitor窗口的堆栈跟踪和输出:
10-24 12:53:48.802 28454-28454/com.example D/BaseActivity: onCreate()
10-24 12:53:48.822 28454-28454/com.example D/BaseActivity: initializeLibrary...
10-24 12:53:48.962 28454-28454/com.example D/BaseActivity: initializeKeys...
10-24 12:53:49.322 28454-28454/com.example D/BaseActivity: initializeCipherinitVector...
10-24 12:53:49.322 28454-28454/com.example D/BaseActivity: ...done!
10-24 12:53:49.672 28454-28454/com.example D/BaseActivity: onResume()
10-24 12:53:49.672 28454-28454/com.example D/BaseActivity: startForeGroundDispatch...
10-24 12:53:49.692 28454-28454/com.example D/BaseActivity: ...done!
10-24 12:54:15.502 28454-28454/com.example D/BaseActivity: onPause()
10-24 12:54:15.502 28454-28454/com.example D/BaseActivity: stopForeGroundDispatch...
10-24 12:54:15.512 28454-28454/com.example D/BaseActivity: ...done!
10-24 12:54:15.562 28454-28454/com.example D/BaseActivity: onCreate()
10-24 12:54:15.562 28454-28454/com.example D/BaseActivity: initializeLibrary...
10-24 12:54:15.572 28454-28454/com.example D/BaseActivity: initializeKeys...
10-24 12:54:15.572 28454-28454/com.example D/BaseActivity: initializeCipherinitVector...
10-24 12:54:15.572 28454-28454/com.example D/BaseActivity: ...done!
10-24 12:54:15.692 28454-28454/com.example D/BaseActivity: onResume()
10-24 12:54:15.692 28454-28454/com.example D/BaseActivity: startForeGroundDispatch...
10-24 12:54:15.702 28454-28454/com.example D/BaseActivity: ...done!
10-24 12:54:41.612 28454-28454/com.example D/BaseActivity: onPause()
10-24 12:54:41.622 28454-28454/com.example D/BaseActivity: stopForeGroundDispatch...
10-24 12:54:41.622 28454-28454/com.example D/BaseActivity: ...done!
10-24 12:54:41.672 28454-28454/com.example D/BaseActivity: onCreate()
10-24 12:54:41.672 28454-28454/com.example D/BaseActivity: initializeLibrary...
10-24 12:54:41.672 28454-28454/com.example D/BaseActivity: initializeKeys...
10-24 12:54:41.682 28454-28454/com.example D/BaseActivity: initializeCipherinitVector...
10-24 12:54:41.682 28454-28454/com.example D/BaseActivity: ...done!
10-24 12:54:41.802 28454-28454/com.example D/BaseActivity: onResume()
10-24 12:54:41.802 28454-28454/com.example D/BaseActivity: startForeGroundDispatch...
10-24 12:54:41.812 28454-28454/com.example E/BaseActivity: Error starting foreground dispatch
java.lang.IllegalStateException: Foreground dispatch can only be enabled when your activity is resumed
at android.nfc.NfcAdapter.enableForegroundDispatch(NfcAdapter.java:1421)
at com.nxp.nfclib.NxpNfcLib.startForeGroundDispatch(:4065)
at com.example.BaseActivity.onResume(BaseActivity.java:422)
at com.example.HomeActivity.onResume(HomeActivity.java:36)
at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1255)
at android.app.Activity.performResume(Activity.java:6495)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3516)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3558)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1526)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6145)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
10-24 12:54:41.832 28454-28454/com.example D/BaseActivity: onPause()
10-24 12:54:41.832 28454-28454/com.example D/BaseActivity: stopForeGroundDispatch...
10-24 12:54:41.832 28454-28454/com.example E/BaseActivity: Error stopping foreground dispatch
java.lang.IllegalStateException: You must disable foreground dispatching while your activity is still resumed
at android.nfc.NfcAdapter.disableForegroundDispatchInternal(NfcAdapter.java:1468)
at android.nfc.NfcAdapter.disableForegroundDispatch(NfcAdapter.java:1454)
at com.nxp.nfclib.NxpNfcLib.stopForeGroundDispatch(:5082)
at com.example.BaseActivity.onPause(BaseActivity.java:477)
at android.app.Activity.performPause(Activity.java:6530)
at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1308)
at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3860)
at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3833)
at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3808)
at android.app.ActivityThread.access$1100(ActivityThread.java:181)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1493)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6145)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
10-24 12:54:41.932 28454-28454/com.example D/BaseActivity: onCreate()
10-24 12:54:41.932 28454-28454/com.example D/BaseActivity: initializeLibrary...
10-24 12:54:41.932 28454-28454/com.example D/BaseActivity: initializeKeys...
10-24 12:54:41.942 28454-28454/com.example D/BaseActivity: initializeCipherinitVector...
10-24 12:54:41.942 28454-28454/com.example D/BaseActivity: ...done!
10-24 12:54:42.002 28454-28454/com.example D/BaseActivity: onResume()
10-24 12:54:42.002 28454-28454/com.example D/BaseActivity: startForeGroundDispatch...
10-24 12:54:42.012 28454-28454/com.example D/BaseActivity: ...done!
10-24 12:56:45.922 28454-28454/com.example D/BaseActivity: onPause()
10-24 12:56:45.922 28454-28454/com.example D/BaseActivity: stopForeGroundDispatch...
10-24 12:56:45.932 28454-28454/com.example D/BaseActivity: ...done!
以下是我的BaseActivity中的onResume和onPause方法:
@Override
protected void onResume() {
Log.d(LOG_TAG, "onResume()");
super.onResume();
try {
Log.d(LOG_TAG, "startForeGroundDispatch...");
nxpNfcLib.startForeGroundDispatch();
Log.d(LOG_TAG, "...done!");
}
catch(Exception e) {
Log.e(LOG_TAG, "Error starting foreground dispatch", e);
showToast("Error starting foreground dispatch: " + e);
}
}
@Override
protected void onPause() {
Log.d(LOG_TAG, "onPause()");
super.onPause();
try {
Log.d(LOG_TAG, "stopForeGroundDispatch...");
nxpNfcLib.stopForeGroundDispatch();
Log.d(LOG_TAG, "...done!");
}
catch(Exception e) {
Log.e(LOG_TAG, "Error stopping foreground dispatch", e);
showToast("Error stopping foreground dispatch: " + e);
}
}
我正在使用NXP / Taplinx库(我没有源代码)但是通过查看上面的堆栈跟踪以及disableForegroundDispatch和enableForegroundDispatch的Android NfcAdapter文档,代码似乎是有序的,所以我不确定到底出了什么问题或为什么。
任何人都可以了解错误是什么以及如何解决?
注意 - 我尝试将super.onResume()
和super.onPause()
调用移到相应的BaseActivity方法的末尾 - 但它没有任何区别。
我也尝试将我的onResume()
代码移到新的onPostResume()
方法中,但这也无济于事。