gpt4 book ai didi

java - 在对话框上查找按钮时出现空指针异常

转载 作者:行者123 更新时间:2023-12-01 15:48:27 25 4
gpt4 key购买 nike

这是在我的 onCreate() 中

final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.go_pro);
Button free = (Button) dialog.findViewById(R.id.still_free);
free.setOnClickListener(new OnClickListener() { //EXCEPTION HERE!
@Override
public void onClick(View v) {
dialog.cancel();
}
});

这是 go_pro 的布局:

 <LinearLayout android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_width="wrap_content">
<Button android:id="@+id/still_free" android:text="Keep the ads" android:layout_width="wrap_content" android:layout_height="wrap_content" ></Button>
<Button android:id="@+id/full_version" android:text="Go PRO" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>

日志猫:

07-09 16:23:40.102: ERROR/AndroidRuntime(11435): FATAL EXCEPTION: main
07-09 16:23:40.102: ERROR/AndroidRuntime(11435): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.convert/com.convert.MainWindowYuval}: java.lang.NullPointerException
07-09 16:23:40.102: ERROR/AndroidRuntime(11435): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
07-09 16:23:40.102: ERROR/AndroidRuntime(11435): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
07-09 16:23:40.102: ERROR/AndroidRuntime(11435): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
07-09 16:23:40.102: ERROR/AndroidRuntime(11435): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
07-09 16:23:40.102: ERROR/AndroidRuntime(11435): at android.os.Handler.dispatchMessage(Handler.java:99)
07-09 16:23:40.102: ERROR/AndroidRuntime(11435): at android.os.Looper.loop(Looper.java:123)
07-09 16:23:40.102: ERROR/AndroidRuntime(11435): at android.app.ActivityThread.main(ActivityThread.java:4627)
07-09 16:23:40.102: ERROR/AndroidRuntime(11435): at java.lang.reflect.Method.invokeNative(Native Method)
07-09 16:23:40.102: ERROR/AndroidRuntime(11435): at java.lang.reflect.Method.invoke(Method.java:521)
07-09 16:23:40.102: ERROR/AndroidRuntime(11435): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
07-09 16:23:40.102: ERROR/AndroidRuntime(11435): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
07-09 16:23:40.102: ERROR/AndroidRuntime(11435): at dalvik.system.NativeStart.main(Native Method)
07-09 16:23:40.102: ERROR/AndroidRuntime(11435): Caused by: java.lang.NullPointerException
**07-09 16:23:40.102: ERROR/AndroidRuntime(11435): at com.convert.MainWindowYuval.onCreate(MainWindowYuval.java:93)**MY CLASS!!@#!@#!@#!@#!@#
07-09 16:23:40.102: ERROR/AndroidRuntime(11435): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-09 16:23:40.102: ERROR/AndroidRuntime(11435): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)

编辑:这太奇怪了!我的 xml 中有两个具有不同 id 的相同按钮,并且 on 创建的两个按钮完全相同,但只有其中一个出现空指针异常

 Button button1 = (Button) dialog.findViewById(R.id.full_version);
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("market://details?id=com.paulmaidment.games.flagsoftheworld"));
startActivity(intent);
dialog.cancel();//TODO OPEN PAID APP HERE!!!
}
});

Button free = (Button)findViewById(R.id.stay_free);
free.setOnClickListener(new OnClickListener() { //EXCEPTION ONLY HERE!
@Override
public void onClick(View v) {
dialog.cancel();
}
});

最佳答案

当应该在 Activity 上调用 findViewById 时,您正在对 Dialog 调用 findViewById。改变

        Button free = (Button) dialog.findViewById(R.id.still_free);

        Button free = (Button) findViewById(R.id.still_free);

应该修复它。

关于java - 在对话框上查找按钮时出现空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6635020/

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