2014-02-12 49 views
-2

我正在構建一個在運行時創建6個編輯文本的程序,並且我想將用戶在這些edittext中輸入的數據保存到sqlite數據庫中,但是出現錯誤。我提供了創建我的編輯文本的代碼,我希望我能得到一些幫助。第二種情況出現錯誤。如何在運行時將動態創建的edittext的數據保存到sqlite數據庫?

the edittext is may have not been initialized. 



    @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      switch(v.getId()){ 
      case R.id.buttonAddIntervention: 

       final TableLayout table = (TableLayout)findViewById(R.id.tableinterventions); 

       EditText ed = new EditText(this); 
       TextView tv = new TextView(this); 
       final TableRow tabr = new TableRow(this); 
       tabr.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)); 
       tv.setText("Code"); 
       ed.setHint("Code"); 
       ed.setText("", null); 
       ed.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)); 
       tv.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)); 
       tabr.addView(tv); 
       tabr.addView(ed); 
       table.addView(tabr, new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)); 

       EditText ed1 = new EditText(this); 
       TextView tv1 = new TextView(this); 
       Button b1 = new Button(this); 
       final TableRow tabr1 = new TableRow(this); 
       tabr1.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)); 
       b1.setText("-"); 
       tv1.setText("Start Time"); 
       ed1.setHint("Start Time"); 
       ed1.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)); 
       tv1.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)); 
       b1.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)); 
       tabr1.addView(tv1); 
       tabr1.addView(ed1); 
       tabr1.addView(b1); 
       table.addView(tabr1, new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)); 




       EditText ed2 = new EditText(this); 
       TextView tv2 = new TextView(this); 
       final TableRow tabr2 = new TableRow(this); 
       tabr2.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)); 
       tv2.setText("End Time"); 
       ed2.setHint("End Time"); 
       ed2.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)); 
       tv2.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)); 
       tabr2.addView(tv2); 
       tabr2.addView(ed2); 
       table.addView(tabr2, new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)); 

       EditText ed3 = new EditText(this); 
       TextView tv3 = new TextView(this); 
       final TableRow tabr3 = new TableRow(this); 
       tabr3.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)); 
       tv3.setText("Description"); 
       ed3.setHint("Description"); 
       ed3.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)); 
       tv3.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)); 
       tabr3.addView(tv3); 
       tabr3.addView(ed3); 
       table.addView(tabr3,new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)); 

       EditText ed4 = new EditText(this); 
       TextView tv4 = new TextView(this); 
       final TableRow tabr4 = new TableRow(this); 
       tabr4.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)); 
       tv4.setText("Product"); 
       ed4.setHint("Product"); 
       ed4.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)); 
       tv4.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)); 
       tabr4.addView(tv4); 
       tabr4.addView(ed4); 
       table.addView(tabr4,new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)); 

       EditText ed5 = new EditText(this); 
       TextView tv5 = new TextView(this); 
       final TableRow tabr5 = new TableRow(this); 
       tabr5.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)); 
       tv5.setText("Serial Number"); 
       ed5.setHint("Serial Number"); 
       ed5.setLayoutParams(new TableRow.LayoutParams(190,LayoutParams.WRAP_CONTENT)); 
       tv5.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)); 
       tabr5.addView(tv5); 
       tabr5.addView(ed5); 
       table.addView(tabr5,new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)); 

       b1.setOnClickListener(new OnClickListener(){ 

        @Override 
        public void onClick(View v1) { 
         // TODO Auto-generated method stub 
         table.removeView(tabr); 
         table.removeView(tabr1); 
         table.removeView(tabr2); 
         table.removeView(tabr3); 
         table.removeView(tabr4); 
         table.removeView(tabr5); 


        } 

       }); 
       break; 

case R.id.BtnTestSaveAll: 

      InterventionsDatabase idb1 = new InterventionsDatabase(); 


     //the error is here the ed,ed1,ed2,ed3,ed4 and ed5 may not have been initialized 
      String cod= String.valueOf(ed.getText()); 
      String s_time = String.valueOf(ed1.getText().toString()); 
      String e_time = String.valueOf(ed2.getText().toString()); 
      String d = String.valueOf(ed3.getText().toString()); 
      String p = String.valueOf(ed4.getText().toString()); 
      String Sn = String.valueOf(ed5.getText().toString()); 


      idb1.setEmp_code(Integer.parseInt(cod)); 
      idb1.setEmp_startTime(Integer.parseInt(s_time)); 
      idb1.setEmp_endTime(Integer.parseInt(e_time)); 
      idb1.setDescription(d); 
      idb1.setProduct(p); 
      idb1.setSerialNumber(Integer.parseInt(Sn)); 

      Dbhelper.createInterventionsDB(Integer.parseInt(cod),Integer.parseInt(s_time),Integer.parseInt(e_time), d, p,Integer.parseInt(Sn)); 
      Toast.makeText(getApplicationContext(), "Yeah", Toast.LENGTH_SHORT).show(); 
      break; 
+4

請張貼相關的代碼和你得到的錯誤。 – laalto

+0

請發表您的代碼 – Ameer

+0

我該如何在這裏發佈代碼? –

回答

0

你必須讓視圖中的所有的EditText的第一,然後做你想做的事情是這樣的:

ViewGroup group = (ViewGroup)findViewById(R.id.your_layout); 
for (int i = 0, count = group.getChildCount(); i < count; ++i) { 
    View view = group.getChildAt(i); 
    if (view instanceof EditText) { 
     ((EditText)view).setText(""); 
    } 
} 
+0

你現在可以檢查代碼嗎? –

2

「可能尚未初始化」,當您要訪問的對象出現錯誤。 ..可能尚未初始化。這意味着,例如,該代碼將無法正常工作:

int a = 5; 
EditText et; 

if(a == 5) { 
    et = new EditText(); 
} 

et.setText("some text"); 

它不會工作,因爲et是內部if語句,條件在這種情況下,當然是經過初始化的,但是編譯器不知道那。向您表明您在此處犯了錯誤,而不是最終讓應用程序在運行時崩潰,這樣更安全。

編輯:

你初始化在EditTextButtononClick()(因此點擊Button時),你要訪問到其他地方(點擊其他Button時)。編譯器阻止了這一點,因爲它必須確保EditText被初始化,它不能依賴用戶先單擊初始化按鈕。

爲了解決此問題,創建這些EditText的是你Activity類的領域和onCreate()初始化它們,就沒有必要在單擊Button當做到這一點。就是這樣。

+0

非常感謝@laalto只是說:)但感謝您的幫助:) –

+0

@Jadballout不客氣!考慮接受對你最有幫助的答案。 –

+0

是的,但非常感謝,我的意思是it.But我想他直接得到了我的意思,因爲我不知道如何說清楚。 –

1

構建這樣的:

switch (var) { 
    case 1: 
    SomeType foo = new SomeType(); 
    // ... 
    break; 

    case 2: 
    foo.something(); 
    // ... 
    break; 
} 

有問題,當case 2正在執行foo未初始化。變量在其聲明的曲線{}範圍內可見,即在這種情況下爲switch。但是,採用情況2時的代碼執行路徑不包括變量的初始化。編譯器很聰明,可以注意到這一點,並給你一個警告。

在你的情況很好的解決方案是移動EditText變量聲明類會員級別:引用時,他們

private EditText mEd1; 

// ... 

    mEd1 = new EditText(this); 

,並檢查非空

if (mEd1 != null) { 
    // ... 
    String s_time = String.valueOf(mEd1.getText().toString()); 
+0

謝謝你,我現在試試這個 –

+0

非常感謝它現在的工作:)再次感謝 –

相關問題