我在横向模式下使用此应用程序时工作正常,但每当我在纵向模式下使用我的应用程序获取空指针异常时
日志:
12-12 17:37:34.865: E/AndroidRuntime(20671): FATAL EXCEPTION: main
12-12 17:37:34.865: E/AndroidRuntime(20671): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.camera/com.example.camera.CameraLauncherActivity}: java.lang.NullPointerException
12-12 17:37:34.865: E/AndroidRuntime(20671): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
12-12 17:37:34.865: E/AndroidRuntime(20671): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
12-12 17:37:34.865: E/AndroidRuntime(20671): at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3576)
12-12 17:37:34.865: E/AndroidRuntime(20671): at android.app.ActivityThread.access$800(ActivityThread.java:140)
12-12 17:37:34.865: E/AndroidRuntime(20671): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1243)
12-12 17:37:34.865: E/AndroidRuntime(20671): at android.os.Handler.dispatchMessage(Handler.java:99)
12-12 17:37:34.865: E/AndroidRuntime(20671): at android.os.Looper.loop(Looper.java:137)
12-12 17:37:34.865: E/AndroidRuntime(20671): at android.app.ActivityThread.main(ActivityThread.java:4921)
12-12 17:37:34.865: E/AndroidRuntime(20671): at java.lang.reflect.Method.invokeNative(Native Method)
12-12 17:37:34.865: E/AndroidRuntime(20671): at java.lang.reflect.Method.invoke(Method.java:511)
12-12 17:37:34.865: E/AndroidRuntime(20671): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1036)
12-12 17:37:34.865: E/AndroidRuntime(20671): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:803)
12-12 17:37:34.865: E/AndroidRuntime(20671): at dalvik.system.NativeStart.main(Native Method)
12-12 17:37:34.865: E/AndroidRuntime(20671): Caused by: java.lang.NullPointerException
12-12 17:37:34.865: E/AndroidRuntime(20671): at com.example.camera.CameraLauncherActivity.onCreate(CameraLauncherActivity.java:103)
12-12 17:37:34.865: E/AndroidRuntime(20671): at android.app.Activity.performCreate(Activity.java:5191)
12-12 17:37:34.865: E/AndroidRuntime(20671): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
12-12 17:37:34.865: E/AndroidRuntime(20671): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
12-12 17:37:34.865: E/AndroidRuntime(20671): ... 12 more
第103行是:
captureButton.setOnClickListener(new View.OnClickListener() {
CameraLauncherActivity.java: -
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);
mCamera = getCameraInstance();
mCameraPreview = new PreviewSurface(this, mCamera);
FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
preview.addView(mCameraPreview);
Button captureButton = (Button) findViewById(R.id.btnCapture);
Log.d(CameraLauncherActivity.LOG_TAG, "captureButton :: "
+ captureButton);
captureButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mCamera.takePicture(null, null, mPicture);
Log.d(CameraLauncherActivity.LOG_TAG, "mCamera.takePicture :: "
+ mCamera);
}
});
}
答案 0 :(得分:1)
你或者在肖像模式下没有btnCapture
,或者其中有一些拼写错误。