2017-03-02 123 views
1

我有很多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(); 
} 

.... 
+0

你的函數'setDateTanggalBeri()'是在同一活動或你的EditText初始化片段? –

+0

是的,它是在同一個活動。我將EditText初始化放在另一個方法中,並在onCreate中調用它。 –

+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()方法可以以相同的方式工作。

我希望它有幫助。

+0

但我必須使editText作爲參數,所以我可以調用多個edittext的方法。我有30個editText來顯示datepickerdialog。 –

+0

我理解你的問題,讓我在這裏閱讀文檔記住,但是你如何創建一個擴展的EditText一個** **類和在那裏實現其方法和使用它,'editTanggal.setDateTanggalBeri()' –

+0

我怎麼加我使用該方法 –

0

最終,我解決了啓發自nullPointerException文件的問題。感謝大家,所以我有線索發現問題。以前,我在2個地方聲明editText,在oncreate之外,再次在內部方法中聲明。事實證明,editText裏面的方法重新聲明,並使其爲空或無法通過其他方法達到。

更清楚,這裏是之前和解決方案後:

前:

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