So what I'm trying to do is to make a Dialog
that shows when clicking a certain Button
. This is the code:
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();
As soon as I click the Button
, app crashes. This is the logcat file:
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)
The logcat says that the problem is in line 673. This is the line 673:
dialog.setContentView(R.layout.alert_start);
I really have no idea how to approach fixing this. I've had nullpointerexceptions
before but never with Dialogs
. What could be wrong?
alert_start xml code:
<?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" />
Line 676:
ok.setOnClickListener(new OnClickListener() {
I believe your problem is with Context
. Change
final Dialog dialog = new Dialog(this, android.R.style.Theme_Translucent);
to
final Dialog dialog = new Dialog(YourActivityName.this, android.R.style.Theme_Translucent);
The way you have it, this
is referring to the onClickListener
and not the Activity Context
. This is your original problem.
Your next problem is, as MH said in a comment, you are trying to get the ok
Button
from the Activity
layout but you need to get it from the Dialog
layout so it is returning null
.
Update
Try
ImageButton ok = (ImageButton) dialog.findViewById(R.id.button_ok);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.