gpt4 book ai didi

java - 带有 TextView 的静态对话框

转载 作者:行者123 更新时间:2023-12-02 04:38:20 25 4
gpt4 key购买 nike

这有什么问题,因为我无法编译为 apk。错误是无法在静态上下文中使用它,但如果我将其更改为上下文应用程序卡住时必须出现对话框。

public static void mydialog(final Context context) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
TextView myMsg = new TextView(this);
myMsg.setText("Central");
}

Logcat 错误:

 android.view.WindowLeaked: Activity com.test.testapp.activities.MenuActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{430fc9a8 V.E..... R.....I. 0,0-1048,466} that was originally added here
at android.view.ViewRootImpl.<init>(ViewRootImpl.java:457)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:267)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
at android.app.Dialog.show(Dialog.java:288)
at com.dialog.dialog.showDialog(RateThisApp.java:229)
at com.dialog.dialog.showDialogIfNeeded(RateThisApp.java:108)
at com.test.testapp.activities.MenuActivity.onCreate(MenuActivity.java:47)
at android.app.Activity.performCreate(Activity.java:5426)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
at android.app.ActivityThread.access$900(ActivityThread.java:161)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5356)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)

最佳答案

静态方法不与类的任何特定实例关联。它是与类本身关联的方法。因此 this 在静态方法中没有任何意义 - 没有要引用的类的实例。

如果此方法位于 Context 类中(例如 Application 子类或 Activity),那么您可以删除static 修饰符并使用 this 就像您已经尝试做的那样。

关于java - 带有 TextView 的静态对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30484545/

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