java.lang.RuntimeException:将结果 ResultInfo{who=null, request=1888, result=0, data=null} 传递给活动失败

发布于 2024-12-19 18:45:50 字数 2628 浏览 1 评论 0原文

我的应用程序允许用户按下按钮,打开相机,他们可以拍照,照片将显示在 ImageView 中。如果用户在相机打开时按后退或取消,我会强制关闭 - 无法将结果 ResultInfo{who=null, request=1888, result=0, data=null} 传递给活动...所以我猜测结果=0 是我需要插入什么来使此停止强制关闭的问题吗?

下面是我的代码。我知道我忘记了一些东西,但就是想不起来! (诚​​然,我学习 Android 开发大约有两周时间)。感谢您的帮助!

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);
        }  

我想我需要在某个地方有一个“其他”,但我不知道该怎么做。

下面是日志猫

    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
        

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
        

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

清风无影 2024-12-26 18:45:50

添加第一个条件应该有效:

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);
        }
    }
}

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);
        }
    }
}
忘年祭陌 2024-12-26 18:45:50

对于 Kotlin 用户,不要忘记添加 ?在数据中:意图?
喜欢

public override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {}

For Kotlin Users don't forget to add ? in data: Intent?
like

public override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {}
囍笑 2024-12-26 18:45:50

如果用户取消请求,数据将返回为NULL。当您调用 data.getExtras().get("data"); 时,线程将抛出 nullPointerException。我认为你只需要添加一个条件来检查返回的数据是否为空。

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

If the user cancel the request, the data will be returned as NULL. The thread will throw a nullPointerException when you call data.getExtras().get("data");. I think you just need to add a conditional to check if the data returned is null.

protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
    if (requestCode == CAMERA_REQUEST) {
       if (data != null)
       {         
           Bitmap photo = (Bitmap) data.getExtras().get("data"); 
           imageView.setImageBitmap(photo);
       }
}  
逐鹿 2024-12-26 18:45:50

对于 Kotlin 用户

您只需在 onActivityResult 中添加带有 Intent 的 ?,因为如果用户取消,数据可能为 null交易或出现任何问题。因此,我们需要在 onActivityResult 中将数据定义为 nullable

只需将 SampleActivity 的 onActivityResult 签名替换为以下内容:

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?)

For Kotlin Users

You just need to add ? with Intent in onActivityResult as the data can be null if user cancels the transaction or anything goes wrong. So we need to define data as nullable in onActivityResult

Just replace onActivityResult signature of SampleActivity with below:

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?)
美男兮 2024-12-26 18:45:50

我建议使用这个:

  1. 检索父亲Intent

    意图intentParent = getIntent();
    
  2. 直接传达消息。

    setResult(RESULT_OK,intentParent);
    

这可以防止其活动丢失,从而产生空数据错误。

I recommend using this:

  1. Retrieve the father Intent.

    Intent intentParent = getIntent();
    
  2. Convey the message directly.

    setResult(RESULT_OK, intentParent);
    

This prevents the loss of its activity which would generate a null data error.

妳是的陽光 2024-12-26 18:45:50
 protected void onActivityResult(int requestCode, int resultCode, Intent data) 
    {  
        if (requestCode == CAMERE_REQUEST && resultCode == RESULT_OK && data != null) 
        {  
           Bitmap photo = (Bitmap) data.getExtras().get("data"); 
           imageView.setImageBitmap(photo);
        } 
}

您可以检查 resultCode 是否等于 RESULT_OK,只有在拍摄并选择照片并且一切正常的情况下才会出现这种情况。这里的 if 子句应该检查每个条件。

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

You can check if the resultCode equals RESULT_OK this will only be the case if a picture is taken and selected and everything worked. This if clause here should check every condition.

恋竹姑娘 2024-12-26 18:45:50

这是我的情况

startActivityForResult(intent, PICK_IMAGE_REQUEST);

我定义了两个具有相同值的请求代码 PICK_IMAGE_REQUESTSCAN_BARCODE_REQUEST ,例如。

static final int BARCODE_SCAN_REQUEST = 1;

static final int PICK_IMAGE_REQUEST = 1;

这也可能导致问题

this is my case

startActivityForResult(intent, PICK_IMAGE_REQUEST);

I defined two request code PICK_IMAGE_REQUESTand SCAN_BARCODE_REQUEST with the same value, eg.

static final int BARCODE_SCAN_REQUEST = 1;

static final int PICK_IMAGE_REQUEST = 1;

this could also causes the problem

坏尐絯℡ 2024-12-26 18:45:50

我收到此错误消息是因为我在主线程上使用网络,而新版本的 Android 有一个“严格”策略来防止这种情况发生。要解决这个问题,只需将任何网络连接调用放入 AsyncTask 中即可。

例子:

    AsyncTask<CognitoCachingCredentialsProvider, Integer, Void> task = new AsyncTask<CognitoCachingCredentialsProvider, Integer, Void>() {

        @Override
        protected Void doInBackground(CognitoCachingCredentialsProvider... params) {
            AWSSessionCredentials creds = credentialsProvider.getCredentials();
            String id = credentialsProvider.getCachedIdentityId();
            credentialsProvider.refresh();
            Log.d("wooohoo", String.format("id=%s, token=%s", id, creds.getSessionToken()));
            return null;
        }
    };

    task.execute(credentialsProvider);

I had this error message show up for me because I was using the network on the main thread and new versions of Android have a "strict" policy to prevent that. To get around it just throw whatever network connection call into an AsyncTask.

Example:

    AsyncTask<CognitoCachingCredentialsProvider, Integer, Void> task = new AsyncTask<CognitoCachingCredentialsProvider, Integer, Void>() {

        @Override
        protected Void doInBackground(CognitoCachingCredentialsProvider... params) {
            AWSSessionCredentials creds = credentialsProvider.getCredentials();
            String id = credentialsProvider.getCachedIdentityId();
            credentialsProvider.refresh();
            Log.d("wooohoo", String.format("id=%s, token=%s", id, creds.getSessionToken()));
            return null;
        }
    };

    task.execute(credentialsProvider);
陪你搞怪i 2024-12-26 18:45:50

我也遇到了这个问题,我通过添加两个条件解决了它,一个是:

resultCode != null

另一个是:

resultCode != RESULT_CANCELED

I also encountered this question, I solved it through adding two conditions one is:

resultCode != null

the other is:

resultCode != RESULT_CANCELED
自控 2024-12-26 18:45:50

我的问题出在被调用的活动中,当它尝试通过“完成”返回到上一个活动时。我错误地设定了意图。以下代码是 Kotlin。

我正在这样做:

        intent.putExtra("foo", "bar")
        finish()

当我应该这样做时:

        val result = Intent()
        result.putExtra("foo", "bar")
        setResult(Activity.RESULT_OK, result)
        finish()

My problem was in the called activity when it tries to return to the previous activity by "finishing." I was incorrectly setting the intent. The following code is Kotlin.

I was doing this:

        intent.putExtra("foo", "bar")
        finish()

When I should have been doing this:

        val result = Intent()
        result.putExtra("foo", "bar")
        setResult(Activity.RESULT_OK, result)
        finish()
赤濁 2024-12-26 18:45:50

当我尝试传递可序列化的模型对象时,我遇到了这个问题。在该模型内,另一个模型是一个变量,但不可序列化。这就是我面临这个问题的原因。确保模型内的所有模型都是可序列化的。

I faced this problem when I tried to pass a serializable model object. Inside that model, another model was a variable but that wasn't serializable. That's why I face this problem. Make sure all the model inside of an model is serializable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文