android 4 - Dialog Fragment - NullPointer异常

时间:2017-05-31 08:56:31

标签: android android-fragments nullpointerexception

Android 4和我的Dialog Fragments存在问题。我可以打开Dialogs,但如果我取消对话或每次获得NullPointerException时都提交。在较新的Android版本中,如5或6,一切都很棒。 太烦人了。为什么google为什么???我搜索了几天并测试了一些东西,但我不能让它工作。

public class BirthdatePickerDialogFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {

//region Variables



//Listener Object
private BirthdatePickerDialogFragment.OnCompleteBabyBirthdateListener setDateListener;

Context sourceActivityContext = null;

//endregion



@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    String savedBirthdateString = SharedPreferenceHelper.getSharedPreferenceString(sourceActivityContext,SharedPreferenceHelper.Birthdate,"");

    int year = 0;
    int month = 0;
    int day = 0;


    if(savedBirthdateString != "")
    {
        String[] splitBirthdateString = savedBirthdateString.split("\\.");


        int strYear = Integer.parseInt(splitBirthdateString[2]);
        int strMonth = (Integer.parseInt(splitBirthdateString[1])) - 1 ; 
        int strDay = Integer.parseInt(splitBirthdateString[0]);

        Calendar c = Calendar.getInstance();
        c.set(strYear,strMonth,strDay);


         year = c.get(Calendar.YEAR);
         month = c.get(Calendar.MONTH);
         day = c.get(Calendar.DAY_OF_MONTH);

    }        
    else{
        // Use the current date as the default date in the picker
        final Calendar c = Calendar.getInstance();
         year = c.get(Calendar.YEAR);
         month = c.get(Calendar.MONTH);
         day = c.get(Calendar.DAY_OF_MONTH);

    }

    // Create a new instance of DatePickerDialog and return it
    return new DatePickerDialog(getActivity(), this, year, month, day);
}

@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
    String strYear =  String.valueOf(year);
    String strDayOfMonth = String.valueOf(dayOfMonth);
    String strMonth = String.valueOf(month + 1);

    setDateListener.onCompleteBirthDatePicker(MessageFormat.format("{0}.{1}.{2}", strDayOfMonth, strMonth, strYear));
}


public static interface OnCompleteBabyBirthdateListener {
    public abstract void onCompleteBirthDatePicker(String babyBirthDate);

}

@Override
public void onAttach(Context context) {
    super.onAttach(context);

    try {
        this.setDateListener = (BirthdatePickerDialogFragment.OnCompleteBabyBirthdateListener)context;
        this.sourceActivityContext = context;
    }
    catch (final ClassCastException e) {
        throw new ClassCastException(context.toString() + " must implement OnCompleteListener");
    }      

}

}

这是出生日期对话框。它会打开,但如果我提交或取消,则会抛出异常。 我希望有人可以帮助我。 Exceptionlog:

E/AndroidRuntime: FATAL EXCEPTION: main
              java.lang.NullPointerException
                  at com.jalbmagic.test.Dialogs.BirthdatePickerDialogFragment.onDateSet(BirthdatePickerDialogFragment.java:93)
                  at android.app.DatePickerDialog.tryNotifyDateSet(DatePickerDialog.java:148)
                  at android.app.DatePickerDialog.onStop(DatePickerDialog.java:155)
                  at android.app.Dialog.dismissDialog(Dialog.java:326)
                  at android.app.Dialog.dismiss(Dialog.java:302)
                  at android.app.Dialog.cancel(Dialog.java:1104)
                  at android.app.Dialog.onTouchEvent(Dialog.java:617)
                  at android.app.Dialog.dispatchTouchEvent(Dialog.java:739)
                  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1873)
                  at android.view.View.dispatchPointerEvent(View.java:7307)
                  at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3174)
                  at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3119)
                  at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4155)
                  at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4134)
                  at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4226)
                  at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:171)
                  at android.os.MessageQueue.nativePollOnce(Native Method)
                  at android.os.MessageQueue.next(MessageQueue.java:125)
                  at android.os.Looper.loop(Looper.java:124)
                  at android.app.ActivityThread.main(ActivityThread.java:4745)
                  at java.lang.reflect.Method.invokeNative(Native Method)
                  at java.lang.reflect.Method.invoke(Method.java:511)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
                  at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:0)

尝试替换

  xAxis.labels().listen('mouseOver', function(e) {
    var labelIndex = e.labelIndex;
    var label = this.getLabel(labelIndex);
    var value = xAxis.scale().ticks().get()[labelIndex];
    label.format(value * 2);
    label.fontColor('red');
    label.draw();
  });

答案 1 :(得分:0)

我发现了我的问题。从API Level 23开始,交换了onAttach(Activity activity)方法,并且新版本是onAttach(Context context)。 我一直用Android 6进行测试,并且只在onAttach(Context context)上实现,所以较低的API级别然后23不能使用这种方法,我从未将活动附加到片段。所以问题很明显。我实施旧方法后一切正常。