1

我在 Android 应用程序中遇到问题,我的活动实现CreateNdefMessageCallbackAPI 14+.

public NdefMessage createNdefMessage(NfcEvent event)我用.注释了实现的方法@TargetApi(14)。它在 Android 4 设备上运行良好,但在低于 Android 4.0 的设备上加载时会崩溃!

唯一的解决方案是为实现创建另一个class?或者有可能有一个有条件的实现@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
4

1 回答 1

0

最后我做了一个内部匿名内部类型来解决这个问题和一个条件来检查设备是否有 NFC 芯片。

mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
if (mNfcAdapter != null) {
   mNfcAdapter.setNdefPushMessageCallback(new CreateNdefMessageCallback() {...}, this);
}

我还围绕此代码添加了第二个测试,以检查 Android 的运行版本。

于 2013-04-16T07:16:09.060 回答