0

如果我单击相机按钮,单独的相机活动会在我的应用程序中产生问题,拍照后,相机活动会停止。

 imageView = (ImageView)findViewById(R.id.imageview01);
 startBtn = (Button) findViewById(R.id.startBtn);
 startBtn.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        try {
            startCamera();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
});
 public void startCamera() 
      throws IOException {
  Log.d("TDM_CAMERA", "Starting camera on the phone...");
  File photosDir = new File(Environment.getExternalStorageDirectory(), "photos");
  if (!photosDir.isDirectory()) {
      photosDir.mkdirs();
  }
  File imageFile = File.createTempFile("testphoto", ".jpg", photosDir);
  Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
  intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(imageFile));
  intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
  startActivityForResult(intent, 1337);
}

现在logcat在点击相机按钮时抛出空点异常,之后它会拍照,因为空点异常突然我的应用程序停止了。

02-21 04:13:18.672: E/AndroidRuntime(1456): FATAL EXCEPTION: main
02-21 04:13:18.672: E/AndroidRuntime(1456): java.lang.RuntimeException: Unable to instantiate application android.app.Application: java.lang.NullPointerException
02-21 04:13:18.672: E/AndroidRuntime(1456):     at android.app.LoadedApk.makeApplication(LoadedApk.java:504)
02-21 04:13:18.672: E/AndroidRuntime(1456):     at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4364)
02-21 04:13:18.672: E/AndroidRuntime(1456):     at android.app.ActivityThread.access$1300(ActivityThread.java:141)
02-21 04:13:18.672: E/AndroidRuntime(1456):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1294)
02-21 04:13:18.672: E/AndroidRuntime(1456):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-21 04:13:18.672: E/AndroidRuntime(1456):     at android.os.Looper.loop(Looper.java:137)
02-21 04:13:18.672: E/AndroidRuntime(1456):     at android.app.ActivityThread.main(ActivityThread.java:5039)
02-21 04:13:18.672: E/AndroidRuntime(1456):     at java.lang.reflect.Method.invokeNative(Native Method)
02-21 04:13:18.672: E/AndroidRuntime(1456):     at java.lang.reflect.Method.invoke(Method.java:511)
02-21 04:13:18.672: E/AndroidRuntime(1456):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-21 04:13:18.672: E/AndroidRuntime(1456):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-21 04:13:18.672: E/AndroidRuntime(1456):     at dalvik.system.NativeStart.main(Native Method)
02-21 04:13:18.672: E/AndroidRuntime(1456): Caused by: java.lang.NullPointerException
02-21 04:13:18.672: E/AndroidRuntime(1456):     at android.app.LoadedApk.initializeJavaContextClassLoader(LoadedApk.java:379)
02-21 04:13:18.672: E/AndroidRuntime(1456):     at android.app.LoadedApk.getClassLoader(LoadedApk.java:322)
02-21 04:13:18.672: E/AndroidRuntime(1456):     at android.app.LoadedApk.makeApplication(LoadedApk.java:496)
02-21 04:13:18.672: E/AndroidRuntime(1456):     ... 11 more

现在LogCat显示我的活动抛出空点异常,这使我的应用程序崩溃 这是我的活动结果

if (requestCode == IMAGE_CAPTURE) {
      if (resultCode == RESULT_OK){
          Log.d("ANDRO_CAMERA","Picture taken!!!");
          imageView.setImageURI(imageUri);
      }
  }
4

2 回答 2

0

通过如下声明请求代码来尝试如下:

startActivityForResult(intent, IMAGE_CAPTURE);

或者

您可以向它提供 id,如下所示:

 private static final IMAGE_CAPTURE=1337;

并将其用作:

startActivityForResult(intent, IMAGE_CAPTURE);
于 2013-02-21T06:15:17.127 回答
0

如果您至少花几秒钟的时间来谷歌这个错误,您会注意到这与您的代码无关。

于 2013-02-21T07:02:00.490 回答