我正在尝试在Android中构建我的第一个自定义相机应用程序。
在模拟器上我得到:
“不幸的是,[app name]已停止”
这是错误日志:(如果您希望我粘贴完整日志,请说明)
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
,但我不知道是什么。
答案 0 :(得分:0)
因为相机 null ,所以只需在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();
}
答案 1 :(得分:0)
camera
方法中的onSurfaceChanged()
实例为空,您应该验证它是否为空,实例化它,然后您可以调用startPreview()
:
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
if(camera == null)
camera = Camera.open();
camera.startPreview();
}
答案 2 :(得分:0)
你说你得错了:
public void onClick(View v) {
camera.takePicture(null, null, this);
}
在你的一条评论中。我也遇到过这个问题。因为当时相机的值为空。修改你的代码如下:
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类,根据你的要求修改它。希望它有帮助!