0

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

我正在使用 sqlite 来填充两个 AutoCompleteTextView 的测试应用程序我正在使用汽车制造商和模型进行测试

自动完成的:

使自动完成

模型自动完成

makeAutocomplete 的列表是从 sql 查询中填充的,并且工作正常

选择品牌时填充第二个

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[]{};
}

日志猫

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 java.lang.reflect.Method.invokeNative(Native Method) 07-31 13:00:19.631: E/AndroidRuntime(1302): 致命异常: main

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): 在 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): 在 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): 在 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): 在 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): 在 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 dalvik.system.NativeStart.main(Native Method)

我可以看到异常被抛出:

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

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

4

1 回答 1

0

I've figured it out. the issue was that I had defined

public AutoCompleteTextView modelAutoComplete;
public AutoCompleteTextView makeAutoComplete;

Then then in function onCreate

I had defined:

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

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.

since the global variable was only defined and not initialized. The function initModelAutoComplete() was referencing the global public variable modelAutoComplete causing the NPE exception.

于 2012-08-03T23:51:09.133 回答