gpt4 book ai didi

android - 为什么我在 Android 中出现 token null is not valid 错误?

转载 作者:搜寻专家 更新时间:2023-11-01 09:26:25 26 4
gpt4 key购买 nike

我希望能打开一个对话框,代码A能正常工作。为了让代码通用,我把函数promptBuyWhenExpired放到Utility.kt中,然后像代码B一样在onCreate()中调用函数,但是得到如下错误信息,我哪里错了?

错误信息

   java.lang.RuntimeException: Unable to start activity ComponentInfo{info.dodata.mirror/ui.UIMain}: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
at android.view.ViewRootImpl.setView(ViewRootImpl.java:765)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:356)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:92)
at android.app.Dialog.show(Dialog.java:330)
at utility.UtilityKt.promptBuyWhenExpired(Utility.kt:106)
at ui.UIMain.onCreate(UIMain.kt:70)
at android.app.Activity.performCreate(Activity.java:6975)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)

代码A

class UIMain : AppCompatActivity() { 
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.layout_main)

promptBuyWhenExpired()

}

private fun promptBuyWhenExpired() {
if (IsExpired(applicationContext) ) {
val builder = AlertDialog.Builder(this)
builder.setMessage(getString(R.string.ExpiredTitle))
builder.setTitle(getString(R.string.ExpiredWarning))
builder.setCancelable(false)
builder.setPositiveButton(getString(R.string.BtnYes)) { dialog, which ->
finish()
}
builder.create().show()
}
}
}

代码B

class UIMain : AppCompatActivity() { 
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.layout_main)

Utility.promptBuyWhenExpired(this)
}

}




Utility.kt
public fun promptBuyWhenExpired(mActivity: Activity) {
var mContent= mActivity.applicationContext
if (IsExpired(mContent) ) {
val builder = AlertDialog.Builder(mContent)
builder.setMessage(mContent.getString(R.string.ExpiredTitle))
builder.setTitle(mContent.getString(R.string.ExpiredWarning))
builder.setCancelable(false)
builder.setPositiveButton(mContent.getString(R.string.BtnYes)) { dialog, which ->
mActivity.finish()
}
builder.create().show()
}
}

在代码 B 中修改

下面的代码可以运行,不知道是不是所有手机都能正常运行。

//promptBuyWhenExpired(this@UImain) is Ok
//promptBuyWhenExpired(this) is Ok too.
public fun promptBuyWhenExpired(mActivity: Activity) {
var mContent= mActivity.applicationContext
if (IsExpired(mContent) ) {
val builder = AlertDialog.Builder(mActivity) //The paramter is mActivity
builder.setMessage(mContent.getString(R.string.ExpiredTitle))
builder.setTitle(mContent.getString(R.string.ExpiredWarning))
builder.setCancelable(false)
builder.setPositiveButton(mContent.getString(R.string.BtnYes)) { dialog, which ->
mActivity.finish()
}
builder.create().show()
}
}

最佳答案

Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?

阅读This Expression .

此步骤适用于代码 B

 Utility.promptBuyWhenExpired(this@UIMain)

然后

public fun promptBuyWhenExpired(mActivity: Activity) {
if (IsExpired(mActivity) ) {

您应该传递 this@CurrentActivityName 而不是 this

注意

builder.setPositiveButton(mContent.getString(R.string.BtnYes)) { dialog, which ->

val handler = Handler()
handler.postDelayed({ mActivity.finish() }, 300)
}

关于android - 为什么我在 Android 中出现 token null is not valid 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50210539/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com