获得NullPointerException异常在我定制的Android相机应用(Getting N

2019-10-17 13:05发布

我试图建立在Android的我第一个自定义相机应用程序。

在模拟器上我得到:

“不幸的是,[应用程序名称]已停止”

这是错误日志:(如果你要我贴完整的日志,请说出来)

11-13 19:14:04.023: E/AndroidRuntime(4474): FATAL EXCEPTION: main
11-13 19:14:04.023: E/AndroidRuntime(4474): java.lang.NullPointerException
11-13 19:14:04.023: E/AndroidRuntime(4474):     at com.kachingcamapp.CameraApplication.surfaceChanged(CameraApplication.java:80)
11-13 19:14:04.023: E/AndroidRuntime(4474):     at android.view.SurfaceView.updateWindow(SurfaceView.java:554)
11-13 19:14:04.023: E/AndroidRuntime(4474):     at android.view.SurfaceView.access$000(SurfaceView.java:81)
11-13 19:14:04.023: E/AndroidRuntime(4474):     at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:169)
11-13 19:14:04.023: E/AndroidRuntime(4474):     at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:671)
11-13 19:14:04.023: E/AndroidRuntime(4474):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1818)
11-13 19:14:04.023: E/AndroidRuntime(4474):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:998)
11-13 19:14:04.023: E/AndroidRuntime(4474):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4212)
11-13 19:14:04.023: E/AndroidRuntime(4474):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
11-13 19:14:04.023: E/AndroidRuntime(4474):     at android.view.Choreographer.doCallbacks(Choreographer.java:555)
11-13 19:14:04.023: E/AndroidRuntime(4474):     at android.view.Choreographer.doFrame(Choreographer.java:525)
11-13 19:14:04.023: E/AndroidRuntime(4474):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
11-13 19:14:04.023: E/AndroidRuntime(4474):     at android.os.Handler.handleCallback(Handler.java:615)
11-13 19:14:04.023: E/AndroidRuntime(4474):     at android.os.Handler.dispatchMessage(Handler.java:92)
11-13 19:14:04.023: E/AndroidRuntime(4474):     at android.os.Looper.loop(Looper.java:137)
11-13 19:14:04.023: E/AndroidRuntime(4474):     at android.app.ActivityThread.main(ActivityThread.java:4745)
11-13 19:14:04.023: E/AndroidRuntime(4474):     at java.lang.reflect.Method.invokeNative(Native Method)
11-13 19:14:04.023: E/AndroidRuntime(4474):     at java.lang.reflect.Method.invoke(Method.java:511)
11-13 19:14:04.023: E/AndroidRuntime(4474):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-13 19:14:04.023: E/AndroidRuntime(4474):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-13 19:14:04.023: E/AndroidRuntime(4474):     at dalvik.system.NativeStart.main(Native Method)

这里是我的代码:

public class CameraApplication extends Activity implements OnClickListener,
                            SurfaceHolder.Callback, Camera.PictureCallback { 

    private static final String TAG = "AppDebug";
    SurfaceView cameraView; 
    SurfaceHolder surfaceHolder; 
    Camera camera; 

    @SuppressWarnings("deprecation")
    @Override 
    public void onCreate(Bundle savedInstanceState)  { 
        super.onCreate(savedInstanceState); 

        setContentView(R.layout.cam_view); 

        cameraView = (SurfaceView) this.findViewById(R.id.CameraView); 

        surfaceHolder = cameraView.getHolder(); 
        surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 
        surfaceHolder.addCallback(this); 

        cameraView.setFocusable(true); 
        cameraView.setFocusableInTouchMode(true); 
        cameraView.setClickable(true); 

        cameraView.setOnClickListener(this); 
        Log.d(TAG,"before calling surfaceCreated");
        surfaceCreated(surfaceHolder);
        Log.d(TAG,"end of onCreate");
    } 

    public void onClick(View v) { 
        camera.takePicture(null, null, this); 
    } 

    public void onPictureTaken(byte[] data, Camera camera) { 
       Uri imageFileUri = 
          getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, new ContentValues()); 
        try { 
              OutputStream imageFileOS = 
                       getContentResolver().openOutputStream(imageFileUri); 
            imageFileOS.write(data); 
            imageFileOS.flush(); 
            imageFileOS.close(); 
        } catch (FileNotFoundException e) { 
            Toast t = Toast.makeText(this,e.getMessage(), Toast.LENGTH_SHORT); 
            t.show(); 
        } catch (IOException e) { 
            Toast t = Toast.makeText(this,e.getMessage(), Toast.LENGTH_SHORT); 
            t.show(); 
        } 
        camera.startPreview(); 
    } 

    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { 
        camera.startPreview(); 
    } 

    public void surfaceCreated(SurfaceHolder holder) { 
        camera = Camera.open(); 

        try { 
            if(camera==null){
                throw new Exception("camera is null");
            }
            camera.setPreviewDisplay(holder); 
   } 
        catch (IOException exception) 
        { 
                      camera.release(); 
                      Log.d(TAG,exception.toString());
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
    } 

    public void surfaceDestroyed(SurfaceHolder holder) { 
           camera.stopPreview(); 
        camera.release(); 
    } 
} // End the Activity

而这里的AndroidManifest XML:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.camapp"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />

    <uses-permission android:name="android.permission.CAMERA" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".CameraApplication"
            android:label="@string/title_activity_starting" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

现在我知道从上面的问题在于surfaceChanged方法,更确切地说,该camera.startPreview这是代码的方法中的唯一的一块。

但我真的没有看到任何破损或错。 东西肯定是null的,但我不知道是什么。

Answer 1:

camera的实例是在你的空onSurfaceChanged()方法,你应该确认其是否为空,实例化它,然后你可以调用startPreview()

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { 
        if(camera == null)
           camera = Camera.open();
        camera.startPreview(); 
    } 


Answer 2:

由于摄像头 ,所以只检查if(camera != null)之前camera.startPreview();surfaceChanged()

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { 
       if(camera == null) 
          camera = Camera.open();
          camera.startPreview(); 
} 


Answer 3:

你说你在得到错误:

public void onClick(View v) { 
  camera.takePicture(null, null, this); 
}

在comments.I之一,因为摄像机在该moment.Modify你作为下面的代码具有空值也有这种issue.Thats:

public void onClick(View view) {
  if (camera != null) {
    Log.d("camera state","camera is NOT null");  
  }else{
    Log.d("camera state","camera is null");
    camera = android.hardware.Camera.open();
}
  camera.takePicture(null, null,new PhotoHandler(getApplicationContext()));
}

PS:我有PhotoHandler类在我的应用程序,该修改根据您的req.Hope它帮助!



文章来源: Getting NullPointerException in my custom Android camera-application