sagar channe
sagar channe

Reputation: 43

Attempt to invoke virtual method 'void com.facebook.marketing.ViewIndexer.unschedule()' on a null object reference

I am getting this kind of error since I am using Facebook Account Kit I don't know how to deal with it Please help me, guys.

E/AndroidRuntime: FATAL EXCEPTION: main Process: in.co.ashclan.thunder, PID: 29664 java.lang.RuntimeException: Unable to pause activity {in.co.ashclan.thunder/in.co.ashclan.thunder.ActivitySplash}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.facebook.marketing.ViewIndexer.unschedule()' on a null object reference at android.app.ActivityThread.performPauseActivityIfNeeded(ActivityThread.java:3830) at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3796) at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3770) at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3741) at android.app.ActivityThread.-wrap16(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1538) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:163) at android.app.ActivityThread.main(ActivityThread.java:6239) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.facebook.marketing.ViewIndexer.unschedule()' on a null object reference at com.facebook.marketing.CodelessActivityLifecycleTracker$1.onActivityPaused(CodelessActivityLifecycleTracker.java:129) at android.app.Application.dispatchActivityPaused(Application.java:229) at android.app.Activity.onPause(Activity.java:1654) at android.support.v4.app.FragmentActivity.onPause(FragmentActivity.java:441) at android.app.Activity.performPause(Activity.java:7024) at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1323) at android.app.ActivityThread.performPauseActivityIfNeeded(ActivityThread.java:3819) at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3796)  at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3770)  at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3741)  at android.app.ActivityThread.-wrap16(ActivityThread.java)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1538)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:163)  at android.app.ActivityThread.main(ActivityThread.java:6239)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)  V/FA: Activity paused, time: 966233817

Here is my code

public class ParticipantsLogin extends AppCompatActivity implements View.OnClickListener{

FButton btn_facebook,btn_Gmail,btn_submit;
FirebaseDatabase database;
DatabaseReference table_participant ;
FirebaseStorage storage;
Context mContext;
DatabaseReference table_user;

//Get GMail Data from login Account
public String personName,personFirstName,personLastName,personEmail,personId;
Uri personPhoto ;

//Root Layout
LinearLayout linearLayout;

//Participant Pojo
ParticipantModel participantModel;

//FireBase to Facebook
private static final int RC_SIGN_IN = 9001;
private static final String TAG = "GoogleSignIn";

//FireBase to Google
private FirebaseAuth mAuth;
GoogleSignInClient mGoogleSignInClient;
public static final int REQUEST_CODE = 7171;

MaterialEditText edtUserName,edtPassword;
String phone,pass;

//Alert Dialog View
MaterialEditText edtMobileNo;
RadioButton rd_male,rd_female;

//Upload Images
Uri saveuri;
StorageReference storageReference ;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    FacebookSdk.sdkInitialize(getApplicationContext());
    AccountKit.initialize(this);
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_participants_login);
    mInit();
    btn_Gmail.setOnClickListener(this);
    btn_facebook.setOnClickListener(this);
    btn_submit.setOnClickListener(this);

    // Configure Google Sign In
    GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestIdToken(getString(R.string.default_web_client_id))
            .requestEmail()
            .build();

    // Build a GoogleSignInClient with the options specified by gso.
    mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
}

//Initialisation
private void mInit()
{
    mContext = ParticipantsLogin.this;
    btn_facebook = (FButton)findViewById(R.id.btn_facebook);
    btn_Gmail = (FButton)findViewById(R.id.btn_Gmail);

    //InIt FireBase
    database = FirebaseDatabase.getInstance();
    table_participant = database.getReference("Participant"); //Linked to Participant table
    mAuth = FirebaseAuth.getInstance();
    storage = FirebaseStorage.getInstance();
    storageReference = storage.getReference();

    //Init ROOT LAYOUT
    linearLayout = (LinearLayout)findViewById(R.id.root_layout);
    participantModel = new ParticipantModel();

    //Submit Button
    btn_submit = (FButton)findViewById(R.id.btn_submit);
    edtUserName = (MaterialEditText)findViewById(R.id.edt_User_id);
    edtPassword = (MaterialEditText)findViewById(R.id.edt_User_password);
}
//OnclickListner
@Override
public void onClick(View view)
{
    switch (view.getId())
    {
        case R.id.btn_facebook:
            startFacebookLogin();
            break;
        case R.id.btn_Gmail:
            startGmailLogin();
            break;
        case R.id.btn_submit:
                    //Save username and password
                        phone = edtUserName.getText().toString();
                        pass = edtPassword.getText().toString();

                        if(edtUserName.getText().length()==0)
                        {
                            edtUserName.setError("Please Enter Your Mobile No");
                        } else if (edtPassword.getText().length()==0) {
                            edtPassword.setError("Please enter correct password");
                        }else {
                            login(phone, pass);
                        }
               break;
            }
    }
