DialogListener 的 startActivity 导致应用程序崩溃

startActivity from DialogListener cause app crashing

我在我的应用程序中使用 socialauth-android,并且我有用于使用 linkedin 登录的按钮。

这是我的代码片段:

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 成功登录时,应用程序崩溃而不是启动新的 activity。

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.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)

这是错误消息:

发生此异常的原因是 ResponseListener callback(onComplete) 在您的 dialogFragment dismiss 之后调用。因此,在 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();
                }   
      }

}