gpt4 book ai didi

Android - popUpWindow 中的 DatePicker

转载 作者:太空狗 更新时间:2023-10-29 16:34:36 26 4
gpt4 key购买 nike

我试图在 popUpWindow 中显示日期选择器。当用户单击 popUpWindow 中的“选择日期”按钮时,应该显示日历。问题是我得到了一个 NullPointerException,显然我的 button 是 null,我不明白为什么。

这是我的主要 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;
}
};

输入旅程.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>

我的日志:

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)

知道哪里出了问题吗?

最佳答案

您的按钮位于您正在使用 (input_journey.xml) 填充弹出窗口的 View 上,而您引用该按钮就好像它是另一个 View 的一部分一样,因此无法访问.

按如下方式在弹出窗口 View 中访问该按钮:

button = (Button) promptView2.findViewById(R.id.button_pick);

请注意,您声明的 View promptView2 是按钮的父 View 。

关于Android - popUpWindow 中的 DatePicker,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31818083/

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