2017-10-11 71 views
-3

幫助我如何從AutocompleteTextview解決這個代碼 我不能看到錯誤代碼從該部分EDITTEXT建議

final String [] myData = myDB.SelectAllData(); 

final AutoCompleteTextView autoCom = (AutoCompleteTextView)findViewById(R.id.TVresult); 
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.select_dialog_singlechoice,myData); 
    autoCom.setAdapter(adapter); 

而這部分

public String[] SelectAllData() { 


    try { 
     String ArrayData[] = null; 
     SQLiteDatabase db; 
     db = this.getReadableDatabase(); // Read Data 

     String strSQL = "SELECT * FROM " + DB_TABLE_NAME; 
     Cursor cursor = db.rawQuery(strSQL, null); 

     if(cursor != null) 
     { 
      if (cursor.moveToFirst()) { 
       ArrayData = new String[cursor.getCount()]; 
       /*** 
       * [x] = Name 
       */ 
       int i= 0; 
       do { 
        ArrayData[i] = cursor.getString(0); 
        i++; 
       } while (cursor.moveToNext()); 

      } 
     } 
     cursor.close(); 

     return ArrayData; 

    } catch (Exception e) { 
     return null; 
    } 

} 

這是我的MainActivity的代碼

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    resulttext = (TextView) findViewById(R.id.TVresult); 
    rescebu = (TextView) findViewById(R.id.english); 
    trans = (Button) findViewById(R.id.translate); 
    respam = (TextView) findViewById(R.id.tagalog); 
    resilo = (TextView) findViewById(R.id.vis); 
    resbik = (TextView) findViewById(R.id.ilonngo); 
    myDB = new DatabaHelper(this); 
    tts = new TextToSpeech(this, this); 
    final String [] myData = myDB.SelectAllData(); 

    final AutoCompleteTextView autoCom = (AutoCompleteTextView)findViewById(R.id.TVresult); 
    final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.select_dialog_singlechoice,myData); 
    autoCom.setAdapter(adapter); 
    autoCom.setThreshold(1); 
    autoCom.setTextColor(Color.RED); 

這是Dabasehelper和SelectAllData方法

public class DatabaHelper extends SQLiteOpenHelper { 
public static final String DATABASE_NAME = "translator4.sqlite"; 
private static final String DB_TABLE_NAME = "wews"; 
public DatabaHelper(Context context) { 

    super(context, DATABASE_NAME, null, 1); 
} 

@Override 
public void onCreate(SQLiteDatabase db) { 

} 

public Cursor getAllData(String English, String Tagalog, String Visaya, String Ilonggo) { 
    SQLiteDatabase db = this.getWritableDatabase(); 
    Cursor res = db.rawQuery("select * from wews where English like '" + English + "' or Tagalog like '" + Tagalog + "' or Visayan like '" + Visaya + "' or Ilonggo like '" + Ilonggo + "';", null); 

    return res; 
} 


@Override 
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) { 

} 


public String[] SelectAllData() { 


    try { 
     String ArrayData[] = null; 
     SQLiteDatabase db; 
     db = this.getReadableDatabase(); // Read Data 

     String strSQL = "SELECT * FROM " + DB_TABLE_NAME; 
     Cursor cursor = db.rawQuery(strSQL, null); 

     if(cursor != null) 
     { 
      if (cursor.moveToFirst()) { 
       ArrayData = new String[cursor.getCount()]; 
       /*** 
       * [x] = Name 
       */ 
       int i= 0; 
       do { 
        ArrayData[i] = cursor.getString(0); 
        i++; 
       } while (cursor.moveToNext()); 

      } 
     } 
     cursor.close(); 

     return ArrayData; 

    } catch (Exception e) { 
     return null; 
    } 

} 

這是因爲你在做錯過了創建的字符串對象ArrayData[i] = new String() logcat的

