Domanda

i would like to display a CalendarView in a dialog , at the center of the screen. Why in the dialog ? Because when i open my CalendarView , it's take all the screen , whereas i would like to display it , in dialog to see the background..

So it's my 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>

and it's my method who open my calendar

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();

            }
        });
}

Without Dialog , my calendar open correctly with the good size that i defined in xml. but with dialog, my app crash..

It's my error log :

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)

Otherwise , i call showCalendar from a item buton in my ActionBar and the NullPointerException appear when i call my listener :

cal.setOnDateChangeListener(new OnDateChangeListener() {...
È stato utile?

Soluzione

Try to change this:

cal = (CalendarView) findViewById(R.id.calendarView1);
dialog.setContentView(R.layout.calendar_main);

to this:

dialog.setContentView(R.layout.calendar_main);
cal = (CalendarView) dialog.findViewById(R.id.calendarView1);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top