I am trying to implement a AutoCompleteView inside one of my dialog boxes. I have been following some tutorials but cant seem to see when I am going wrong. When ever I click the button to launch the dialog box the app crashes with the below error. Is the AutoCompleteView correct?
Error
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)
Dialog with 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);
Dialog XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://ift.tt/nIICcg"
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[] {};
}
}
Aucun commentaire:
Enregistrer un commentaire