Process: application.example.com.myapplication, PID: 20663 
                          Theme: themes:{default=overlay:system, iconPack:system, fontPkg:system, com.android.systemui=overlay:system, com.android.systemui.navbar=overlay:system} 
                          java.lang.RuntimeException: Unable to start activity ComponentInfo{application.example.com.myapplication/application.example.com.myapplication.MainActivity}: java.lang.NullPointerException: storage == null 
                           at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2462) 
                           at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2522) 
                           at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363) 
                           at android.os.Handler.dispatchMessage(Handler.java:102) 
                           at android.os.Looper.loop(Looper.java:148) 
                           at android.app.ActivityThread.main(ActivityThread.java:5475) 
                           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) 
                           at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:102) 
                          Caused by: java.lang.NullPointerException: storage == null 
                           at java.util.Arrays$ArrayList.<init>(Arrays.java:38) 
                           at java.util.Arrays.asList(Arrays.java:155) 
                           at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:137) 
                           at application.example.com.myapplication.MainActivity.onCreate(MainActivity.java:56) 
                           at android.app.Activity.performCreate(Activity.java:7125) 
                           at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108) 
                           at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2415) 
                           at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2522)  
                           at android.app.ActivityThread.-wrap11(ActivityThread.java)  
                           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363)  
                           at android.os.Handler.dispatchMessage(Handler.java:102)  
                           at android.os.Looper.loop(Looper.java:148)  
                           at android.app.ActivityThread.main(ActivityThread.java:5475)  
                           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)  
                           at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:102) 
+1

你可以嘗試縮短你發佈到[mcve]的代碼量嗎? – LW001

+3

可能的重複[什麼是NullPointerException,以及如何解決它?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it ) – 0X0nosugar

回答

0

可能修改這個代碼塊... while循環日誌。

public String[] SelectAllData() { 


    try { 
     String ArrayData[] = null; 
     SQLiteDatabase db; 
     db = this.getReadableDatabase(); // Read Data 

     String strSQL = "SELECT * FROM " + DB_TABLE_NAME; 
     Cursor cursor = db.rawQuery(strSQL, null); 

     if(cursor != null) 
     { 
      if (cursor.moveToFirst()) { 
       ArrayData = new String[cursor.getCount()]; 
       /*** 
       * [x] = Name 
       */ 
       int i= 0; 
       do { 
        ArrayData[i] = new String(); 
        ArrayData[i] = cursor.getString(0); 
        i++; 
       } while (cursor.moveToNext()); 

      } 
     } 
     cursor.close(); 

     return ArrayData; 

    } catch (Exception e) { 
     return null; 
    } 

} 
+0

導致:java.lang.NullPointerException:storage == null at java.util.Arrays $ ArrayList。 (Arrays.java:38) at android.widget.ArrayAdapter上的java.util.Arrays.asList(Arrays.java:155) 。 (ArrayAdapter.java:137) – Marvenn

+0

它們再次出現錯誤 – Marvenn

+0

此行創建最終的String [] myData = myDB.SelectAllData();錯誤,因爲它返回null。檢查你的表,如果它有記錄或沒有。 –

0

我建議改變: -

public String[] SelectAllData() { 


    try { 
     String ArrayData[] = null; 
     SQLiteDatabase db; 
     db = this.getReadableDatabase(); // Read Data 

     String strSQL = "SELECT * FROM " + DB_TABLE_NAME; 
     Cursor cursor = db.rawQuery(strSQL, null); 

     if(cursor != null) 
     { 
      if (cursor.moveToFirst()) { 
       ArrayData = new String[cursor.getCount()]; 
       /*** 
       * [x] = Name 
       */ 
       int i= 0; 
       do { 
        ArrayData[i] = cursor.getString(0); 
        i++; 
       } while (cursor.moveToNext()); 

      } 
     } 
     cursor.close(); 

     return ArrayData; 

    } catch (Exception e) { 
     return null; 
    } 

} 

到: -

public String[] SelectAllData() { 

    try { 
     String ArrayData[]; 
     SQLiteDatabase db; 
     db = this.getReadableDatabase(); // Read Data 

     String strSQL = "SELECT * FROM " + DB_TABLE_NAME; 
     Cursor cursor = db.rawQuery(strSQL, null); 

     ArrayData = new String[cursor.getCount()]; 
     int i = 0; 
     while (cursor.moveToNext()) { 
      ArrayData[i++] = cursor.getString(0); 
      //ArrayData[cursor.getPosition()] = cursor.getString(0); //Could be an alternative 
     } 
     cursor.close(); 

     return ArrayData; 

    } catch (Exception e) { 
     return new String[0]; 
    } 
} 

總之,對於一個空指針檢查是沒用它並不表示沒有數據,而當沒有數據時,返回計數爲0的空遊標。

因此,當沒有數據cursor.moveToFirst和無內if的代碼將被執行。因爲這樣的ArrayData將爲空(可能的原因爲空指針異常)。

如果沒有數據或者在try塊中捕獲到另一個異常,修改的代碼將返回一個空數組(0個元素)。

您也可以使用計數器/索引,使用cursor.getPosition(),這實際上是相同的(根據註釋掉的行)。