我想在屏幕中央的对话框中显示 CalendarView。为什么在对话框中?因为当我打开我的 CalendarView 时,它占据了所有屏幕,而我想在对话框中显示它以查看背景..
所以这是我的 calendar_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<CalendarView
android:id="@+id/calendarView1"
android:layout_width="400dp"
android:layout_height="400dp"
android:layout_marginLeft="150dp"/>
</RelativeLayout>
打开我的日历是我的方法
public void showCalendar() {
Dialog dialog = new Dialog(getBaseContext());
// setContentView(R.layout.calendar_main);
cal = (CalendarView) findViewById(R.id.calendarView1);
dialog.setContentView(R.layout.calendar_main);
cal.setOnDateChangeListener(new OnDateChangeListener() {
@Override
public void onSelectedDayChange(CalendarView view, int year, int month,
int dayOfMonth) {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(),"Selected Date is\n\n"
+dayOfMonth+" : "+month+" : "+year ,
Toast.LENGTH_LONG).show();
}
});
}
如果没有 Dialog ,我的日历会以我在 xml 中定义的良好大小正确打开。但是通过对话框,我的应用程序崩溃了..
这是我的错误日志:
E/AndroidRuntime(27862): FATAL EXCEPTION: main
E/AndroidRuntime(27862): java.lang.NullPointerException
E/AndroidRuntime(27862): at com.pdf.GetViewPDF.showCalendar(GetViewPDF.java:90)
E/AndroidRuntime(27862): at com.pdf.GetViewPDF.onOptionsItemSelected(GetViewPDF.java:113)
E/AndroidRuntime(27862): at android.app.Activity.onMenuItemSelected(Activity.java:2453)
E/AndroidRuntime(27862): at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:846)
E/AndroidRuntime(27862): at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:153)
E/AndroidRuntime(27862): at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:956)
E/AndroidRuntime(27862): at com.android.internal.view.menu.ActionMenuView.invokeItem(ActionMenuView.java:170)
E/AndroidRuntime(27862): at com.android.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:85)
E/AndroidRuntime(27862): at android.view.View.performClick(View.java:3117)
E/AndroidRuntime(27862): at android.view.View$PerformClick.run(View.java:11941)
E/AndroidRuntime(27862): at android.os.Handler.handleCallback(Handler.java:587)
E/AndroidRuntime(27862): at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime(27862): at android.os.Looper.loop(Looper.java:132)
E/AndroidRuntime(27862): at android.app.ActivityThread.main(ActivityThread.java:4123)
E/AndroidRuntime(27862): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(27862): at java.lang.reflect.Method.invoke(Method.java:491)
E/AndroidRuntime(27862): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
E/AndroidRuntime(27862): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
E/AndroidRuntime(27862): at dalvik.system.NativeStart.main(Native Method)
否则,我从我的 ActionBar 中的项目按钮调用 showCalendar,当我调用我的侦听器时出现 NullPointerException:
cal.setOnDateChangeListener(new OnDateChangeListener() {...