本文介绍了AutoCompleteView崩溃光标处的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的弗里斯特后只有不到一半我的文字,因此第二个(完成)后。

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

makeAutocomplete

modelAutocomplete

modelAutocomplete

makeAutocomplete的名单是从SQL查询填充和正常工作

makeAutocomplete's list is populated from a sql query and it works fine

在选择使第二填充

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中

13 07-31:00:19.631:E / AndroidRuntime(1302):在
  android.app.ActivityThread.main(ActivityThread.java:3683)

13 07-31:00:19.631:E / AndroidRuntime(1302):在
  java.lang.reflect.Method.invokeNative(本机方法)07-31
  13: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

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

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

13 07-31: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)

13 07-31: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)

13 07-31: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)

13 07-31: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)

13 07-31: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)

13 07-31: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)

13 07-31: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)

13 07-31: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)

13 07-31: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)

13 07-31: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)

13 07-31: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)

13 07-31: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()被引用的全球公共变量modelAutoComplete导致NPE例外。

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

这篇关于AutoCompleteView崩溃光标处的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 08:42