我不知道如何解决这个NullPointerException

时间:2013-07-24 19:03:07

标签: java android eclipse nullpointerexception

所以我要做的就是制作一个Dialog,点击某个Button时显示。这是代码:

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

点击Button后,应用就崩溃了。这是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() {

1 个答案:

答案 0 :(得分:1)

我相信您的问题在于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在评论中所说,您正试图从ok布局获取Button Activity,但您需要从{{1}获取它布局所以它返回Dialog

<强>更新

尝试

null