Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force close when Orientation Changes with open Date DialogueFragment

I have an Activity and All i want is

Activity--> OnClick of Some layout (That has textview)-->Open Datepicker--> set selected value in txtvalue

i want to aceess Datepicker Using DialogFragment with Below Code.

package com.app.ourforms.date;

import java.util.Calendar;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.os.Bundle;
import android.util.Log;
import android.widget.DatePicker;
import android.widget.TextView;

public class DatePickerFragment extends DialogFragment implements
        DatePickerDialog.OnDateSetListener {

    TextView txt;

    public DatePickerFragment() {
    }

    public DatePickerFragment(TextView txt) {
        this.txt = txt;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceSateate) {

        Log.i("TAG", "Inside onCreateDialog");
        final Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int day = c.get(Calendar.DAY_OF_MONTH);

        return new DatePickerDialog(getActivity(), this, year, month, day);
    }

    public void onDateSet(DatePicker view, int year, int month, int day) {
        Log.i("TAG", "Inside onDateSet");
        txt.setText(day + "/" + (month + 1) + "/" + year);
        // Do something with the date chosen
    }
}

Above code Worked fine when Device does not change its orientation. But When Datepicker is shown and Orientation Changes i am getting force close as Below

01-04 07:34:14.580: E/AndroidRuntime(2375): FATAL EXCEPTION: main
01-04 07:34:14.580: E/AndroidRuntime(2375): java.lang.NullPointerException
01-04 07:34:14.580: E/AndroidRuntime(2375):     at com.app.ourforms.date.DatePickerFragment.onDateSet(DatePickerFragment.java:39)
01-04 07:34:14.580: E/AndroidRuntime(2375):     at android.app.DatePickerDialog.tryNotifyDateSet(DatePickerDialog.java:148)
01-04 07:34:14.580: E/AndroidRuntime(2375):     at android.app.DatePickerDialog.onClick(DatePickerDialog.java:116)
01-04 07:34:14.580: E/AndroidRuntime(2375):     at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
01-04 07:34:14.580: E/AndroidRuntime(2375):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-04 07:34:14.580: E/AndroidRuntime(2375):     at android.os.Looper.loop(Looper.java:137)
01-04 07:34:14.580: E/AndroidRuntime(2375):     at android.app.ActivityThread.main(ActivityThread.java:5039)
01-04 07:34:14.580: E/AndroidRuntime(2375):     at java.lang.reflect.Method.invokeNative(Native Method)
01-04 07:34:14.580: E/AndroidRuntime(2375):     at java.lang.reflect.Method.invoke(Method.java:511)
01-04 07:34:14.580: E/AndroidRuntime(2375):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-04 07:34:14.580: E/AndroidRuntime(2375):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-04 07:34:14.580: E/AndroidRuntime(2375):     at dalvik.system.NativeStart.main(Native Method)

Here is my method for calling DatePickerFragment

public void showDatePickerDialog(View v) {
        Log.i("TAG", "Inside showDatePickerDialog");
        DialogFragment newFragment = new DatePickerFragment((TextView)v);
        newFragment.show(getFragmentManager(), "datePicker");
    }

Please help me to get out of this. Thanks in Advance.

like image 840
Bhavesh Patadiya Avatar asked Jan 04 '13 07:01

Bhavesh Patadiya


2 Answers

I am Not Sure this Solution Work for you but you can try it out.

The Problem you are facing its because your Activity Reloads every time your Orientation Changes. you can call OnConfigurationchanged() method and inside Menifest file you can set Configuchanges to orientation|screensize

By Doing this your Activity will not load when orientation Changes and you will not get longer face the Problem you are Currently Facing.

May Help you.

like image 182
Yog Guru Avatar answered Oct 20 '22 11:10

Yog Guru


Your TextView reference is null. The NPE is on line 39 of your DatePickerFragment. The reason its null is because when the orientation changes the entire view hierarchy is recreated, and the reference to the TextView that you passed in to your DatePickerFragment no longer exists.

Instead of passing in the TextView instance, try passing in the ID of the TextView.

new DatePickerFragment(v.getId());

Then implement

public Bundle onSaveInstanceState ()

and

public void onRestoreInstanceState (Bundle savedInstanceState)

to save and restore the ID of your TextView for when your DatePickerDialog is destroyed and recreated.

You will then need to use

getActivity().findViewById(your_text_view_ID)

to find your TextView instance in the view hierarchy so that you can set it's text.

like image 1
Akos Cz Avatar answered Oct 20 '22 10:10

Akos Cz