帮助我修复此代码 来自AutocompleteTextview我看不到这部分的错误代码
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;
}
}
这是日志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)
答案 0 :(得分:0)
此代码块中可能的修改,因为您错过了在do ... while循环中创建字符串对象ArrayData[i] = new String()
。
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;
}
}
答案 1 :(得分: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
将 false ,并且 if
中的所有代码都不会被执行。因此,ArrayData将为null(空指针异常的可能原因)。
如果没有数据或者在try块中捕获了另一个异常,修改后的代码将返回一个空数组(0个元素)。
您也可以使用cursor.getPosition()
来使用计数器/索引,这实际上是相同的(根据注释掉的行)。