为什么我在Android中获得令牌null无效错误?

时间:2018-05-07 09:03:29

标签: android kotlin

我希望打开一个对话框,Code A效果很好。为了使代码普遍存在,我将函数promptBuyWhenExpired放入Utility.kt,然后我在onCreate()中调用函数,就像代码B一样,但是我得到以下错误信息,我做了什么错?

错误信息

   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()
    }
}

1 个答案:

答案 0 :(得分:2)

  

引起:android.view.WindowManager $ BadTokenException:无法执行   添加窗口 - 令牌null无效;你的活动在运行吗?

阅读This Expression

DO

代码B

的此步骤
composer global require codeception/codeception

然后

 Utility.promptBuyWhenExpired(this@UIMain)

您应该传递 public fun promptBuyWhenExpired(mActivity: Activity) { if (IsExpired(mActivity) ) { ,而不是 this@CurrentActivityName

注意

this