0

実際には、アクティビティ(LoginActivity)に接続されたフラグメント(LoginFragment)からFacebookとGoogle plusを介してサインインしようとしています。サインインをクリックすると、別のアクティビティ(HomeActivity)に移動します。Facebookサインインは正常に機能していますが、 Googleプラスサインインで、次のような例外が発生しています:-

         Caused by: java.lang.NullPointerException
                at archerpenny.valmoref.LoginFragment.onActivityResult(LoginFragment.java:264)
                at archerpenny.valmoref.LogInActivity.onActivityResult(LogInActivity.java:32)
                at android.app.Activity.dispatchActivityResult(Activity.java:5372)
                at android.app.ActivityThread.deliverResults(ActivityThread.java:3215)
                at android.app.ActivityThread.handleSendResult(ActivityThread.java:3262)
                at android.app.ActivityThread.access$1200(ActivityThread.java:140)
                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1286)
                at android.os.Handler.dispatchMessage(Handler.java:99)
                at android.os.Looper.loop(Looper.java:177)
                at android.app.ActivityThread.main(ActivityThread.java:4944)
                at java.lang.reflect.Method.invokeNative(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:511)
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
                at dalvik.system.NativeStart.main(Native Method)

I think there is some problem with my OnActivityResult(),but I dont know how to solve it.

Here is my LoginFragment.java--


