幫助我如何從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)
你可以嘗試縮短你發佈到[mcve]的代碼量嗎? – LW001
可能的重複[什麼是NullPointerException,以及如何解決它?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it ) – 0X0nosugar