从父Activity调用autoFocus()返回NPE

时间:2013-09-10 17:18:18

标签: android commonsware-cwac

我已经实例化了一个扩展CameraFragment(并实现了TabListener)的片段类型,我试图从父Activity调用autoFocus()。从扩展CameraFragment的片段调用autoFocus()工作正常,但从父Activity调用它会产生NPE。我在主Activity中监听一个onKeyDown()事件,它按照logcat按预期工作。这是我的NPE追踪:

09-10 10:00:16.410: D/app(5283): onKeyDown: 80

09-10 10:00:16.415: D/AndroidRuntime(5283): Shutting down VM

09-10 10:00:16.415: W/dalvikvm(5283): threadid=1: thread exiting with uncaught exception (group=0x40f7b2a0)

09-10 10:00:16.425: E/AndroidRuntime(5283): FATAL EXCEPTION: main

09-10 10:00:16.425: E/AndroidRuntime(5283): java.lang.NullPointerException

09-10 10:00:16.425: E/AndroidRuntime(5283):     at com.commonsware.cwac.camera.CameraFragment.autoFocus(CameraFragment.java:96)

09-10 10:00:16.425: E/AndroidRuntime(5283):     at com.me.app.appCameraFragment.callAutoFocus(cameraFragment.java:232)

09-10 10:00:16.425: E/AndroidRuntime(5283):     at com.me.app.MainTabActivity.onKeyDown(MainTabActivity.java:264)

09-10 10:00:16.425: E/AndroidRuntime(5283):     at android.view.KeyEvent.dispatch(KeyEvent.java:2705)

09-10 10:00:16.425: E/AndroidRuntime(5283):     at android.app.Activity.dispatchKeyEvent(Activity.java:2423)

09-10 10:00:16.425: E/AndroidRuntime(5283):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:2019)

09-10 10:00:16.425: E/AndroidRuntime(5283):     at android.view.ViewRootImpl.deliverKeyEventPostIme(ViewRootImpl.java:3851)

09-10 10:00:16.425: E/AndroidRuntime(5283):     at android.view.ViewRootImpl.handleImeFinishedEvent(ViewRootImpl.java:3799)

09-10 10:00:16.425: E/AndroidRuntime(5283):     at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:2934)

09-10 10:00:16.425: E/AndroidRuntime(5283):     at android.os.Handler.dispatchMessage(Handler.java:99)

09-10 10:00:16.425: E/AndroidRuntime(5283):     at android.os.Looper.loop(Looper.java:137)

09-10 10:00:16.425: E/AndroidRuntime(5283):     at android.app.ActivityThread.main(ActivityThread.java:4921)

09-10 10:00:16.425: E/AndroidRuntime(5283):     at java.lang.reflect.Method.invokeNative(Native Method)

09-10 10:00:16.425: E/AndroidRuntime(5283):     at java.lang.reflect.Method.invoke(Method.java:511)

09-10 10:00:16.425: E/AndroidRuntime(5283):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1036)

09-10 10:00:16.425: E/AndroidRuntime(5283):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:803)

09-10 10:00:16.425: E/AndroidRuntime(5283):     at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

我不确定您正在使用的代码版本,但第96行是当前CameraFragment中的右括号。

如果autoFocus() NullPointerException cameraView null cameraView null onCreateView() {{}}是否尚未调用{{1}}。在请求自动对焦之前,您需要片段可见并完全创建。