如何解决在Android应用中设置日期时发生NullPointerException
我正在遵循此tutorial来实现点击编辑文本的日期和时间。
import pickle
import os
#defining save,add any variables to this that you want to save following the same format
def save():
pickle.dump(race,open(r'C:\Users\%username%\Desktop\test\race.dat','wb'))
print("Saved!")
return
#checking if a save file already exists
race_exist=os.path.isfile(r'C:\Users\%username%\Desktop\test\race.dat')
#if the save file exists,start from here
if race_exist==True:
race=pickle.load(open(r'C:\Users\%username%\Desktop\test\race.dat','rb'))
if race=='human':
direction=input("You arrive at a crossroads during your travel,would you like to go left or right")
if direction=='save':
save()
else:print('someone stabbed you because theyre racist.')
#if save file does not exist,start from here:
elif race_exist==False:
race=input("Are you an orc or human?")
pickle.dump(race,'wb'))
if race=='orc':
print("People dislike orcs,you get stabbed during your sleep and die!")
elif race=='human':
direction=input("You arrive at a crossroads during your travel,would you like to go left or right")
if direction=='save':
save()
我可以在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
中看到日期片段,但是当我按下app
按钮时,应用崩溃了,并且出现了错误
java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'void android.widget.EditText.setText(java.lang.CharSequence)' 在com.thumbsol.accuratemobileassetsmanagament.LoadActivity $ DatePickerFragment.onDateSet(LoadActivity.java:300) 在android.app.DatePickerDialog.onClick(DatePickerDialog.java:134)
行号ok
是300
如何摆脱这个错误?任何帮助将不胜感激。
解决方法
您仅定义static EditText DateEdit
,并且还需要通过findViewById
来获取它。
例如:
DateEdit = (EditText) findViewById(R.id.date_edit);
,
那是因为您没有在代码中的任何地方设置DateEdit
的值。
提示:尽量不要将视图用作静态视图。可能会导致泄漏。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。