使用MVP登录时获取TransactionTooLargeException

时间:2018-10-12 14:32:49

标签: java google-signin android-mvp presenter transactiontoolargeexception

我在我的应用中使用了MVP,但出现错误

Error reporting crash
    android.os.TransactionTooLargeException: data parcel size 1052448 bytes

我知道这个问题曾经问过,但是其他解决方案对我不起作用。我的活动是使用Google登录按钮将用户登录到Firebase

这是演示者代码:

View view;
    private final static int RC_SIGN_IN = 2;

    //TODO : try to find a way to edit this use
    FirebaseUser currentUser;

    GoogleApiClient  mGoogleApiClient;

    public GeneralSignActivityPresenter(View view){
        this.view = view;
    }

    public void signInWithGoogle(String requestIdToken) {

        GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestIdToken(requestIdToken)
                .requestEmail()
                .build();
        if(mGoogleApiClient == null || !mGoogleApiClient.isConnected()){
             mGoogleApiClient = new GoogleApiClient.Builder((Context) view)//TODO : check this context, TODO : check why this variable isn't used
                .enableAutoManage((FragmentActivity) view, new GoogleApiClient.OnConnectionFailedListener() {//TODO : check this casting
                    @Override
                    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
                        view.onGoogleConnectionFailed();
                        Log.v("Logging", "connection result is : " + connectionResult.toString());
                    }
                })
                .addApi(Auth.GOOGLE_SIGN_IN_API)
                .build();
        }
        GoogleSignInClient mGoogleSignInClient = GoogleSignIn.getClient((Context) view, gso);//TODO : check this context

        Intent signInIntent = mGoogleSignInClient.getSignInIntent();

        view.startSignIntent(signInIntent);
    }

    public void navigate(final String userType) {
        currentUser = FirebaseAuth.getInstance().getCurrentUser();
        String currentUserUid = currentUser.getUid();
        final DatabaseReference usersReference = FirebaseDatabase.getInstance().getReference("users");
        final DatabaseReference currentUserReference =  usersReference.child(currentUserUid);
        usersReference.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                if (!dataSnapshot.child(currentUser.getUid()).exists()) {
                    Map<String, Object> map = new HashMap<>();
                    map.put("userName", currentUser.getDisplayName());
                    map.put("userImage", currentUser.getPhotoUrl());
                    map.put("userEmail", currentUser.getEmail());
                    map.put("points", 0);
                    map.put("acceptedQuestions", 0);
                    map.put("refusedQuestions", 0);
                    map.put("acceptedLessons", 0);
                    map.put("refusedLessons", 0);
                    map.put("userType", userType);
                    usersReference.child(currentUser.getUid()).setValue(map);
                }
                else {
                    //TODO : check if this part is working or no
                    currentUserReference.child("userName").setValue(currentUser.getDisplayName());
                    currentUserReference.child("userImage").setValue(currentUser.getPhotoUrl());
                    currentUserReference.child("userType").setValue(userType);
                }
                view.goToMainActivity();
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
    }

    public void checkCurrentUser(){
        if (currentUser != null) {
           view.goToMainActivity();
        }
    }

    public interface View {
        void onGoogleConnectionFailed();

这是活动代码:

  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_general_sign);
        ButterKnife.bind(this);
        FirebaseApp.initializeApp(this);
        presenter = new GeneralSignActivityPresenter(this);
        button = findViewById(R.id.googleBtn);
        userTypesSpinner = findViewById(R.id.userTypesSpinner);
        unStudentSignAlertText = findViewById(R.id.unStudentSignAlertText);


        ArrayAdapter<CharSequence> userTypesAdapter = ArrayAdapter.createFromResource(this,
                R.array.user_types_array, android.R.layout.simple_spinner_item);
        userTypesAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        userTypesSpinner.setAdapter(userTypesAdapter);

        userTypesSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                userType = adapterView.getItemAtPosition(i).toString();
                if (userType.equals("طالب")) {
                    unStudentSignAlertText.setVisibility(View.GONE);
                } else {
                    unStudentSignAlertText.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });

       /* mCallbackManager = CallbackManager.Factory.create();
        LoginButton loginButton = findViewById(R.id.facebookBtn);
        loginButton.setReadPermissions("email", "public_profile");
        loginButton.registerCallback(mCallbackManager, new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {
                Log.d(TAG, "facebook:onSuccess:" + loginResult);
                Toast.makeText(GeneralSignActivity.this, "onSuccess" + loginResult, Toast.LENGTH_SHORT).show();
                handleFacebookAccessToken(loginResult.getAccessToken());
            }

            @Override
            public void onCancel() {
                Log.d(TAG, "facebook:onCancel");
                Toast.makeText(GeneralSignActivity.this, "onCancel" , Toast.LENGTH_SHORT).show();

                // ...
            }

            @Override
            public void onError(FacebookException error) {
                Log.d(TAG, "facebook:onError", error);
                Toast.makeText(GeneralSignActivity.this, "onError : " + error, Toast.LENGTH_SHORT).show();

                // ...
            }
        });*/

// ...


        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                presenter.signInWithGoogle(getString(R.string.default_web_client_id));
            }
        });

        mCallbackManager = CallbackManager.Factory.create();
    }

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

        // Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
        if (requestCode == RC_SIGN_IN) {
            Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
            try {
                // Google Sign In was successful, authenticate with Firebase
                GoogleSignInAccount account = task.getResult(ApiException.class);
                AuthCredential credential = GoogleAuthProvider.getCredential(account.getIdToken(), null);
                auth.signInWithCredential(credential)
                        .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                            @Override
                            public void onComplete(@NonNull Task<AuthResult> task) {
                                if (task.isSuccessful()) {
                                    // Sign in success, update UI with the signed-in user's information
                                    presenter.navigate(userType);
                                } else {
                                    // If sign in fails, display a message to the user.
                                    Toast.makeText(GeneralSignActivity.this, "فشل التسجيل في التطبيق قد يكون لديك مشكلة في الإتصال بالانترنت أو أن إدارة البرنامج قامت بإلغاء تفعيل حسابك", Toast.LENGTH_SHORT).show();
                                    //updateUI(null);
                                }

                                // ...
                            }
                        });

                //Toast.makeText(this, "نجح تسجيل الدخول",Toast.LENGTH_SHORT).show();
            } catch (ApiException e) {
                Toast.makeText(GeneralSignActivity.this, "حدث خطأ أثناء محاولة التسجيل برجاء اعادة المحاولة", Toast.LENGTH_SHORT).show();

            }
        }//TODO
           // mCallbackManager.onActivityResult(requestCode, resultCode, data);

    }

    @Override
    public void onStart() {
        super.onStart();
        //TODO : note : don't try to update the users data here again
        presenter.checkCurrentUser();
    }

    @Override
    public void onGoogleConnectionFailed() {
        Toast.makeText(GeneralSignActivity.this, "حدث خطأ برجاء إعادة المحاولة", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void goToMainActivity() {
        startActivity(new Intent(GeneralSignActivity.this, MainActivity.class));
    }

    @Override
    public void startSignIntent(Intent signInIntent) {
        startActivityForResult(signInIntent, RC_SIGN_IN);
    }

            void goToMainActivity();

            void startSignIntent(Intent signInIntent);
        }

我为解决该错误做了很多尝试,但是我无法在两个类之间发送大数据,所以我无法弄清问题所在。

0 个答案:

没有答案