我在Android应用中遇到问题,我的活动实现了CreateNdefMessageCallback
API 14+
中的public NdefMessage createNdefMessage(NfcEvent event)
。
我使用@TargetApi(14)
为已实施的方法class
添加了注释。它在Android 4设备上运行良好,但在Android 4.0以下的设备上加载时会出现崩溃!
唯一的解决方案是为实现创建另一个@TargetApi
?或者可以使用implements CreateNdefMessageCallback
进行条件实现?
如果我删除了04-15 11:22:28.063: E/AndroidRuntime(3784): FATAL EXCEPTION: main
04-15 11:22:28.063: E/AndroidRuntime(3784): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{XXXX/XXXX.MyActivity}: java.lang.ClassNotFoundException: XXXX.MyActivity in loader dalvik.system.PathClassLoader[/data/app/XXXX.apk]
04-15 11:22:28.063: E/AndroidRuntime(3784): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
04-15 11:22:28.063: E/AndroidRuntime(3784): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
04-15 11:22:28.063: E/AndroidRuntime(3784): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
04-15 11:22:28.063: E/AndroidRuntime(3784): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
04-15 11:22:28.063: E/AndroidRuntime(3784): at android.os.Handler.dispatchMessage(Handler.java:99)
04-15 11:22:28.063: E/AndroidRuntime(3784): at android.os.Looper.loop(Looper.java:123)
04-15 11:22:28.063: E/AndroidRuntime(3784): at android.app.ActivityThread.main(ActivityThread.java:4627)
04-15 11:22:28.063: E/AndroidRuntime(3784): at java.lang.reflect.Method.invokeNative(Native Method)
04-15 11:22:28.063: E/AndroidRuntime(3784): at java.lang.reflect.Method.invoke(Method.java:521)
04-15 11:22:28.063: E/AndroidRuntime(3784): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-15 11:22:28.063: E/AndroidRuntime(3784): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-15 11:22:28.063: E/AndroidRuntime(3784): at dalvik.system.NativeStart.main(Native Method)
04-15 11:22:28.063: E/AndroidRuntime(3784): Caused by: java.lang.ClassNotFoundException: XXXX.MyActivity in loader dalvik.system.PathClassLoader[/data/app/XXXX.apk]
04-15 11:22:28.063: E/AndroidRuntime(3784): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
04-15 11:22:28.063: E/AndroidRuntime(3784): at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
04-15 11:22:28.063: E/AndroidRuntime(3784): at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
04-15 11:22:28.063: E/AndroidRuntime(3784): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
04-15 11:22:28.063: E/AndroidRuntime(3784): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
04-15 11:22:28.063: E/AndroidRuntime(3784): ... 11 more
:
{{1}}
答案 0 :(得分:0)
最后我制作了一个内部的匿名内部类型来解决问题以及检查设备是否有NFC芯片的条件。
mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
if (mNfcAdapter != null) {
mNfcAdapter.setNdefPushMessageCallback(new CreateNdefMessageCallback() {...}, this);
}
我还在此代码周围添加了第二个测试,以检查Android的运行版本。