//Methods
private void startFacebookLogin()
{
    Intent intent = new Intent(ParticipantsLogin.this, AccountKitActivity.class);
    AccountKitConfiguration.AccountKitConfigurationBuilder configurationBuilder =
            new AccountKitConfiguration.AccountKitConfigurationBuilder(LoginType.PHONE,
                    AccountKitActivity.ResponseType.TOKEN);
    intent.putExtra(AccountKitActivity.ACCOUNT_KIT_ACTIVITY_CONFIGURATION,configurationBuilder.build());
    startActivityForResult(intent,REQUEST_CODE);
}
private void startGmailLogin()
{
    Intent signInIntent = mGoogleSignInClient.getSignInIntent();
    startActivityForResult(signInIntent, RC_SIGN_IN);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, final Intent data)
{
    super.onActivityResult(requestCode, resultCode, data);
    try{
        //Activity result set for facebook login
        if(requestCode==REQUEST_CODE)
        {
            AccountKitLoginResult result = data.getParcelableExtra(AccountKitLoginResult.RESULT_KEY);
            if(result.getError()!=null)
            {
                Toast.makeText(this, ""+result.getError().getErrorType().getMessage(), Toast.LENGTH_SHORT).show();
                return;
            }else if(result.wasCancelled())
            {
                Toast.makeText(this, "Canceled", Toast.LENGTH_SHORT).show();
                return;
            }
            else
            {
                if(result.getAccessToken() != null)
                {
                    //Show Dialog
                    final AlertDialog watingDialog = new SpotsDialog(this);
                    watingDialog.show();
                    watingDialog.setMessage("Please Wait");
                    watingDialog.setCancelable(false);
                    //get Current Phone
                    AccountKit.getCurrentAccount(new AccountKitCallback<Account>() {
                        @Override
                        public void onSuccess(Account account) {
                            String phone  = account.getPhoneNumber().toString();
                            final String userphone = phone.substring(3);
                            Log.e("fbno-->",userphone);

                            //Check if User Exist on Firebase if not then add it
                            table_participant.orderByKey().equalTo(userphone)
                                    .addListenerForSingleValueEvent(new ValueEventListener() {
                                        @Override
                                        public void onDataChange(DataSnapshot dataSnapshot) {

                                            if(!dataSnapshot.child(userphone).exists())//if User not Exist
                                            {
                                                Intent intent = new Intent(ParticipantsLogin.this,RegistrationActivity.class);
                                                intent.putExtra("mobilno",userphone);
                                                startActivity(intent);
                                                watingDialog.dismiss();
                                                finish();

                                            }else //if User Exist
                                            {
                                                startActivity(new Intent(ParticipantsLogin.this, Activity_DashBoard2.class));
                                            }
                                        }
                                        @Override
                                        public void onCancelled(DatabaseError databaseError) {
                                        }
                                    });
                        }
                        @Override
                        public void onError(AccountKitError accountKitError) {
                            Toast.makeText(ParticipantsLogin.this, ""+accountKitError.getErrorType().getMessage(), Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            }
        }
    }catch (Exception e)
    {
        Log.e("-->FbError",e.toString());
    }


    // 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);
            firebaseAuthWithGoogle(account);
        } catch (ApiException e) {
            // Google Sign In failed, update UI appropriately
            Log.w(TAG, "Google sign in failed", e);
            // ...
        }
    }
}

private void firebaseAuthWithGoogle(GoogleSignInAccount acct)
{
    Log.d(TAG, "firebaseAuthWithGoogle:" + acct.getId());
    //Show Dialog
    final AlertDialog watingDialog = new SpotsDialog(this);
    watingDialog.show();
    watingDialog.setMessage("Please Wait");
    watingDialog.setCancelable(false);

    AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
    mAuth.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
                        Log.d(TAG, "signInWithCredential:success");
                        //showFireBaseDialog();
                        watingDialog.dismiss();
                        //If user is not registred then register it
                        FirebaseUser user = mAuth.getCurrentUser();
                        updateUI(user);
                    } else {
                        // If sign in fails, display a message to the user.
                        Log.w(TAG, "signInWithCredential:failure", task.getException());
                       // Snackbar.make(findViewById(R.id.main_layout), "Authentication Failed.", Snackbar.LENGTH_SHORT).show();
                        updateUI(null);
                    }
                }
            });
}
//User Login
private void login(final String phone, final String pass)
{
    try
    {
        Log.d("-->",phone +" "+pass);

        if (util.isConnectedToInterNet(getBaseContext())) {

            final ProgressDialog mDialog = new ProgressDialog(ParticipantsLogin.this);
            mDialog.setMessage("Please Wait.....");
            mDialog.setCancelable(false);
            mDialog.show();

            table_participant.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    //Check if User doesnt exist in database
                    if (dataSnapshot.child(phone).exists()) {
                        //mDialog.dismiss();
                        //get User Values
                        Log.d("-->123",dataSnapshot.toString());
                        ParticipantModel user = dataSnapshot.child(phone).getValue(ParticipantModel.class);
                            user.setMobile(phone);
                            Log.d("PoJo-->",user.toString());
                            if (user.getFirstname().equals(pass)) {
                                Toast.makeText(getApplicationContext(),"Successful",Toast.LENGTH_LONG).show();

                                startActivity(new Intent(mContext,Activity_DashBoard2.class));
                                PreferenceUtil.setSignIn(mContext,true);
                                PreferenceUtil.setMobileNo(mContext,phone);
                                PreferenceUtil.setPass(mContext,pass);

                            mDialog.dismiss();
                            finish();
                        } else {
                            mDialog.dismiss();
                            Toast.makeText(getApplicationContext(), "Wrong Password", Toast.LENGTH_LONG).show();
                        }
                    } else {
                        mDialog.dismiss();
                        Toast.makeText(getApplicationContext(), "User Doesnt Exist", Toast.LENGTH_LONG).show();
                    }
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });
        } else {
            Toast.makeText(ParticipantsLogin.this, "Please Check Your Internet Connection!", Toast.LENGTH_SHORT).show();
        }
    }catch (Exception e)
    {
        Log.e("-->",e.toString());
        Toast.makeText(mContext, "Something went Wrong!", Toast.LENGTH_SHORT).show();
    }
}
private void updateUI(FirebaseUser user)
{
    if (user != null) {
        // User is signed in
        GoogleSignInAccount acct = GoogleSignIn.getLastSignedInAccount(getApplicationContext());
        if (acct != null) {
            personName = acct.getDisplayName();
            personFirstName = acct.getGivenName();
            personLastName = acct.getFamilyName();
            personEmail = acct.getEmail();
            personId = acct.getId();
            personPhoto = acct.getPhotoUrl();

       /*     Toast.makeText(ParticipantsLogin.this, "" + personName + "\n" +
                    personFirstName + "\n" +
                    personLastName + "\n" +
                    personEmail + "\n", Toast.LENGTH_LONG).show();*/
            PreferenceUtil.setSignIn(ParticipantsLogin.this,true);
        } else {
            // No user is signed in
        }
        //Show Dialog
        android.support.v7.app.AlertDialog.Builder alertDialog = new android.support.v7.app.AlertDialog.Builder(ParticipantsLogin.this);
        alertDialog.setTitle("One More Step... ");
        alertDialog.setMessage("Please Enter Your Registred Mobile");

        LayoutInflater layoutInflater = this.getLayoutInflater();
        View view = layoutInflater.inflate(R.layout.custom_mobileno_dialog, null);

        edtMobileNo = (MaterialEditText) view.findViewById(R.id.edt_gm_MobileNo);

        alertDialog.setView(view);
        alertDialog.setIcon(R.drawable.ic_person);

        //Set Buttons
        alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                final String phone = edtMobileNo.getText().toString();
                final AlertDialog watingDialog = new SpotsDialog(ParticipantsLogin.this);
                watingDialog.show();
                watingDialog.setMessage("Please Wait");
                watingDialog.setCancelable(false);

                //Check if User Exist on Firebase if not then add it
                table_participant.orderByKey().equalTo(phone)
                        .addListenerForSingleValueEvent(new ValueEventListener() {
                            @Override
                            public void onDataChange(DataSnapshot dataSnapshot) {
                                if (!dataSnapshot.child(phone).exists())//if User not Exist
                                {
                                    Intent intent = new Intent(ParticipantsLogin.this, RegistrationActivity.class);
                                    intent.putExtra("email", personEmail);
                                    intent.putExtra("firstname", personFirstName);
                                    intent.putExtra("lastname", personLastName);
                                    intent.putExtra("mobilno", phone);

                                    PreferenceUtil.setMobileNo(mContext,phone);
                                    PreferenceUtil.setEmailid(mContext,personEmail);
                                    PreferenceUtil.setLastname(mContext,personLastName);
                                    PreferenceUtil.setFirstname(mContext,personFirstName);
                                    PreferenceUtil.setPass(mContext,personFirstName);

                                    startActivity(intent);
                                    watingDialog.dismiss();
                                    finish();

                                } else//if User Exist
                                {
                                    table_participant.child(phone)
                                            .addListenerForSingleValueEvent(new ValueEventListener() {
                                                @Override
                                                public void onDataChange(DataSnapshot dataSnapshot) {
                                                    ParticipantModel localUser = dataSnapshot.getValue(ParticipantModel.class);
                                                    startActivity(new Intent(ParticipantsLogin.this, Activity_DashBoard2.class));
                                                    util.currentParticipant = localUser;

                                                    PreferenceUtil.setMobileNo(mContext,phone);
                                                    PreferenceUtil.setEmailid(mContext,personEmail);
                                                    PreferenceUtil.setLastname(mContext,personLastName);
                                                    PreferenceUtil.setFirstname(mContext,personFirstName);
                                                    PreferenceUtil.setPass(mContext,personFirstName);

                                                    watingDialog.dismiss();
                                                    finish();
                                                }

                                                @Override
                                                public void onCancelled(DatabaseError databaseError) {
                                                }
                                            });
                                }
                            }
                            @Override
                            public void onCancelled(DatabaseError databaseError) {
                            }
                        });
            }
        });
        alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        alertDialog.show();
    }
}
//Check User VAlidataions
public boolean utilsCheck()
{
    boolean cancel = false;
    View focusView = null;
    if (TextUtils.isEmpty(edtUserName.getText())){
        edtUserName.setError("Please enter first Name");
        focusView=edtUserName;
        cancel=true;
    }
    if (TextUtils.isEmpty(edtPassword.getText())){
        edtPassword.setError("Please enter Password");
        focusView=edtPassword;
        cancel=true;
    }
    return cancel;
}

}

