我有很多edittext來顯示DatePickerDialog,所以我使用方法和edittext作爲參數。然後Java說,從內部類訪問參數應該是最終的。edittext聲明爲final,settext不起作用
但是,當我運行應用程序,它停在edittext.settext()我懷疑edittext作爲最終的問題,因爲我有另一個代碼,不會使edittext作爲參數運行良好。
這是代碼:
public DatePickerDialog setDateTanggalBeri(final EditText editTanggal){
DatePickerDialog datePickTgl;
Calendar newCalendar = Calendar.getInstance();
datePickTgl = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public void onDateSet(DatePicker datePicker, int i, int i1, int i2) {
final Calendar newDate = Calendar.getInstance();
newDate.set(i,i1,i2);
editTanggal.setText(dateFormatter.format(newDate.getTime()));
}
},newCalendar.get(Calendar.YEAR),newCalendar.get(Calendar.MONTH),newCalendar.get(Calendar.DAY_OF_MONTH));
return datePickTgl;
}
,這裏是錯誤:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.droid.anindya.jadwalimunisasibayiku, PID: 5669
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.EditText.setText(java.lang.CharSequence)' on a null object reference
at com.droid.anindya.jadwalimunisasibayiku.Display_Jadwal$1.onDateSet(Display_Jadwal.java:209)
at android.app.DatePickerDialog.onClick(DatePickerDialog.java:137)
at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:163)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
,這是怎麼實現的:
的onCreate外狀態:
DatePickerDialog DPD1;
DatePickerDialog DPD2;
DatePickerDialog DPD3;
DatePickerDialog DPD4;
DatePickerDialog DPD5;
狀態我n面的onCreate:
DPD1 = setDateTanggalBeri(textTgl1);
DPD2 = setDateTanggalBeri(textTgl2);
DPD3 = setDateTanggalBeri(textTgl3);
DPD4 = setDateTanggalBeri(textTgl4);
DPD5 = setDateTanggalBeri(textTgl5);
....
EDITTEXT上點擊:
public void tanggalBeri1Clicked(View view) {
DPD1.show();
}
public void tanggalBeri2Clicked(View view) {
DPD2.show();
}
....
你的函數'setDateTanggalBeri()'是在同一活動或你的EditText初始化片段? –
是的,它是在同一個活動。我將EditText初始化放在另一個方法中,並在onCreate中調用它。 –
你能否包含錯誤信息? –