edittext声明为final,settext不起作用

时间:2017-03-02 19:20:08

标签: java android nullpointerexception android-edittext settext

我有很多edittext来显示DatePickerDialog,所以我创建了一个方法和edittext作为参数。然后Java说,当从内部类中访问时,参数应该是最终的。

但是当我运行应用程序时,它停在edittext.settext()我怀疑edittext作为最后一个是问题,因为我有另一个代码不能使edittext作为参数运行良好。

这是代码:

sp

这是错误:

     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;

}

这就是我实施的方式:

在onCreate之外的状态:

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; 
单击

editText:

DPD1 = setDateTanggalBeri(textTgl1);
DPD2 = setDateTanggalBeri(textTgl2);
DPD3 = setDateTanggalBeri(textTgl3);
DPD4 = setDateTanggalBeri(textTgl4);
DPD5 = setDateTanggalBeri(textTgl5);
....

2 个答案:

答案 0 :(得分:3)

由于您说EditText位于同一Activity,我建议将其设为类私有对象。

示例:

public class MainActivity extends AppCompatActivity {

    private EditText editTanggal; //declaration

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        ...
        //Initiate the EditText here
        //Example:
        //editTanggal = (EditText) findViewById(R.id.editText);
        ...
    }

    ...

    public DatePickerDialog setDateTanggalBeri() {

        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())); //Only use
            }
        }, newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));

        return datePickTgl;
    }
}

这样做,EditText不一定是finalsetDateTanggalBeri()方法可以以相同的方式工作。

我希望它有所帮助。

答案 1 :(得分:0)

最终,我解决了来自nullPointerException文档的问题。谢谢你们所有人,所以我有线索发现这个问题。以前,我在2个地方声明editText,在oncreate之外,再在inside方法之外。原来,editText在方法redeclare中,并使其为null或其他方法无法访问。

更清楚地说,这里是解决方案之前和之后:

之前:

EditText textTgl1;
EditText textTgl2;
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.table_jadwal);
    ......
    setTanggalBeri()

    DPD1 = setDateTanggalBeri(textTgl1);
    DPD2 = setDateTanggalBeri(textTgl2);
    ....
}
public void setTanggalBeri(){
  EditText textTgl1 = (EditText) findViewById(R.id.editTanggal1);
  EditText textTgl2= (EditText) findViewById(R.id.editTanggal2);
}

之后:

EditText textTgl1;
EditText textTgl2;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.table_jadwal);
    ......
    setTanggalBeri()

    DPD1 = setDateTanggalBeri(textTgl1);
    DPD2 = setDateTanggalBeri(textTgl2);
    ....
}
public void setTanggalBeri(){
    textTgl1 = (EditText) findViewById(R.id.editTanggal1);
    textTgl2 = (EditText) findViewById(R.id.editTanggal2);
 }