AutoCompleteTextView sqlite填充异常 [英] AutoCompleteTextView sqlite poplulation exception
问题描述
我的第一篇文章只有不到一半的文字,因此是第二篇(完整的)文章.
My frist post only had less than half my text hence the second (complete) post.
我正在使用 sqlite 开发一个测试应用程序来填充两个 AutoCompleteTextView 我正在使用汽车制造商和模型进行测试
I'm working on a test app using sqlite to populate two AutoCompleteTextView's i'm using car make and model for the test
自动完成:
自动完成
模型自动完成
makeAutocomplete 的列表是从 sql 查询中填充的,并且工作正常
makeAutocomplete's list is populated from a sql query and it works fine
选择 make 时填充第二个
the second is populated when the make is selected
makeAutoComplete.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
final String[] makeSelected = {arg0.getItemAtPosition(arg2).toString()};
final String[] modelDeal = sqlDBModel.getAllModelFilter(makeSelected);
ArrayAdapter<String> modelAdapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_dropdown_item_1line, modelDeal);
initModelAutoComplete(modelAdapter);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
initModelAutoComplete 声明
public void initModelAutoComplete(ArrayAdapter<String> adapter){
//adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, list);
modelAutoComplete.setAdapter(adapter);
modelAutoComplete.setThreshold(1);
modelAutoComplete.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3){
arg0.getItemAtPosition(arg2);
}
});
}
SQLiteModelSearch.getAllModelFilter 声明
public String[] getAllModelFilter(String[] vehiclemake){
if(vehiclemake != null){
Cursor cursor = this.sqliteDBInstance.query(DB_MAKEMODEL_TABLE,
new String[]{DB_COLUMN_MAKE, DB_COLUMN_MODEL},
DB_COLUMN_MAKE+"=?",
vehiclemake,
null,
null,
null,
null);
if( cursor != null){
String[] str = new String[cursor.getCount()];
int i = 0;
while(cursor.moveToNext()){
str[i] = cursor.getString(cursor.getColumnIndex(DB_COLUMN_MODEL));
i++;
}
return str;
} else {
Log.i("vehiclemake = ", "NULL");
return new String[]{};
}
}
return new String[]{};
}
LogCat
07-31 13:00:19.631:E/AndroidRuntime(1302):在android.app.ActivityThread.main(ActivityThread.java:3683)
07-31 13:00:19.631: E/AndroidRuntime(1302): at android.app.ActivityThread.main(ActivityThread.java:3683)
07-31 13:00:19.631:E/AndroidRuntime(1302):在java.lang.reflect.Method.invokeNative(Native Method) 07-3113:00:19.631:E/AndroidRuntime(1302):致命异常:主要
07-31 13:00:19.631: E/AndroidRuntime(1302): at java.lang.reflect.Method.invokeNative(Native Method) 07-31 13:00:19.631: E/AndroidRuntime(1302): FATAL EXCEPTION: main
07-31 13:00:19.631: E/AndroidRuntime(1302):java.lang.NullPointerException
07-31 13:00:19.631: E/AndroidRuntime(1302): java.lang.NullPointerException
07-31 13:00:19.631:E/AndroidRuntime(1302):在com.myapp.sqltest.database.SQLiteModelSearch.getAllModelFilter(SQLiteModelSearch.java:100)
07-31 13:00:19.631: E/AndroidRuntime(1302): at com.myapp.sqltest.database.SQLiteModelSearch.getAllModelFilter(SQLiteModelSearch.java:100)
07-31 13:00:19.631:E/AndroidRuntime(1302):在com.myapp.sqltest.activity.addVehicleActivity$2.onItemSelected(addVehicleActivity.java:62)
07-31 13:00:19.631: E/AndroidRuntime(1302): at com.myapp.sqltest.activity.addVehicleActivity$2.onItemSelected(addVehicleActivity.java:62)
07-31 13:00:19.631:E/AndroidRuntime(1302):在android.widget.AdapterView.fireOnSelected(AdapterView.java:871)
07-31 13:00:19.631: E/AndroidRuntime(1302): at android.widget.AdapterView.fireOnSelected(AdapterView.java:871)
07-31 13:00:19.631:E/AndroidRuntime(1302):在android.widget.AdapterView.access$200(AdapterView.java:42)
07-31 13:00:19.631: E/AndroidRuntime(1302): at android.widget.AdapterView.access$200(AdapterView.java:42)
07-31 13:00:19.631:E/AndroidRuntime(1302):在android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:837)
07-31 13:00:19.631: E/AndroidRuntime(1302): at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:837)
07-31 13:00:19.631:E/AndroidRuntime(1302):在android.os.Handler.handleCallback(Handler.java:587)
07-31 13:00:19.631: E/AndroidRuntime(1302): at android.os.Handler.handleCallback(Handler.java:587)
07-31 13:00:19.631:E/AndroidRuntime(1302):在android.os.Handler.dispatchMessage(Handler.java:92)
07-31 13:00:19.631: E/AndroidRuntime(1302): at android.os.Handler.dispatchMessage(Handler.java:92)
07-31 13:00:19.631:E/AndroidRuntime(1302):在android.os.Looper.loop(Looper.java:130)
07-31 13:00:19.631: E/AndroidRuntime(1302): at android.os.Looper.loop(Looper.java:130)
07-31 13:00:19.631:E/AndroidRuntime(1302):在java.lang.reflect.Method.invoke(Method.java:507)
07-31 13:00:19.631: E/AndroidRuntime(1302): at java.lang.reflect.Method.invoke(Method.java:507)
07-31 13:00:19.631:E/AndroidRuntime(1302):在com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-31 13:00:19.631: E/AndroidRuntime(1302): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-31 13:00:19.631:E/AndroidRuntime(1302):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-31 13:00:19.631: E/AndroidRuntime(1302): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-31 13:00:19.631:E/AndroidRuntime(1302):在dalvik.system.NativeStart.main(本机方法)
07-31 13:00:19.631: E/AndroidRuntime(1302): at dalvik.system.NativeStart.main(Native Method)
我可以看到正在抛出异常:
I can see that the exception is being thrown at:
Cursor cursor = this.sqliteDBInstance.query(DB_MAKEMODEL_TABLE,
new String[]{DB_COLUMN_MAKE, DB_COLUMN_MODEL},
DB_COLUMN_MAKE+"=?",
vehiclemake,
null,
null,
null,
null);
但不知道为什么,我观察了所有进入函数的变量,但没有一个为空?
but can't tell why, i've watched all the variables going into the functions and none of them are null?
推荐答案
我已经想通了.问题是我已经定义了
I've figured it out. the issue was that I had defined
public AutoCompleteTextView modelAutoComplete;
public AutoCompleteTextView makeAutoComplete;
然后在函数 onCreate
Then then in function onCreate
我已经定义:
AutoCompleteTextView modelAutoComplete = (AutoCompleteTextView) findViewById(R.id.autoCompleteModel);
AutoCompleteTextView makeAutoComplete = (AutoCompleteTextView) findViewById(R.id.autoCompleteModel);
我忽略了变量范围,因为 makeAutoComplete 和 modelAutoComplete 都定义了两次,一个是全局类变量,第二个是局部函数变量.
I overlooked variable scope since both makeAutoComplete and modelAutoComplete where defined twice, one as a global class variable, and the second as a local function variable.
因为全局变量只是被定义而不是被初始化.函数 initModelAutoComplete() 正在引用导致 NPE 异常的全局公共变量 modelAutoComplete.
since the global variable was only defined and not initialized. The function initModelAutoComplete() was referencing the global public variable modelAutoComplete causing the NPE exception.
这篇关于AutoCompleteTextView sqlite填充异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!