所以我想做的是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() {