AutoCompleteView在游标上崩溃

时间:2012-08-01 17:26:49

标签: android sqlite

我的帖子只有不到一半的文字,因此是第二个(完整的)帖子。

我正在使用sqlite开发测试应用程序来填充两个AutoCompleteTextView我正在使用汽车品牌和模型进行测试

自动完成的:

makeAutocomplete

modelAutocomplete

makeAutocomplete的列表是从SQL查询填充的,它可以正常工作

选择make时会填充第二个

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):at   android.app.ActivityThread.main(ActivityThread.java:3683)

     

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):致命异常:主

     

07-31 13:00:19.631:E / AndroidRuntime(1302):   显示java.lang.NullPointerException

     

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):at   com.myapp.sqltest.activity.addVehicleActivity $ 2.onItemSelected(addVehicleActivity.java:62)

     

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):at   android.widget.AdapterView.access $ 200(AdapterView.java:42)

     

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):at   android.os.Handler.handleCallback(Handler.java:587)

     

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):at   android.os.Looper.loop(Looper.java:130)

     

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):at   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.main(ZygoteInit.java:597)

     

07-31 13:00:19.631:E / AndroidRuntime(1302):at   dalvik.system.NativeStart.main(原生方法)

我可以看到异常被抛出:

Cursor cursor = this.sqliteDBInstance.query(DB_MAKEMODEL_TABLE,
                                                    new String[]{DB_COLUMN_MAKE, DB_COLUMN_MODEL},
                                                    DB_COLUMN_MAKE+"=?",
                                                    vehiclemake,
                                                    null,
                                                    null,
                                                    null,
                                                    null);

但是不知道为什么,我已经看过所有变量进入函数而且没有一个是空的?

1 个答案:

答案 0 :(得分:0)

我已经弄清楚了。问题是我已经定义了

public AutoCompleteTextView modelAutoComplete;
public AutoCompleteTextView makeAutoComplete;

然后在函数onCreate

我定义了:

AutoCompleteTextView modelAutoComplete = (AutoCompleteTextView) findViewById(R.id.autoCompleteModel);
AutoCompleteTextView makeAutoComplete = (AutoCompleteTextView) findViewById(R.id.autoCompleteModel);

我忽略了变量范围,因为makeAutoComplete和modelAutoComplete都定义了两次,一次作为全局类变量,第二次作为局部函数变量。

因为全局变量只是定义而未初始化。函数initModelAutoComplete()引用了全局公共变量modelAutoComplete,导致NPE异常。