java.lang.RuntimeException的:不提供结果ResultInfo {谁= NULL,请求= 1888,结果为0,数据= NULL}到活动结果、数据、RuntimeExceptio

2023-09-12 06:19:45 作者:眉间星河

我的应用程序允许用户在preSS一个按钮,它会打开相机,他们可以拍一张照片,它会在ImageView的显示出来。如果用户presses回或取消,而相机是开放的,我得到这个强制关闭 - 未交付结果ResultInfo {谁= NULL,请求= 1888,结果为0,数据= NULL}到活动......所以我猜测结果= 0是什么我需要插入,使这个站的力量关闭的问题?

下面是我的code。我知道我忘了什么东西,但只是不能看着办吧! (诚​​然,我大约2星期到学习Android开发)。感谢您的帮助!

 私有静态最终诠释CAMERA_REQUEST = 1888;
    私人ImageView的ImageView的;


@覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);
    this.imageView =(ImageView的)this.findViewById(R.id.photostrippic1);

    ImageView的photoButton =(ImageView的)this.findViewById(R.id.photostrippic1);



        photoButton.setOnClickListener(新View.OnClickListener(){
        公共无效的onClick(视图v){
            意图cameraIntent =新的意图(android.provider.MediaStore.ACTION_IM​​AGE_CAPTURE);
            startActivityForResult(cameraIntent,CAMERA_REQUEST);
        }

    });

  保护无效onActivityResult(INT申请code,INT结果code,意图数据){
    如果(要求code == CAMERA_REQUEST){
        。位图照片=(位图)data.getExtras()获得(数据);
        imageView.setImageBitmap(照片);
    }
 

我想我需要一个东西在那里的地方,但我不知道究竟要做到这一点。

下面

是logcat的

  java.lang.RuntimeException的:不提供结果ResultInfo {谁= NULL,请求= 1888,结果为0,数据= NULL}到活动{photo.booth.app /照片。 booth.app.PhotoboothActivity}:显示java.lang.NullPointerException
    在android.app.ActivityThread.deliverResults(ActivityThread.java:2934)
    在android.app.ActivityThread.handleSendResult(ActivityThread.java:2986)
    在android.app.ActivityThread.access $ 2000(ActivityThread.java:132)
    在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1068)
    在android.os.Handler.dispatchMessage(Handler.java:99)
    在android.os.Looper.loop(Looper.java:150)
    在android.app.ActivityThread.main(ActivityThread.java:4293)
    在java.lang.reflect.Method.invokeNative(本机方法)
    在java.lang.reflect.Method.invoke(Method.java:507)
    在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:849)
    在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
    在dalvik.system.NativeStart.main(本机方法)
    显示java.lang.NullPointerException:产生的原因
    在photo.booth.app.PhotoboothActivity.onActivityResult(PhotoboothActivity.java:76)
    在android.app.Activity.dispatchActivityResult(Activity.java:4108)
    在android.app.ActivityThread.deliverResults(ActivityThread.java:2930)
    ... 11更多
 

解决方案

添加这个第一个条件应该工作:

 保护无效onActivityResult(INT申请code,INT结果code,意图数据){
    如果(结果code!= RESULT_CANCELED){
        如果(要求code == CAMERA_REQUEST){
            。位图照片=(位图)data.getExtras()获得(数据);
            imageView.setImageBitmap(照片);
        }
    }
}
 
Java开发之规范

My app allows the user to press a button, it opens the camera, they can take a photo and it will show up in an imageview. If the user presses back or cancel while the camera is open I get this force close - Failure delivering result ResultInfo{who=null, request=1888, result=0, data=null} to activity... so i am guessing the result=0 is the issue what would I need to insert to make this stop force closing?

Below is my code. I know I am forgetting something but just cant figure it out! (Admittedly I am about 2 weeks into learning android development). Thanks for any help!

    private static final int CAMERA_REQUEST = 1888; 
    private ImageView imageView;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    this.imageView = (ImageView)this.findViewById(R.id.photostrippic1);

    ImageView photoButton = (ImageView) this.findViewById(R.id.photostrippic1);



        photoButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent cameraIntent = new      Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
            startActivityForResult(cameraIntent, CAMERA_REQUEST); 
        }

    });

  protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
    if (requestCode == CAMERA_REQUEST) {  
        Bitmap photo = (Bitmap) data.getExtras().get("data"); 
        imageView.setImageBitmap(photo);
    }  

I guess I would need a "else" in there somewhere but I dont exactly know to do that.

below is the logcat

    java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1888, result=0, data=null} to activity {photo.booth.app/photo.booth.app.PhotoboothActivity}: java.lang.NullPointerException
    at android.app.ActivityThread.deliverResults(ActivityThread.java:2934)
    at android.app.ActivityThread.handleSendResult(ActivityThread.java:2986)
    at android.app.ActivityThread.access$2000(ActivityThread.java:132)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1068)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:150)
    at android.app.ActivityThread.main(ActivityThread.java:4293)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:507)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
    at dalvik.system.NativeStart.main(Native Method)
    Caused by: java.lang.NullPointerException
    at photo.booth.app.PhotoboothActivity.onActivityResult(PhotoboothActivity.java:76)
    at android.app.Activity.dispatchActivityResult(Activity.java:4108)
    at android.app.ActivityThread.deliverResults(ActivityThread.java:2930)
    ... 11 more

解决方案

Adding this first conditional should work:

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if(resultCode != RESULT_CANCELED){
        if (requestCode == CAMERA_REQUEST) {  
            Bitmap photo = (Bitmap) data.getExtras().get("data"); 
            imageView.setImageBitmap(photo);
        }
    }
}