我是新來的android編程。我只想在android中製作自定義相機。爲了做到這一點,我用這個網站(http://developer.android.com/guide/topics/media/camera.html) 雖然我遵循網站的步驟,但我收到了一個錯誤,「不幸的是,您的應用程序已停止」。我搜索了關於「不幸已經停止」的所有問題,並且我在這裏閱讀了答案。但他們沒有幫助我。請幫幫我!Android「不幸已停止」錯誤
這裏是我的cameraActivity代碼:
public class CameraActivity extends Activity {
protected static final String TAG = null;
protected static final String MEDIA_TYPE_IMAGE = ".jpeg";
private static Camera mCamera;
private CameraPreview mPreview;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);
// Create an instance of Camera
mCamera = getCameraInstance();
// Create our Preview view and set it as the content of our activity.
mPreview = new CameraPreview(this, mCamera);
FrameLayout preview = (FrameLayout) findViewById (R.id.camera_preview);
preview.addView(mPreview);
final PictureCallback mPicture = new PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
if (pictureFile == null){
Throwable e = null;
Log.d(TAG, "Error creating media file, check storage permissions: " +
e.getMessage());
return;
}
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(data);
fos.close();
} catch (FileNotFoundException e) {
Log.d(TAG, "File not found: " + e.getMessage());
} catch (IOException e) {
Log.d(TAG, "Error accessing file: " + e.getMessage());
}
}
private File getOutputMediaFile(String mediaTypeImage) {
// TODO Auto-generated method stub
return null;
}
};
Button captureButton = (Button) findViewById(id.button_capture);
captureButton.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
// get an image from the camera
mCamera.takePicture(null, null, mPicture);
}});}
public static Camera getCameraInstance(){
Camera c = null;
try {
c = Camera.open(); // attempt to get a Camera instance
}
catch (Exception e){
// Camera is not available (in use or does not exist)
}
return c; // returns null if camera is unavailable
}}
,當我在logcat中點擊錯誤,這表明我這個
"mcamera.setPreviewDisplay(holder);"在此代碼:
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder mHolder;
private Camera mcamera;
public CameraPreview(Context context,Camera camera){
super(context);
mcamera=camera;
mHolder=getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
public void surfaceCreated(SurfaceHolder holder) {
// The Surface has been created, now tell the camera where to draw the preview.
try {
mcamera.setPreviewDisplay(holder);
mcamera.startPreview();
} catch (IOException e) {
Log.d(TAG, "Error setting camera preview: " + e.getMessage());
}
}
在logcat的
E/AndroidRuntime(1071): FATAL EXCEPTION: main E/AndroidRuntime(1071): java.lang.NullPointerException E/AndroidRuntime(1071): at com.example.ntry.CameraPreview.surfaceCreated(CameraPreview.java:32) E/AndroidRuntime(1071): at android.view.SurfaceView.updateWindow(SurfaceView.java:569) E/AndroidRuntime(1071): at android.view.SurfaceView.access$000(SurfaceView.java:86) E/AndroidRuntime(1071): at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:174) E/AndroidRuntime(1071): at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:680) E/AndroidRuntime(1071): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1842) E/AndroidRuntime(1071): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989) E/AndroidRuntime(1071): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351) E/AndroidRuntime(1071): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749) E/AndroidRuntime(1071): at android.view.Choreographer.doCallbacks(Choreographer.java:562) E/AndroidRuntime(1071): at android.view.Choreographer.doFrame(Choreographer.java:532) E/AndroidRuntime(1071): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735) E/AndroidRuntime(1071): at android.os.Handler.handleCallback(Handler.java:725) E/AndroidRuntime(1071): at android.os.Handler.dispatchMessage(Handler.java:92) E/AndroidRuntime(1071): at android.os.Looper.loop(Looper.java:137) E/AndroidRuntime(1071): at android.app.ActivityThread.main(ActivityThread.java:5041) E/AndroidRuntime(1071): at java.lang.reflect.Method.invokeNative(Native Method) E/AndroidRuntime(1071): at java.lang.reflect.Method.invoke(Method.java:511) E/AndroidRuntime(1071): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) E/AndroidRuntime(1071): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) E/AndroidRuntime(1071): at dalvik.system.NativeStart.main(Native Method)
什麼是在線32 CameraPreview? – Raghunandan
@Raghunandan第32行 - >「mcamera.setPreviewDisplay(holder);」 – Adaichi
如果你想要好的攝像頭代碼示例看看這個項目由大學生完成https://github.com/ProjPossibility/2014-CSUN-Cam4Blind – portfoliobuilder