gpt4 book ai didi

java - 我不知道如何修复这个 NullPointerException

转载 作者:行者123 更新时间:2023-12-01 14:18:47 25 4
gpt4 key购买 nike

所以我想做的是制作一个在单击某个按钮时显示的对话框。这是代码:

final Dialog dialog = new Dialog(this, android.R.style.Theme_Translucent);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.alert_start);

ImageButton ok = (ImageButton)findViewById(R.id.button_ok);
ok.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
SharedPreferences settings = getSharedPreferences(CheckpointsActivity.PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putInt(CheckpointsActivity.PREFS_CURRENT_CHECKPOINT, -1);
editor.putInt(CheckpointsActivity.PREFS_CURRENT_HINT, 1);
editor.putBoolean(CheckpointsActivity.PREFS_TIMER_IS_PAUSED, false);
editor.commit();

Intent intent = new Intent(PlaceItemActivity.this, CheckpointsActivity.class);
intent.putExtra(MainActivity.INTENT_PLACE_ITEM_ID, placeItem.id);
//intent.setClass(this, CheckpointsActivity.class);
startActivity(intent);
}
});

ImageButton cancel = (ImageButton)findViewById(R.id.button_negative);
cancel.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();

一旦我单击按钮,应用程序就会崩溃。这是 logcat 文件:

07-24 20:45:00.261: E/AndroidRuntime(17139): FATAL EXCEPTION: main
07-24 20:45:00.261: E/AndroidRuntime(17139): java.lang.NullPointerException
07-24 20:45:00.261: E/AndroidRuntime(17139): at android.view.ViewGroup.addViewInner(ViewGroup.java:3338)
07-24 20:45:00.261: E/AndroidRuntime(17139): at android.view.ViewGroup.addView(ViewGroup.java:3210)
07-24 20:45:00.261: E/AndroidRuntime(17139): at android.view.ViewGroup.addView(ViewGroup.java:3186)
07-24 20:45:00.261: E/AndroidRuntime(17139): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:289)
07-24 20:45:00.261: E/AndroidRuntime(17139): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:279)
07-24 20:45:00.261: E/AndroidRuntime(17139): at android.app.Dialog.setContentView(Dialog.java:482)
07-24 20:45:00.261: E/AndroidRuntime(17139): at si.dvanadva.evanturist.PlaceItemActivity.onClick(PlaceItemActivity.java:673)
07-24 20:45:00.261: E/AndroidRuntime(17139): at android.view.View.performClick(View.java:4204)
07-24 20:45:00.261: E/AndroidRuntime(17139): at android.view.View$PerformClick.run(View.java:17355)
07-24 20:45:00.261: E/AndroidRuntime(17139): at android.os.Handler.handleCallback(Handler.java:725)
07-24 20:45:00.261: E/AndroidRuntime(17139): at android.os.Handler.dispatchMessage(Handler.java:92)
07-24 20:45:00.261: E/AndroidRuntime(17139): at android.os.Looper.loop(Looper.java:137)
07-24 20:45:00.261: E/AndroidRuntime(17139): at android.app.ActivityThread.main(ActivityThread.java:5041)
07-24 20:45:00.261: E/AndroidRuntime(17139): at java.lang.reflect.Method.invokeNative(Native Method)
07-24 20:45:00.261: E/AndroidRuntime(17139): at java.lang.reflect.Method.invoke(Method.java:511)
07-24 20:45:00.261: E/AndroidRuntime(17139): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-24 20:45:00.261: E/AndroidRuntime(17139): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-24 20:45:00.261: E/AndroidRuntime(17139): at dalvik.system.NativeStart.main(Native Method)

logcat 说问题出在第 673 行。这是第 673 行:

dialog.setContentView(R.layout.alert_start);

我真的不知道如何解决这个问题。我以前遇到过 nullpointerexceptions,但从未遇到过 Dialogs。可能出了什么问题?

alert_start xml 代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/alert_start"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@color/dim"
android:padding="30dp"
>

<ImageButton
android:id="@+id/button_ok"
android:layout_width="58dp"
android:layout_height="58dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:background="@drawable/btn_forward"
android:scaleType="fitXY"

/>

<ImageButton
android:id="@+id/button_negative"
android:layout_width="58dp"
android:layout_height="58dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:background="@drawable/btn_exit"
android:scaleType="fitXY"

/>

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/button_negative"
android:layout_toLeftOf="@+id/button_ok"
android:layout_margin="5dp"

android:text="@string/alert_start"
android:gravity="center_horizontal|center_vertical"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@color/black"
android:background="@drawable/background_rounded_corners" />

第 676 行:

ok.setOnClickListener(new OnClickListener() {

最佳答案

我相信您的问题出在Context上。改变

final Dialog dialog = new Dialog(this, android.R.style.Theme_Translucent);

final Dialog dialog = new Dialog(YourActivityName.this, android.R.style.Theme_Translucent);

按照您的方式,this 指的是 onClickListener 而不是 Activity Context。这是你原来的问题。

你的下一个问题是,正如 MH 在评论中所说,你试图从 Activity 布局中获取 ok Button 但你需要从 Dialog 布局中获取它,因此它返回 null

更新

尝试

ImageButton ok = (ImageButton) dialog.findViewById(R.id.button_ok);

关于java - 我不知道如何修复这个 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17842686/

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