【问题标题】:NullPointerException when setText to EditText - onDateSet当 setText 到 EditText 时出现 NullPointerException - onDateSet
【发布时间】:2013-11-15 12:40:23
【问题描述】:

我有一个editText,当用户单击它时,会出现一个日期对话框,当设置日期时,日期应该出现在editText字段中。虽然很简单,但我找不到为什么会出现 nullPointer 异常。我尝试了几种方法并更改了很多代码,但没有任何效果。 这是我的代码的概念之一:

开始上课:

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");

}

}

日期选择类:

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

还有 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>

我已经使它与另一种方法一起使用,但它已被弃用,我不想使用它。 DialogFragments 似乎是正确的方法。

编辑:

 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)

【问题讨论】:

  • 请发布堆栈跟踪。
  • 添加堆栈跟踪和/或指定代码中的行。
  • 你确定在第二种情况下 EditText 也有 id etDepDate
  • 嗨 BabyGorilla,你从哪里得到 NullPointerException?向我们展示你的 logcat...
  • 编辑文本对你来说是空的。

标签: java android android-dialogfragment android-datepicker


【解决方案1】:

这是因为您使用的是editText = (EditText) view.findViewById(R.id.etDepDate);。在这种情况下,findViewById 返回 null,因为 editText 不是 DatePicker 视图的子视图。

如果 editText 是您活动的内容视图的一部分,您可以使用它:

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

顺便说一句,这不是一个很好的做法。最好将外部 OnDateSetListener 传递给片段。

【讨论】:

  • 哇,这成功了!虽然知道我必须以正确的方式工作。
【解决方案2】:

这一行

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

因为您在DatePicker 中找不到您的EditText,所以给您带来了错误。您需要在 DatePick 的构造函数中传递您的 EditText 或以某种方式将值传递回其原始 Activity

【讨论】:

    【解决方案3】:

    为您的 DatePicker 类创建一个构造函数并将 EditText 传递给它。 例如:

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

    然后简单地在onDateSet()方法中使用它。

    【讨论】:

    • 这一行的多个标记 - 避免片段中的非默认构造函数:使用默认构造函数加上 Fragment#setArguments(Bundle) 代替 - 此片段应提供默认构造函数(不带参数的公共构造函数) (com.xxx.mytrips.DatePick)
    【解决方案4】:

    使用

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

    而不是

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

    因为,您的 DatePick 对话框中没有 edittext。因此只有 findViewById() 返回 null ..

    【讨论】:

      【解决方案5】:

      如果你从这个函数中删除视图会怎样?

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

      【讨论】:

      • DatePick 类型的 findViewById(int) 方法未定义
      【解决方案6】:

      您无法在 DatePick 类中访问 EditText。您需要将文本从DatePick 类发送回Start。 你可以这样做。

      1. DatePick类中定义一个接口和回调方法。

        callbackListener listener;
        
        interface callbackListener {
        public void setText(String text);
        }
        
      2. 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. DatePick 类的onAttach 方法中,将原始活动分配给接口对象。

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

      4.调用onDateSet方法中的回调方法并传递文本

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

      【讨论】:

        【解决方案7】:

        由于您的 Edittext 在 Fragment Activity 中而不是在 DatePicker DialogFragment 中,因此您无法以这种方式直接访问它。尝试创建对您的片段活动的引用并通过方法访问它。在此活动中声明要访问的 Edittext 范围。那么,

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

        然后像这样重构您的 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);
        }
        

        【讨论】:

        • 你也可以使用界面来完成。
        猜你喜欢
        • 1970-01-01
        • 2014-04-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多