android – 当方向更改时,强制关闭并打开日期对话框

前端之家收集整理的这篇文章主要介绍了android – 当方向更改时,强制关闭并打开日期对话框前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我有一个活动,我想要的只是

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

我想要使​​用带有代码的DialogFragment的aceess Datepicker.

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
    }
}

上面的代码当Device没有改变它的方向时工作正常.但是当显示Datepicker并且方向更改时,我将强制关闭,如下所示

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)

这是我调用DatePickerFragment的方法

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

请帮我摆脱这个.
提前致谢.

最佳答案
我不确定这个解决方案适合你,但你可以尝试一下.

您面临的问题是因为每次定向更改时您的活动都会重新加载.您可以调用OnConfigurationchanged()方法,在Menifest文件中可以将Configuchanges设置为orientation | screensize

通过执行此操作,您的活动将不会在方向更改时加载,并且您将不会更长时间面对您当前面临的问题.

可以帮到你.

原文链接:https://www.f2er.com/android/430761.html

猜你在找的Android相关文章