Chosen1 Chosen1 - 3 months ago 18
Android Question

Android - Cannot create ListView within custom Dialog

With my app, what happens is that when a user clicks a certain button, the custom dialog displays. The custom dialog consists of two widgets:


  • Edit Text field

  • ListView



This is supposed to work similar to how autocomplete would work. When the user has entered a certain number of characters (3) or more, I want to display a filtered ListView of the results. I get the text value from the EditText and I use it in an SQLite statement (works fine). I then add it to an ArrayList (no problem), but when I try and set the ArrayAdapter, I get the following message:

08-11 11:49:20.552 8045-8045/com.example.maindec.sme E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.maindec.sme, PID: 8045
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:401)
at android.widget.ArrayAdapter.getView(ArrayAdapter.java:369)
at android.widget.AbsListView.obtainView(AbsListView.java:2346)
at android.widget.ListView.makeAndAddView(ListView.java:1875)
at android.widget.ListView.fillDown(ListView.java:702)
at android.widget.ListView.fillFromTop(ListView.java:763)
at android.widget.ListView.layoutChildren(ListView.java:1684)
at android.widget.AbsListView.onLayout(AbsListView.java:2148)
at android.view.View.layout(View.java:16630)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
at android.view.View.layout(View.java:16630)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
at android.view.View.layout(View.java:16630)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
at android.view.View.layout(View.java:16630)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
at android.view.View.layout(View.java:16630)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
at android.view.View.layout(View.java:16630)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
at android.view.View.layout(View.java:16630)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
at com.android.internal.policy.PhoneWindow$DecorView.onLayout(PhoneWindow.java:2678)
at android.view.View.layout(View.java:16630)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2171)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1931)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
at android.view.Choreographer.doCallbacks(Choreographer.java:670)
at android.view.Choreographer.doFrame(Choreographer.java:606)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
08-11 11:52:36.596 8045-8053/com.example.maindec.sme W/art: Suspending all threads took: 5.267ms
08-11 11:54:20.640 8045-8045/com.example.maindec.sme I/Process: Sending signal. PID: 8045 SIG: 9


That message only display when I try to set the adapter. I've debugged and this is definitely the problem.

Here's my code:

final AlertDialog.Builder builder = new AlertDialog.Builder(this);

builder.setTitle("Smoke weed everyday...");

builder.setPositiveButton("Try Again", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//dialog.cancel();
}
});


builder.setView(getLayoutInflater().inflate(R.layout.item_list_dialog, null));



final Dialog dialog = builder.create();

// THIS IS THE NAME OF THE BUTTON
itemListSelection.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

dialog.setCanceledOnTouchOutside(false);

dialog.show();

final EditText itemNumberInputField = (EditText) dialog.findViewById(R.id.item_number_input_field);

final ListView filteredItemNumberList = (ListView) dialog.findViewById(R.id.item_number_list);

itemNumberInputField.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {

}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
currentValue = itemNumberInputField.getText().toString();

smeDatabase = openOrCreateDatabase("SME", MODE_PRIVATE, null);
if (currentValue.length() > 2) {
keyNumbersList.clear();
Cursor itemResultSet = smeDatabase.rawQuery("SELECT * " +
"FROM item_list", null);

for (int i = 0; i < itemResultSet.getCount(); i++) {
itemResultSet.moveToPosition(i);
keyNumbersList.add(itemResultSet.getString(0));
System.out.println("Test: " + itemResultSet.getString(0));
}
ArrayAdapter<String> itemNumbersAdapter = new ArrayAdapter<String>(NewKeyNumber.this, android.R.layout.simple_list_item_1, keyNumbersList);
//THIS IS THE LINE THAT FAILS filteredItemNumberList.setAdapter(itemNumbersAdapter);
}
}

@Override
public void afterTextChanged(Editable s) {

}

});
}
});


Any help with this would be appreciated. Thanks.

Answer

It turned out that the reason why it kept failing was because I had a null value inside my ArrayList, so I did this to solve it:

if (currentItem != null) {
          keyNumbersList.add(itemResultSet.getString(0));
          System.out.println("Test: " + itemResultSet.getString(0));
}

Filtering out any null values. Works great.