简体   繁体   English

Fragment中的OnActivityResult不被调用

[英]OnActivityResult in Fragment does not called

I know this question have been asked a few times before. 我知道这个问题已经问过几次了。 I've tried all of them but it doesn't work in my case. 我已经尝试了所有方法,但对我而言不起作用。

I'm trying to upload an image from Gallery or capture from Camera via intents. 我正在尝试从图库上传图像或通过意图从相机捕获图像。 So, the implementation is to call onActivityResult from the Fragment. 因此,实现是从Fragment调用onActivityResult When the button in Fragment is clicked, it returns NullPointerException in MainActivity. 单击Fragment中的按钮时,它将在MainActivity中返回NullPointerException

Here's the logcat error: 这是logcat错误:

java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=65538, result=0, data="imagepath"} to activity {com.example/com.example.activity.MainActivity}: java.lang.NullPointerException

In MainActivity : MainActivity

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    // Null Pointer Exception here
    Session.getActiveSession().onActivityResult(this, requestCode,
            resultCode, data);
}

Session.getActiveSession() came from Facebook as I am using Facebook SDK login. Session.getActiveSession()来自Facebook,因为我正在使用Facebook SDK登录。

If I comment out this line, the Camera/Gallery intent worked BUT I can't use Facebook SDK login button. 如果我在此行中注释掉,则表示相机/图库的意图有效,但我不能使用Facebook SDK登录按钮。

In UploadFragment : UploadFragment

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (resultCode != getActivity().RESULT_OK)
        return;

    switch (requestCode) {
        case CAMERA_REQUEST:
        //
        case SELECT_PICTURE:
        //
    }
}

The fragment's ActivitResult is called via this: 片段的ActivitResult通过以下方式调用:

    btnCamera.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            try {
                startActivityForResult(cameraIntent, CAMERA_REQUEST);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });

    btnGallery.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            // To open up a gallery browser
            startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PICTURE);
        }
    });

How do I solve this? 我该如何解决?


UPDATE 更新

I noticed that if I have logged in to Facebook, the problem doesn't occur as it found an active session. 我注意到,如果我已登录Facebook,则不会发现该问题,因为它发现了活动会话。 Otherwise, an error will occur. 否则,将发生错误。

I'm guessing that Facebook Session throws the error since there is no session involved. 我猜测Facebook会话会引发错误,因为不涉及任何会话。

Refer to my solution below. 请参阅下面的解决方案。

I noticed that I didn't logged in to Facebook. 我注意到我没有登录Facebook。 The problem here is that Facebook is trying to get its active session, but couldn't find it in the SharedPreference (I'm using both normal and social login). 这里的问题是Facebook尝试获取其活动会话,但在SharedPreference中找不到它(我同时使用普通登录和社交登录)。

Managed to solve the problem by a simple workaround. 通过简单的解决方法设法解决了问题。

Wrap the Session.getActiveSession with try-catch block. 用try-catch块包装Session.getActiveSession That will get rid of the crash. 那将摆脱崩溃。

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {

    super.onActivityResult(requestCode, resultCode, data);

    try {
        Session.getActiveSession().onActivityResult(this, requestCode,
                resultCode, data);
    } catch (Exception e) {
        // do nothing
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM