简体   繁体   English

setText到EditText时发生NullPointerException-onDateSet

[英]NullPointerException when setText to EditText - onDateSet

I have an editText, where when the user clicks it, a date Dialog appears, and when date is set, the date should appear in the editText field. 我有一个editText,当用户单击它时,将显示一个日期对话框,并且在设置日期后,该日期应出现在editText字段中。 Although simple enough, I can 't find why I get a nullPointer Exception. 尽管很简单,但我找不到为什么会出现nullPointer异常。 I have tried several ways and changed my code a lot, but nothing works. 我尝试了几种方法并更改了很多代码,但是没有任何效果。 Here it is one of the concepts of my code: 这是我的代码的概念之一:

Start class: 开始课程:

public class Start extends FragmentActivity {

int year;
int month;
int day;

static final int DATE_DIALOG_ID = 999;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_start);
    EditText editText = (EditText) findViewById(R.id.etDepDate);
    editText.setText("date");

}

public void showDatePickerDialog(View v) {
    DialogFragment newFragment = new DatePick();
    newFragment.show(getFragmentManager(), "datepicker");

}

}

DatePick Class: DatePick类:

 public class DatePick extends DialogFragment implements
    DatePickerDialog.OnDateSetListener {
EditText editText;

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    // Use the current date as the default date in the picker
    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);

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

public void onDateSet(DatePicker view, int year, int month, int day) {
    // Do something with the date chosen by the user
    editText = (EditText) view.findViewById(R.id.etDepDate);
    editText.setText(day + "/" + month + "/" + year);
}

@Override
public void show(FragmentManager manager, String tag) {
    // TODO Auto-generated method stub
    super.show(manager, tag);
}
}

also XML: 也是XML:

 <EditText
     android:id="@+id/etDepDate"
    android:layout_width="wrap_content"
    android:layout_height="50dp"
    android:ems="10"
    android:focusable="false"
    android:hint="@string/dhint"
    android:inputType="date"
    android:onClick="showDatePickerDialog" >

    <requestFocus />
</EditText>

I had made it work with another method, but it is deprecated and I don 't want to use it. 我已经用另一种方法使它工作了,但是已经过时了,我不想使用它。 DialogFragments seems the right way to do it. DialogFragments似乎是正确的方法。

EDIT: 编辑:

 11-14 20:38:06.368: E/AndroidRuntime(31557): FATAL EXCEPTION: main
 11-14 20:38:06.368: E/AndroidRuntime(31557): java.lang.NullPointerException
11-14 20:38:06.368: E/AndroidRuntime(31557):    at        com.xxx.mytrips.DatePick.onDateSet(DatePick.java:36)
11-14 20:38:06.368: E/AndroidRuntime(31557):    at     android.app.DatePickerDialog.tryNotifyDateSet(DatePickerDialog.java:148)
11-14 20:38:06.368: E/AndroidRuntime(31557):    at android.app.DatePickerDialog.onClick(DatePickerDialog.java:116)
11-14 20:38:06.368: E/AndroidRuntime(31557):    at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
11-14 20:38:06.368: E/AndroidRuntime(31557):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-14 20:38:06.368: E/AndroidRuntime(31557):    at android.os.Looper.loop(Looper.java:137)
11-14 20:38:06.368: E/AndroidRuntime(31557):    at android.app.ActivityThread.main(ActivityThread.java:5041)
11-14 20:38:06.368: E/AndroidRuntime(31557):    at java.lang.reflect.Method.invokeNative(Native Method)
11-14 20:38:06.368: E/AndroidRuntime(31557):    at java.lang.reflect.Method.invoke(Method.java:511)
11-14 20:38:06.368: E/AndroidRuntime(31557):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
11-14 20:38:06.368: E/AndroidRuntime(31557):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
11-14 20:38:06.368: E/AndroidRuntime(31557):    at dalvik.system.NativeStart.main(Native Method)

It's because you're using editText = (EditText) view.findViewById(R.id.etDepDate); 这是因为您使用的是editText = (EditText) view.findViewById(R.id.etDepDate); . findViewById returns null in this case because editText is not a child of the DatePicker view. 在这种情况下,因为editText不是DatePicker视图的子级,所以findViewById返回null。

If the editText is part of the content view of your activity you can use this : 如果editText是活动内容视图的一部分,则可以使用以下命令:

editText = (EditText) getActivity().findViewById(R.id.etDepDate);

This is not really good practice by the way. 顺便说一句,这并不是真正的好习惯。 Preferably you would pass an external OnDateSetListener to the fragment. 最好将一个外部OnDateSetListener传递给该片段。

This line 这条线

    editText = (EditText) view.findViewById(R.id.etDepDate);

is giving you error as you can't find your EditText in DatePicker . 给您错误,因为您在DatePicker找不到您的EditText You will need to pass your EditText in the constructor for DatePick or somehow pass the value back to it in its orignal Activity 您将需要在DatePick的构造EditText中传递您的EditText或以某种方式在其原始Activity中将值传递回它

Create a constructor for you DatePicker class and pass the EditText to it. 为您的DatePicker类创建一个构造函数,并将EditText传递给它。 For example: 例如:

public DatePickerFragment (EditText editText) {
    this.editText = editText;
}

and then simply use it in onDateSet() method. 然后只需在onDateSet()方法中使用它onDateSet()

Use 采用

editText = (EditText) getActivity().findViewById(R.id.etDepDate);

instead of 代替

editText = (EditText) view.findViewById(R.id.etDepDate);

Because, you don't have edittext in your DatePick dialog.. Because of this only findViewById() returning null.. 因为,您的DatePick对话框中没有edittext。因此,只有findViewById()返回null。

What if you remove view from this function? 如果您从此功能中删除视图怎么办?

public void onDateSet(DatePicker view, int year, int month, int day) {
    // Do something with the date chosen by the user
    //so instead of:
    //editText = (EditText) view.findViewById(R.id.etDepDate);
    //use this:
    editText = (EditText) findViewById(R.id.etDepDate);
    editText.setText(day + "/" + month + "/" + year);
}

You can't access EditText inside DatePick class. 您无法在DatePick类中访问EditText You need to send the text back to Start from DatePick class. 您需要将文本发送回从DatePickStart you can do it this way. 你可以这样

  1. Define an interface and callback method in DatePick class. DatePick类中定义接口和回调方法。

     callbackListener listener; interface callbackListener { public void setText(String text); } 
  2. Implement the interface in Start class. Start类中实现接口。

     class Start extends FragmentActivity implements DatePick.callbackListener { ..... @Override public void setText(String text) { EditText editText = (EditText) view.findViewById(R.id.etDepDate); editText.setText(text); 

    } }

  3. In onAttach method of DatePick class assign the original activity to the interface object. DatePick类的onAttach方法中,将原始活动分配给接口对象。

     @Override public void onAttach(Activity activity) { super.onAttach(activity); listener = (callbackListener) activity; } 

4.Call the callback method in onDateSet method and pass the text 4,在onDateSet方法中调用回调方法并传递文本

public void onDateSet(DatePicker view, int year, int month, int day) {
    listener.setText(day + "/" + month + "/" + year);
}

Since your Edittext is in Fragment Activity and not in the DatePicker DialogFragment You cannot access it directly in this way. 由于您的Edittext在Fragment活动中,而不在DatePicker DialogFragment中,因此您不能以这种方式直接访问它。 Try creating a reference to your fragment activity and access it through a method. 尝试创建对片段活动的引用,并通过一种方法对其进行访问。 Declare Edittext's scope to be accessed across in this activity. 声明要在此活动中访问的Edittext范围。 Then, 然后,

public void setDate(String Dte){
  editText.setText(Dte);
}

And just reconstruct your DialogFragment onDateSet method like this. 只需像这样重构您的DialogFragment onDateSet方法。

public void onDateSet(DatePicker view, int year, int month, int day) {
 // Do something with the date chosen by the user
 ((MainActivity)getActivity()).setDate(day + "/" + month + "/" + year);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM