对话框中的AutoCompleteView

时间:2016-01-29 12:51:20

标签: android sqlite autocompletetextview

我正在尝试在我的一个对话框中实现AutoCompleteView。我一直在关注一些教程,但似乎无法看到我什么时候出错。当我单击按钮启动对话框时,应用程序崩溃时出现以下错误。 AutoCompleteView是否正确?

错误

01-29 12:47:59.018 5481-5481/com.example.rory.prototypev2 E/AndroidRuntime: FATAL EXCEPTION: main
01-29 12:47:59.018 5481-5481/com.example.rory.prototypev2 E/AndroidRuntime: Process: com.example.rory.prototypev2, PID: 5481
01-29 12:47:59.018 5481-5481/com.example.rory.prototypev2 E/AndroidRuntime: java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.Cursor android.database.sqlite.SQLiteDatabase.query(java.lang.String, java.lang.String[], java.lang.String, java.lang.String[], java.lang.String, java.lang.String, java.lang.String)' on a null object reference
01-29 12:47:59.018 5481-5481/com.example.rory.prototypev2 E/AndroidRuntime:     at com.example.rory.prototypev2.DBMain.getIngredientsForInput(DBMain.java:418)
01-29 12:47:59.018 5481-5481/com.example.rory.prototypev2 E/AndroidRuntime:     at com.example.rory.prototypev2.enterRecipe$1.onClick(enterRecipe.java:72)
01-29 12:47:59.018 5481-5481/com.example.rory.prototypev2 E/AndroidRuntime:     at android.view.View.performClick(View.java:5204)
01-29 12:47:59.018 5481-5481/com.example.rory.prototypev2 E/AndroidRuntime:     at android.view.View$PerformClick.run(View.java:21153)
01-29 12:47:59.018 5481-5481/com.example.rory.prototypev2 E/AndroidRuntime:     at android.os.Handler.handleCallback(Handler.java:739)
01-29 12:47:59.018 5481-5481/com.example.rory.prototypev2 E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:95)
01-29 12:47:59.018 5481-5481/com.example.rory.prototypev2 E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:148)
01-29 12:47:59.018 5481-5481/com.example.rory.prototypev2 E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5417)
01-29 12:47:59.018 5481-5481/com.example.rory.prototypev2 E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
01-29 12:47:59.018 5481-5481/com.example.rory.prototypev2 E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
01-29 12:47:59.018 5481-5481/com.example.rory.prototypev2 E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

使用AutoCompleteView对话

        ingredient = (Button) findViewById(R.id.showIngredientDialog);
    ingredient.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

            db1.open();
            String filler = "Filler";
            recipe_number = db1.insertRecipe(filler);

            // custom ingredient_dialog
            final Dialog dialog = new Dialog(context);
            dialog.setContentView(R.layout.ingredient_dialog);
            dialog.setTitle("Add Ingredient");

            // set the custom ingredient_dialog components
            //final EditText ingredient = (EditText) dialog.findViewById(R.id.name);

            String[] list = db1.getIngredientsForInput();
            text = (AutoCompleteTextView)findViewById(R.id.name);
            ArrayAdapter adapter3 = new ArrayAdapter(getApplicationContext(),android.R.layout.simple_list_item_1,list);
            text.setAdapter(adapter3);
            text.setThreshold(1);

对话框XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<AutoCompleteTextView
    android:id="@+id/name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:ems="10"
    android:layout_marginTop="72dp"
    android:hint="AutoComplete TextView">
    <requestFocus />
</AutoCompleteTextView>

<Spinner
    android:id="@+id/measurement"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/name"
    android:layout_alignParentStart="true"
    android:entries="@array/measurements"/>

<Spinner
    android:id="@+id/unit"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/measurement"
    android:layout_alignParentStart="true"
    android:entries="@array/units"/>

<Button
    android:id="@+id/dialogButtonNext"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Next"
    android:layout_below="@+id/unit"
    android:layout_toStartOf="@+id/dialogButtonOK" />

<Button
    android:id="@+id/dialogButtonOK"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text=" Ok "
    android:layout_marginRight="5dp"
    android:layout_below="@+id/unit"
    android:layout_toEndOf="@+id/name"
    android:gravity="center"/>

getIngredientsForInput()

    public String[] getIngredientsForInput()
{
    Cursor cursor = this.sqliteDBInstance.query(CONTENTS_TABLE, new String[] {KEY_CONTENTS_NAME}, null, null, null, null, null);

    if(cursor.getCount() >0)
    {
        String[] str = new String[cursor.getCount()];
        int i = 0;

        while (cursor.moveToNext())
        {
            str[i] = cursor.getString(cursor.getColumnIndex(KEY_CONTENTS_NAME));
            i++;
        }
        return str;
    }
    else
    {
        return new String[] {};
    }
}

1 个答案:

答案 0 :(得分:1)

由于 AutocompleteTextView 在对话框布局中定义 ,因此您应该执行以下操作以避免NPE

text = (AutoCompleteTextView)findViewById(R.id.name);替换为

text = (AutoCompleteTextView)dialog.findViewById(R.id.name);