Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ASNETutorial]Application always crashing after changing orientation (rotating device) #47

Open
ITurchenko opened this issue Nov 4, 2014 · 6 comments

Comments

@ITurchenko
Copy link

Hello again.

How to reproduce bug:
Open example project (https://github.com/gorbin/ASNETutorial) on device, rotate device at least once and then press any of the social buttons.

Result for facebook:

Process: com.github.gorbin.asnetutorial, PID: 13770
    java.lang.NullPointerException
            at com.facebook.Session$AuthorizationRequest$1.startActivityForResult(Session.java:1902)
            at com.facebook.Session.tryLoginActivity(Session.java:1409)
            at com.facebook.Session.authorize(Session.java:1159)
            at com.facebook.Session.open(Session.java:1237)
            at com.facebook.Session.openForRead(Session.java:471)
            at com.github.gorbin.asne.facebook.FacebookSocialNetwork.requestLogin(FacebookSocialNetwork.java:149)
            at com.github.gorbin.asne.core.SocialNetwork.requestLogin(SocialNetwork.java:205)
            at com.github.gorbin.asnetutorial.MainFragment$1.onClick(MainFragment.java:160)
            at android.view.View.performClick(View.java:4658)
            at android.view.View$PerformClick.run(View.java:19461)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:146)
            at android.app.ActivityThread.main(ActivityThread.java:5653)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
            at dalvik.system.NativeStart.main(Native Method)

For VKontakte will be something like

Caused by: java.lang.NullPointerException: Argument 'currentActivity' cannot be null
            at com.facebook.internal.Validate.notNull(Validate.java:29)
            at com.facebook.Session.onActivityResult(Session.java:723)
            at com.github.gorbin.asne.facebook.FacebookSocialNetwork.onActivityResult(FacebookSocialNetwork.java:535)
            at com.github.gorbin.asne.core.SocialNetworkManager.onActivityResult(SocialNetworkManager.java:92)
            at ru.**MyPackage***.MyLoginActivity.onActivityResult(LoginActivity.java:75)
            at android.app.Activity.dispatchActivityResult(Activity.java:5734)
            at android.app.ActivityThread.deliverResults(ActivityThread.java:3696)
            at android.app.ActivityThread.handleSendResult(ActivityThread.java:3743)
            at android.app.ActivityThread.access$1400(ActivityThread.java:172)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1356)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:146)
            at android.app.ActivityThread.main(ActivityThread.java:5653)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
            at dalvik.system.NativeStart.main(Native Method)

after finishing fragment.

@gorbin gorbin changed the title Application always crashing after changing orientation (rotating device) []Application always crashing after changing orientation (rotating device) Nov 5, 2014
@gorbin gorbin changed the title []Application always crashing after changing orientation (rotating device) [ASNETutorial]Application always crashing after changing orientation (rotating device) Nov 5, 2014
@gorbin
Copy link
Owner

gorbin commented Nov 5, 2014

check it, thanks. Forgot about it in ASNETutorial app.

@tylerjroach
Copy link

This looks to be a bug in ASNE rather than just the tutorial. This same exception also occurs when replacing fragments in an activity. For example, say I have a home activity that can open either a login fragment or a signup fragment. If I open the signup fragment, press back, then go to the login fragment, any requestLogin() call will cause the same null pointer.

If it is any help, I've verified this behavior doesn't occur using the Android Social Networks library ASNE was based off.

@tylerjroach
Copy link

@ITurchenko Here is a workaround that may help for the time being. This worked for me.

Fragment fragment = getSupportFragmentManager().findFragmentByTag(SOCIAL_NETWORK_TAG);
        if(fragment != null) {
            getSupportFragmentManager().beginTransaction().remove(fragment).commit();
            getSupportFragmentManager().executePendingTransactions();
        }

On your orientation change, remove the old asne fragment and create a new one. executePendingTransactions() must be used to make sure the old one is removed before the new created.

Hope this helps.

@tylerjroach
Copy link

I think the issue is the way that the context is passed.

FacebookSocialNetwork fbNetwork = new FacebookSocialNetwork(this, fbScope);
        mSocialNetworkManager.addSocialNetwork(fbNetwork);

On orientation changed or a replaced fragment, the original "this" fragment context that is passed is stale.

@ultraon
Copy link

ultraon commented Mar 17, 2015

Is this bug already fixed?

@ultraon
Copy link

ultraon commented Mar 17, 2015

I found another way to avoid crash and memory leaks (use this code in your fragment):

...
private SocialNetworkManager snm;
...
@Override
    public void onResume() {
        super.onResume();
        if (null == snm || snm.getInitializedSocialNetworks().isEmpty()) initSocialManager();
    }

    @Override
    public void onPause() {
        super.onPause();
        if (isRemoving() || getActivity().isFinishing()/* or check for change rotation event*/) {
            if (null != snm && snm.isAdded()) getFragmentManager().beginTransaction().remove(snm).commit();
            snm = null;
        }
    }
...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants