Reputation: 1437
I am trying to display a datepicker in a popUpWindow. When the user clicks the button "pick a date" in the popUpWindow the calendar is supposed to be displayed. The problem is that I get a NullPointerException
as apparently my button
is null and I don't understand why.
This is my main activity:
private void popUpWindow2() {
LayoutInflater layoutInflater = LayoutInflater.from(MarkerActivity.this);
View promptView2 = layoutInflater.inflate(R.layout.input_journey, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MarkerActivity.this);
alertDialogBuilder.setView(promptView2);
button = (Button) findViewById(R.id.button_pick);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showDialog(DATE_DIALOG_ID);
}
});
// setup a dialog window
alertDialogBuilder.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
String msg3 = "The POI has been added to your journey on " + day + "-" + month+1 + "-" + year;
Toast.makeText(getApplicationContext(), msg3, Toast.LENGTH_LONG).show();
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
// create an alert dialog
AlertDialog alert = alertDialogBuilder.create();
alert.show();
}
@Override
public Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
// set date picker as current date
return new DatePickerDialog(this, datePickerListener, year, month,day);
}
return null;
}
public DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {
// when dialog box is closed, below method will be called.
public void onDateSet(DatePicker view, int selectedYear,int selectedMonth, int selectedDay) {
year = selectedYear;
month = selectedMonth;
day = selectedDay;
}
};
input_journey.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_below="@+id/edittext1"
android:text="Would you like to add this POI to your journey?"
android:id="@+id/add_to_journey" />
<Button
android:id="@+id/button_pick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pick a date!" />
</LinearLayout>
my logcat:
08-04 20:52:07.748 3552-3552/com.example.diana.track E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at com.example.diana.track.MarkerActivity.popUpWindow2(MarkerActivity.java:457)
at com.example.diana.track.MarkerActivity.access$000(MarkerActivity.java:51)
at com.example.diana.track.MarkerActivity$3.onClick(MarkerActivity.java:123)
at android.view.View.performClick(View.java:4421)
at android.view.View$PerformClick.run(View.java:17903)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5214)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
at dalvik.system.NativeStart.main(Native Method)
Any idea what might be wrong?
Upvotes: 2
Views: 772
Reputation: 1158
Your button is on the view you are inflating the popup window with (input_journey.xml) and instead you are referencing the button as if it is part of another view, thus can't be reached.
Access that button within your popup window view as follows:
button = (Button) promptView2.findViewById(R.id.button_pick);
Note that the View
promptView2 that you have declared is the parent view for your button.
Upvotes: 3
Reputation: 8851
Replace findViewById(R.id.button_pick);
with promptView2.findViewById(R.id.button_pick);
Upvotes: 2