来自DialogListener的startActivity导致应用程序崩溃

时间:2015-01-20 21:28:13

标签: java android android-intent android-fragments listener

我在我的应用中使用socialauth-android,并且我已经通过linkedin登录了按钮。

这是我的代码snipet:

public class SigninDialog extends android.app.DialogFragment{

    public SigninDialog () {}

    SocialAuthAdapter adapter;
    Profile profileMap;


    @Override
    public View onCreateView(LayoutInflater inflater,
            @Nullable ViewGroup container,
            @Nullable Bundle savedInstanceState) {


         View view = inflater.inflate(R.layout.dialog_signin, container, false);
         getDialog().setTitle("Sign In");

         //Login via Linkedin
          adapter = new SocialAuthAdapter(new ResponseListener());
         ((Button) view.findViewById(R.id.Signin_linkedinBtn)).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                getDialog().dismiss();
                adapter.authorize(getActivity(), Provider.LINKEDIN);
            }
        });

        return view;
    }

        //Listener For the Linkedin Connection 
        private final class ResponseListener implements DialogListener {

            //If login with linkedin success then execute this void
            @Override
            public void onComplete(Bundle values) {           
                profileMap =  adapter.getUserProfile(); 
                String email=profileMap.getEmail(); 

                //Error Lines
                Intent i = new Intent(getActivity(),CallActivity.class);
                startActivity(i); // The error is raised here

                }   
      }

}

问题是当使用linkedin登录成功时,应用程序崩溃而不是启动新活动。

01-20 22:10:44.184: E/AndroidRuntime(5759): FATAL EXCEPTION: main
01-20 22:10:44.184: E/AndroidRuntime(5759): Process: com.example.kontactapp, PID: 5759
01-20 22:10:44.184: E/AndroidRuntime(5759): java.lang.IllegalStateException: Fragment SigninDialog{4b0a067c} not attached to Activity
01-20 22:10:44.184: E/AndroidRuntime(5759):     at android.app.Fragment.startActivity(Fragment.java:1047)
01-20 22:10:44.184: E/AndroidRuntime(5759):     at android.app.Fragment.startActivity(Fragment.java:1033)
01-20 22:10:44.184: E/AndroidRuntime(5759):     at com.example.kontactapp.SigninDialog$ResponseListener.onComplete(SigninDialog.java:173)
01-20 22:10:44.184: E/AndroidRuntime(5759):     at org.brickred.socialauth.android.SocialAuthAdapter$5$1.run(SocialAuthAdapter.java:731)
01-20 22:10:44.184: E/AndroidRuntime(5759):     at android.os.Handler.handleCallback(Handler.java:733)
01-20 22:10:44.184: E/AndroidRuntime(5759):     at android.os.Handler.dispatchMessage(Handler.java:95)
01-20 22:10:44.184: E/AndroidRuntime(5759):     at android.os.Looper.loop(Looper.java:136)
01-20 22:10:44.184: E/AndroidRuntime(5759):     at android.app.ActivityThread.main(ActivityThread.java:5021)
01-20 22:10:44.184: E/AndroidRuntime(5759):     at java.lang.reflect.Method.invokeNative(Native Method)
01-20 22:10:44.184: E/AndroidRuntime(5759):     at java.lang.reflect.Method.invoke(Method.java:515)
01-20 22:10:44.184: E/AndroidRuntime(5759):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827)
01-20 22:10:44.184: E/AndroidRuntime(5759):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643)
01-20 22:10:44.184: E/AndroidRuntime(5759):     at dalvik.system.NativeStart.main(Native Method)

这是错误信息:

1 个答案:

答案 0 :(得分:1)

由于ResponseListener回调(onComplete)而发生此异常是在dialogFragment关闭后调用的。因此,在startActivity()调用之后,在onComplete方法中关闭对话框片段。

public class SigninDialog extends android.app.DialogFragment{

    public SigninDialog () {}

    SocialAuthAdapter adapter;
    Profile profileMap;


    @Override
    public View onCreateView(LayoutInflater inflater,
            @Nullable ViewGroup container,
            @Nullable Bundle savedInstanceState) {


         View view = inflater.inflate(R.layout.dialog_signin, container, false);
         getDialog().setTitle("Sign In");

         //Login via Linkedin
          adapter = new SocialAuthAdapter(new ResponseListener());
         ((Button) view.findViewById(R.id.Signin_linkedinBtn)).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                adapter.authorize(getActivity(), Provider.LINKEDIN);
            }
        });

        return view;
    }

        //Listener For the Linkedin Connection 
        private final class ResponseListener implements DialogListener {

            //If login with linkedin success then execute this void
            @Override
            public void onComplete(Bundle values) {           
                profileMap =  adapter.getUserProfile(); 
                String email=profileMap.getEmail(); 

                //Error Lines
                Intent i = new Intent(getActivity(),CallActivity.class);
                startActivity(i); // The error is raised here
                 getDialog().dismiss();
                }   
      }

}