public class LoginFragment extends Fragment implements View.OnClickListener,GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
    TextView Fpassword, Signup;
    EditText email, password;
    ImageView Signin, fb_button, gplus_button;
    LogInActivity myContext;
    static String personName;
    private boolean mIntentInProgress;
    FragmentManager fragmentManager;
    private CallbackManager callbackmanager;
    //for G+
    private static final int PROFILE_PIC_SIZE = 30;
    private GoogleApiClient mGoogleApiClient;
    private ConnectionResult mConnectionResult;
    private boolean mSignInClicked;
    static final int RC_SIGN_IN = 0;
    /* Is there a ConnectionResult resolution in progress? */
    private boolean mIsResolving = false;

    /* Should we automatically resolve ConnectionResults when possible? */
    private boolean mShouldResolve = false;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        myContext = (LogInActivity) activity;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_login, container, false);
        Fpassword = (TextView) view.findViewById(R.id.ForgotPassword);
        Signup = (TextView) view.findViewById(R.id.SignUp);
        email = (EditText) view.findViewById(R.id.Email_Val);
        password = (EditText) view.findViewById(R.id.PasswordVal);
        Signin = (ImageView) view.findViewById(R.id.signin);
        fb_button = (ImageView) view.findViewById(R.id.FB_btn);

        gplus_button = (ImageView) view.findViewById(R.id.Gplus_btn);
        FacebookSdk.sdkInitialize(getActivity().getApplicationContext());
        Fpassword.setPaintFlags(Fpassword.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
        Signup.setPaintFlags(Signup.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
        email.setHintTextColor(Color.parseColor("#727272"));
        password.setHintTextColor(Color.parseColor("#727272"));
        Fpassword.setOnClickListener(this);
        Signup.setOnClickListener(this);
        Signin.setOnClickListener(this);
        fb_button.setOnClickListener(this);
        gplus_button.setOnClickListener(this);
        return view;

    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.ForgotPassword) {
            Fragment forgotpassword = new ForgotPasswordFragment();
            myContext.getSupportFragmentManager().beginTransaction().add(R.id.container_login, forgotpassword).addToBackStack("fragBack1").commit();

        } else if (v.getId() == R.id.SignUp) {
            Fragment signup = new SignUpFragment();
            myContext.getSupportFragmentManager().beginTransaction().add(R.id.container_login, signup).addToBackStack("fragBack1").commit();

        } else if (v.getId() == R.id.signin) {

            Intent intent_signin = new Intent(myContext, HomeActivity.class);
            startActivity(intent_signin);


        } else if (v.getId() == R.id.FB_btn) {

            //Intent intent_signin = new Intent(myContext, HomeActivity.class);
           // startActivity(intent_signin);
            Fblogin();

        } else if (v.getId() == R.id.Gplus_btn)
        {
            mGoogleApiClient = new GoogleApiClient.Builder(getActivity()).addConnectionCallbacks(this).addOnConnectionFailedListener(this).addApi(Plus.API).addScope(new Scope(Scopes.PROFILE)).build();

            //Intent intent_signin = new Intent(myContext, HomeActivity.class);
            // startActivity(intent_signin);
            onSignInClicked();


        }

    }

    private void onSignInClicked() {
        // User clicked the sign-in button, so begin the sign-in process and automatically
        // attempt to resolve any errors that occur.

        mShouldResolve = true;
        mGoogleApiClient.connect();


    }

    @Override
    public void onStart() {
        super.onStart();

    }

    @Override
    public void onStop() {
        super.onStop();

    }
    private void Fblogin()
    {
        callbackmanager = CallbackManager.Factory.create();

        // Set permissions
        LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("public_profile, email, user_birthday,user_friends"));

        LoginManager.getInstance().registerCallback(callbackmanager, new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {
                // App code
                GraphRequest request = GraphRequest.newMeRequest(
                        loginResult.getAccessToken(),
                        new GraphRequest.GraphJSONObjectCallback() {
                            @Override
                            public void onCompleted(JSONObject object, GraphResponse response) {
                                // Application code
                                Log.d("LoginActivity", response.toString());
                                Log.d("LoginActivity", object.toString());
                                String jsonresult = String.valueOf(object);
                                System.out.println("JSON Result" + jsonresult);
                                String str_firstname=null,str_id=null;


                                try {
                                    str_firstname=object.getString("name");
                                    str_id = object.getString("id");
                                    String str_email = object.getString("email");
                                    Intent login = new Intent(myContext, HomeActivity.class);
                                    login.putExtra("name", str_firstname);
                                    login.putExtra("URL", "https://graph.facebook.com/" + str_id + "/picture?width="+PROFILE_PIC_SIZE+"&height="+PROFILE_PIC_SIZE);
                                    startActivity(login);


                                } catch (JSONException e) {
                                    e.printStackTrace();
                                    Log.d("animesh123","aa");
                                }



                            }
                        });
                Bundle parameters = new Bundle();
                parameters.putString("fields", "id,name,email,gender, birthday");
                request.setParameters(parameters);
                request.executeAsync();


            }

            @Override
            public void onCancel() {
                // App code
                Log.v("LoginActivity", "cancel");
            }

            @Override
            public void onError(FacebookException exception) {
                // App code
                Log.v("LoginActivity", exception.getCause().toString());
            }
        });
    }



    @Override
    public void onConnected(Bundle bundle) {
        mSignInClicked = false;
        Toast.makeText(myContext, "User is connected!", Toast.LENGTH_LONG).show();
        if (Plus.PeopleApi.getCurrentPerson(mGoogleApiClient) != null) {
            Person currentPerson = Plus.PeopleApi
                    .getCurrentPerson(mGoogleApiClient);
            personName = currentPerson.getDisplayName();
            String birthDate=currentPerson.getAboutMe();
            String personPhotoUrl = currentPerson.getImage().getUrl();
            String personGooglePlusProfile = currentPerson.getUrl();
            String email = Plus.AccountApi.getAccountName(mGoogleApiClient);

            Log.d("sachin", "Name: " + personName + " " + currentPerson.getName() + ", plusProfile: "
                    + personGooglePlusProfile + ", email: " + email
                    + ",BirthDay:" + birthDate + ", Image: " + personPhotoUrl);
            personPhotoUrl = personPhotoUrl.substring(0,
                    personPhotoUrl.length() - 2)
                    + PROFILE_PIC_SIZE;

            Intent login=new Intent(myContext,HomeActivity.class);
            login.putExtra("name",personName);
            login.putExtra("URL", personPhotoUrl);
            startActivity(login);

        }


    }

    @Override
    public void onConnectionSuspended(int i) {
        mGoogleApiClient.connect();
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        callbackmanager.onActivityResult(requestCode,resultCode,data);
        Log.d("ani", "onActivityResult:" + requestCode + ":" + resultCode + ":" + data);

        if (requestCode == RC_SIGN_IN) {
            // If the error resolution was not successful we should not resolve further.
            if (resultCode != getActivity().RESULT_OK) {
                mShouldResolve = false;
            }

            mIsResolving = false;
            mGoogleApiClient.connect();
        }
    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult)
    {

        // Could not connect to Google Play Services.  The user needs to select an account,
        // grant permissions or resolve an error in order to sign in. Refer to the javadoc for
        // ConnectionResult to see possible error codes.
        Log.d("ani", "onConnectionFailed:" + connectionResult);

        if (!mIsResolving && mShouldResolve) {
            if (connectionResult.hasResolution()) {
                try {
                    connectionResult.startResolutionForResult(getActivity(), RC_SIGN_IN);
                    mIsResolving = true;
                } catch (IntentSender.SendIntentException e) {
                    Log.e("ani", "Could not resolve ConnectionResult.", e);
                    mIsResolving = false;
                    mGoogleApiClient.connect();
                }
            }
        }

    }
}





Here is My LoginActivity:--

    public class LogInActivity extends AppCompatActivity
{
    FragmentManager fragmentManager;
    Fragment blankFragment=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_log_in);
        blankFragment=new LoginFragment();
        fragmentManager=getSupportFragmentManager();
        fragmentManager.beginTransaction().add(R.id.container_login, blankFragment).addToBackStack("fragBack1").commit();

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == LoginFragment.RC_SIGN_IN) {
            LoginFragment fragment = (LoginFragment) getSupportFragmentManager()
                    .findFragmentById(R.id.container_login);
            fragment.onActivityResult(requestCode, resultCode, data);
        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }
}
4

1 に答える 1