I write an application in Kotlin language on the platform Firebase. And after authentication, I need to open another activity from Interface.
While checking I get the error in this line:
startActivity(Intent(applicationContext, AnotherActivity::class.java))
Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
My code:
Interface:
interface OnResultSuccessListener {
fun onResultSuccess(isSuccess: Boolean)
}
class an Authentication:
class AuthenticationActivity : AppCompatActivity(), OnResultSuccessListener {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_authentication)
...
}
override fun onResultSuccess(isSuccess: Boolean) {
if (isSuccess) {
startActivity(Intent(applicationContext, AnotherActivity::class.java)) /*ERROR IS HERE*/
overridePendingTransition(R.anim.fade_in, R.anim.fade_out)
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
// Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
if (requestCode == RC_SIGN_IN) {
val task = GoogleSignIn.getSignedInAccountFromIntent(data)
try {
// Google Sign In was successful, authenticate with Firebase
val account = task.getResult(ApiException::class.java)
firebaseAuthWithGoogle(account)
} catch (e: ApiException) {
}
}
}
}
Helper class with CRUD methods Firebase:
class FirestoreCRUD {
val db = Firebase.firestore
var geoPoint: GeoPoint? = null
private val authenticationActivity = AuthenticationActivity()
val COLLECTION_USERS = "Users"
...
...
//add new user to Firestore
fun addNewUserToFirestore(firebaseUser: FirebaseUser?) {
FirebaseInstanceId.getInstance().instanceId
.addOnCompleteListener(OnCompleteListener { task ->
if (task.isSuccessful) {
...
...
// Add a new document with a generated ID
db.collection(COLLECTION_USERS)
.document(firebaseUser.uid)
.set(userModel)
.addOnSuccessListener { documentReference ->
/*CALL onResultSuccess IN AuthenticationActivity */
authenticationActivity.onResultSuccess(true)
}
.addOnFailureListener { e ->
Log.d(LOG_TAG, "Error adding document", e)
authenticationActivity.onResultSuccess(false)
}
}
else authenticationActivity.onResultSuccess(false)
})
}
}
Call onActivityResult failed from onResultSuccess.
I will happy for any help!
Thanks a lop!
So, I resolved the problem. It was easy.
How said Tenfour04:
The only way the context of an Activity can be null is if you are passing around an instance of your Activity that you instantiated yourself, which you should never do...
It is true.
Solution:
should send context to FirestoreCRUD class:
FirestoreUtil(this).addNewUserToFirestore(user)
FirestoreCRUD class receivers straight to Interface:
class FirestoreUtil(var onResultSuccessListener: OnResultSuccessListener) {}
And call the Interface method in AuthenticationActivity class from FirestoreCRUD class:
onResultSuccessListener!!.onResultSuccess(true)
And then my activity doesn't restart and everything runs smoothly!
I hope this helps somebody.