空指针异常:在 Android 应用中设置日期时发生。

huangapple go评论89阅读模式
英文:

NullPointerException when setting Date in Android app

问题

我正在按照这个教程来实现在编辑文本点击时显示日期和时间。

public class LoadActivity extends AppCompatActivity {

 @SuppressLint("StaticFieldLeak")
 static EditText DateEdit;

  @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_load);
    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
     
     EditText startTimeEditText = (EditText) findViewById(R.id.start_time);   

    startTimeEditText.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            showTruitonTimePickerDialog(v);
            showTruitonDatePickerDialog(v);
        }
    });

}  // end onCreate

   public void showTruitonDatePickerDialog(View v) {
    DialogFragment newFragment = new DatePickerFragment();
    newFragment.show(getSupportFragmentManager(), "datePicker");
}

public static class DatePickerFragment extends DialogFragment implements
        DatePickerDialog.OnDateSetListener {

    @NonNull
    @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(requireActivity(), this, year, month, day);
    }

  
}

    @SuppressLint("SetTextI18n")
    public void onDateSet(DatePicker view, int year, int month, int day) {
        // Do something with the date chosen by the user
        DateEdit.setText(day + ":" + (month + 1) + ":" + year);
    }

}// end main class

我能够在我的应用程序中看到日期片段,但当我点击“确定”按钮时,应用程序崩溃,我得到错误信息:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.EditText.setText(java.lang.CharSequence)' on a null object reference
at com.thumbsol.accuratemobileassetsmanagament.LoadActivity$DatePickerFragment.onDateSet(LoadActivity.java:300)
at android.app.DatePickerDialog.onClick(DatePickerDialog.java:134)

行号300DateEdit.setText(day + ":" + (month + 1) + ":" + year);

我该如何解决这个错误?任何帮助将不胜感激。

英文:

I am following this tutorial for implementing date and time on edit text click.

public class LoadActivity extends AppCompatActivity {
@SuppressLint("StaticFieldLeak")
static EditText DateEdit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_load);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
EditText startTimeEditText = (EditText) findViewById(R.id.start_time);   
startTimeEditText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showTruitonTimePickerDialog(v);
showTruitonDatePickerDialog(v);
}
});
}  // end onCreate
public void showTruitonDatePickerDialog(View v) {
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getSupportFragmentManager(), "datePicker");
}
public static class DatePickerFragment extends DialogFragment implements
DatePickerDialog.OnDateSetListener {
@NonNull
@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(requireActivity(), this, year, month, day);
}
}
@SuppressLint("SetTextI18n")
public void onDateSet(DatePicker view, int year, int month, int day) {
// Do something with the date chosen by the user
DateEdit.setText(day + ":" + (month + 1) + ":" + year);
}
}// end main class

I am able to see the date fragment in my app but when I press ok button the app crashes and I am getting error

> java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.EditText.setText(java.lang.CharSequence)' on a null object reference
at com.thumbsol.accuratemobileassetsmanagament.LoadActivity$DatePickerFragment.onDateSet(LoadActivity.java:300)
at android.app.DatePickerDialog.onClick(DatePickerDialog.java:134)

The line number 300 is DateEdit.setText(day + ":" + (month + 1) + ":" + year);

How can I get rid of this error? Any help would be highly appreciated.

答案1

得分: 1

你只需要定义一个 static EditText DateEdit,然后通过 findViewById 获取它。
例如:

DateEdit = (EditText) findViewById(R.id.date_edit);
英文:

You only define static EditText DateEdit, and also need to get it via findViewById.
for example:

DateEdit = (EditText) findViewById(R.id.date_edit);

答案2

得分: 0

因为你在代码中没有设置DateEdit的值。

提示:尽量不要将视图设置为静态,这可能会导致内存泄漏。

英文:

That's because you're not setting the value of DateEdit anywhere in your code.

Tip: try to not use a view as static. It may cause leaks.

huangapple
  • 本文由 发表于 2020年8月16日 20:27:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/63436880.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定