public class SplashScreen extends AppCompatActivity {

Context mcontext;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash_screen);

   try {
       if (PreferenceUtil.getSignIn(this)) {
           final Handler handler = new Handler();
           handler.postDelayed(new Runnable() {
               @Override
               public void run() {
                   Intent intent = new Intent(SplashScreen.this, Activity_DashBoard2.class);
                   startActivity(intent);
                   finish();

               }
           }, 7000);
       } else {
           final Handler handler = new Handler();
           handler.postDelayed(new Runnable() {
               @Override
               public void run() {
                   Intent intent = new Intent(SplashScreen.this, ParticipantsLogin.class);
                   startActivity(intent);
                   finish();
               }
           }, 7000);
       }

   }catch (Exception e)
   {
       Log.e("-->splashexp",e.toString());
   }
}

private void login(final String phone, final String pass) {

    FirebaseDatabase database = FirebaseDatabase.getInstance();
    final DatabaseReference table_user = database.getReference("Participant");

    Log.d("-->",phone +" "+pass);

    if (util.isConnectedToInterNet(getBaseContext())) {

        final ProgressDialog mDialog = new ProgressDialog(mcontext);
        mDialog.setMessage("Please Wait.....");
        mDialog.show();

        table_user.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                //Check if User doesnt exist in database
                if (dataSnapshot.child(phone).exists()) {
                    //mDialog.dismiss();
                    //get User Values
                    Log.d("-->123",dataSnapshot.toString());
                    ParticipantModel user = dataSnapshot.child(phone).getValue(ParticipantModel.class);
                    user.setMobile(phone);
                    Log.d("PoJo-->",user.toString());
                    if (user.getFirstname().equals(pass)) {
                        //Toast.makeText(getApplicationContext(),"Successful",Toast.LENGTH_LONG).show();
                        startActivity(new Intent(mcontext, DashBoard.class));
                        util.currentParticipant = user;
                        finish();
                    } else {
                        mDialog.dismiss();
                        Toast.makeText(getApplicationContext(), "Wrong Password", Toast.LENGTH_LONG).show();
                    }
                } else {
                    mDialog.dismiss();
                    Toast.makeText(getApplicationContext(), "User Doesnt Exist", Toast.LENGTH_LONG).show();
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
    } else {
        Toast.makeText(mcontext, "Please Check Your Internet Connection!", Toast.LENGTH_SHORT).show();
    }

}

@Override
public void onBackPressed() {
    super.onBackPressed();
    finish();
}

}

Upvotes: 4

Views: 2178

Answers (3)

Upgrading facebook-android-sdk did the trick for me:

implementation com.facebook.android:facebook-android:5.1.1

Also I discourage people from using wildcard format such as

implementation com.facebook.android:facebook-android-sdk:[4:5)

despite facebook recommending it on their website, since it can lead to the unpredictable builds if the latest version of the library changes and breaks something.

Upvotes: 3

Ho&#224;ng Đ&#244;ng
Ho&#224;ng Đ&#244;ng

Reputation: 27

You must change marketing in fbsdk.

implementation com.facebook.android:facebook-android-sdk:4.34.0

change to: implementation com.facebook.android:facebook-android-sdk:[4:5)

It will auto change com.facebook.marketing.

Upvotes: 1

Abhishek Tiwari
Abhishek Tiwari

Reputation: 304

I'd encountered the same problem on Splash,I was using latest fb sdk i.e.

implementation 'com.facebook.android:facebook-android-sdk:4.34.0'

I changed it to

implementation 'com.facebook.android:facebook-login:[4,5)'

This solved the problem in my case.

Upvotes: 1

Related Questions