Armanoide
Armanoide

Reputation: 1348

nullPointerException on Datepicker

It seems that i have a problem with the declaration of my DatePicker. the null pointer is in :

pickerDate.init(year, month, day, null);

My code :

private static DatePicker pickerDate;

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        cc = this;
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_reservation);
        Button btn_add = (Button) findViewById(R.id.btn_add_reservation);
        pickerDate = (DatePicker) findViewById(R.id.datePickerId);
        final Calendar c = Calendar.getInstance();
        year = c.get(Calendar.YEAR);
        month = c.get(Calendar.MONTH);
        day = c.get(Calendar.DAY_OF_MONTH);
        pickerDate.init(0, 0, 0, null);
//      pickerDate.init(year, month, day, null);
        btn_add.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) { open_a_form_page(); } });
    }
}

My Xml :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="50dp" >

<DatePicker
        android:id="@+id/datePickerId"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true" />


</RelativeLayout>

and the error show :

08-12 01:06:49.790: E/AndroidRuntime(2410): FATAL EXCEPTION: main
08-12 01:06:49.790: E/AndroidRuntime(2410): java.lang.RuntimeException: Unable to start activity ComponentInfo{xx.xx.cc/ee.rr.name_app.XActivity}: java.lang.NullPointerException

any Idea ?

Upvotes: 1

Views: 1676

Answers (1)

Petros Koutsolampros
Petros Koutsolampros

Reputation: 2800

This seems to happen because your Datepicker is in a different xml file than your current activity. You either have to move your datepicker into the xml of the activity you are using or inflate the other file and take the picker from there like this:

View theInflatedView = getLayoutInflater().inflate(R.layout.otherxmlfile, null);
DatePicker pickerDate = (DatePicker) theInflatedView.findViewById(R.id.datePickerId);

Upvotes: 3

Related Questions