如何从接口方法打开另一个 activity
How to open another activity from Interface method
我在 Firebase 平台上用 Kotlin 语言编写了一个应用程序。身份验证后,我需要从界面打开另一个 activity。
在检查时我在这一行中得到错误:
startActivity(Intent(applicationContext, AnotherActivity::class.java))
Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
我的代码:
接口:
interface OnResultSuccessListener {
fun onResultSuccess(isSuccess: Boolean)
}
class 身份验证:
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 使用 CRUD 方法 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)
})
}
}
从 onResultSuccess 调用 onActivityResult 失败。
我很乐意提供任何帮助!
谢谢你!
Activity 的上下文可以为 null 的唯一方法是,如果您正在传递您自己实例化的 Activity 的实例,您永远不应该这样做。必须查看更多您的代码才能知道您在何处执行此操作。
所以,我解决了这个问题。很简单。
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...
这是真的。
解决方案:
应该发送 context 到 FirestoreCRUD class:
FirestoreUtil(this).addNewUserToFirestore(user)
FirestoreCRUD class 直接接收接口:
class FirestoreUtil(var onResultSuccessListener: OnResultSuccessListener) {}
并从 FirestoreCRUD class:
调用 AuthenticationActivity class 中的接口方法
onResultSuccessListener!!.onResultSuccess(true)
然后我的 activity 没有重新启动,一切运行顺利!
我希望这对某人有所帮助。
我在 Firebase 平台上用 Kotlin 语言编写了一个应用程序。身份验证后,我需要从界面打开另一个 activity。
在检查时我在这一行中得到错误:
startActivity(Intent(applicationContext, AnotherActivity::class.java))
Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
我的代码:
接口:
interface OnResultSuccessListener {
fun onResultSuccess(isSuccess: Boolean)
}
class 身份验证:
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 使用 CRUD 方法 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)
})
}
}
从 onResultSuccess 调用 onActivityResult 失败。
我很乐意提供任何帮助!
谢谢你!
Activity 的上下文可以为 null 的唯一方法是,如果您正在传递您自己实例化的 Activity 的实例,您永远不应该这样做。必须查看更多您的代码才能知道您在何处执行此操作。
所以,我解决了这个问题。很简单。
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...
这是真的。
解决方案:
应该发送 context 到 FirestoreCRUD class:
FirestoreUtil(this).addNewUserToFirestore(user)
FirestoreCRUD class 直接接收接口:
class FirestoreUtil(var onResultSuccessListener: OnResultSuccessListener) {}
并从 FirestoreCRUD class:
调用 AuthenticationActivity class 中的接口方法onResultSuccessListener!!.onResultSuccess(true)
然后我的 activity 没有重新启动,一切运行顺利!
我希望这对某人有所帮助。