从相机拍照可以在纵向模式下正常工作,但在横向模式下可以获得异常

时间:2013-11-19 07:00:05

标签: android nullpointerexception android-camera-intent landscape-portrait

在我的应用程序中,我从相机拍摄照片并在ImageView中显示。这在肖像模式下工作得非常好,但是当我从相机拍摄照片后,在点击相机的“完成”按钮时,我以横向模式使用应用程序。应用程序崩溃,我得到RunTimeException。我的纵向和横向布局文件类似。我无法解决这个问题,我需要一些建议。请尽快帮助我。

Logcat中的错误如下所示:

 11-19 14:35:19.404: E/AndroidRuntime(17122): FATAL EXCEPTION: main

11-19 14:35:19.404: E/AndroidRuntime(17122): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=2500, result=-1, data=null} to activity {com.sample.postcare2/com.sample.postcare2.HowItWorksActivity}: java.lang.NullPointerException

11-19 14:35:19.404: E/AndroidRuntime(17122):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3387)
11-19 14:35:19.404: E/AndroidRuntime(17122):    at android.app.ActivityThread.handleSendResult(ActivityThread.java:3437)

11-19 14:35:19.404: E/AndroidRuntime(17122):    at android.app.ActivityThread.access$1100(ActivityThread.java:139)
11-19 14:35:19.404: E/AndroidRuntime(17122):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1291)

11-19 14:35:19.404: E/AndroidRuntime(17122):    at android.os.Handler.dispatchMessage(Handler.java:99)

11-19 14:35:19.404: E/AndroidRuntime(17122):    at android.os.Looper.loop(Looper.java:154)
11-19 14:35:19.404: E/AndroidRuntime(17122):    at android.app.ActivityThread.main(ActivityThread.java:4944)

11-19 14:35:19.404: E/AndroidRuntime(17122):    at java.lang.reflect.Method.invokeNative(Native Method)
11-19 14:35:19.404: E/AndroidRuntime(17122):    at java.lang.reflect.Method.invoke(Method.java:511)

11-19 14:35:19.404: E/AndroidRuntime(17122):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)

11-19 14:35:19.404: E/AndroidRuntime(17122):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)

11-19 14:35:19.404: E/AndroidRuntime(17122):    at dalvik.system.NativeStart.main(Native Method)

11-19 14:35:19.404: E/AndroidRuntime(17122): Caused by: java.lang.NullPointerException

11-19 14:35:19.404: E/AndroidRuntime(17122):    at com.sample.postcare2.HowItWorksActivity.onActivityResult(HowItWorksActivity.java:218)

11-19 14:35:19.404: E/AndroidRuntime(17122):    at android.app.Activity.dispatchActivityResult(Activity.java:4740)

11-19 14:35:19.404: E/AndroidRuntime(17122):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3383)
11-19 14:35:19.404: E/AndroidRuntime(17122):    ... 11 more

代码如下所示:

public class HowItWorksActivity extends Activity {

ImageButton btn_Account,btn_Photo,btn_Edit,btn_Flip,btn_Post;
RelativeLayout rl;
private static final int CAMERA_PIC_REQUEST = 2500;
private static final int SELECT_PICTURE = 1;
Bitmap bmap_image;
Uri uri_image;

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);     
    setContentView(R.layout.activity_how_it_works);
    addListenerOnButton();
          }
   private void addListenerOnButton() {
      btn_Photo = (ImageButton) findViewById(R.id.btn_Photo);
      btn_Photo.setOnClickListener(new View.OnClickListener() {

//这里有2个选项:1是拍照,另一个是从图库中选择。拍照的代码在下方..

  private void takepicture() {
    ContentValues values = new ContentValues();
values.put(Media.TITLE,"PostCard Image");
values.put(Media.DESCRIPTION, "Image captured by camera via PostCare");
uri_image = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, values);
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri_image);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
}

    @SuppressWarnings("deprecation")
protected void onActivityResult(int requestCode,int resultCode,Intent data){
       if(requestCode == CAMERA_PIC_REQUEST && resultCode == RESULT_OK){
        //image = (Bitmap) data.getExtras().get("data");
        //bmap_image = image;                   
        imgview.setImageURI(uri_image);
    }
         super.onActivityResult(requestCode, resultCode, data);     
}

   @Override
protected void onSaveInstanceState(Bundle outState) {
    if(uri_image!=null)
        outState.putParcelable("post_card", uri_image);                 
    super.onSaveInstanceState(outState);
}

1 个答案:

答案 0 :(得分:1)

您应该在自己的Activity

中阻止生命周期的更改
android:configChanges="keyboardHidden|orientation|screenLayout|screenSize"