使用我的应用程序,会发生的情况是,当用户单击某个按钮时,会显示自定义对话框。自定义对话框由两个小部件组成:
这应该与自动完成的工作方式类似。当用户输入一定数量的字符(3)或更多时,我想显示结果的过滤ListView。我从EditText获取文本值,并在SQLite语句中使用它(工作正常)。然后我将它添加到ArrayList(没问题),但是当我尝试设置ArrayAdapter时,我收到以下消息:
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
当我尝试设置适配器时,仅显示该消息。我已经调试了,这绝对是个问题。
这是我的代码:
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) {
}
});
}
});
任何有关此的帮助将不胜感激。感谢。
答案 0 :(得分:0)
您不应该使用for迭代光标并手动获取项目位置。
您可以使用:
if(cursor.moveToFirst()){
do {
String value= cursor.getString(0);
}while (cursor.moveToNext());
}
cursor.close();
另外,定义
itemNumbersAdapter作为全局变量。
和
ArrayAdapter<String> itemNumbersAdapter = new ArrayAdapter<String>(NewKeyNumber.this, android.R.layout.simple_list_item_1, keyNumbersList);
然后只更改keyNumbersList数据。 更改keyNumbersList数据后,您必须调用 的 itemNumbersAdapter.notifyDataSetChanged(); 强>
答案 1 :(得分:0)
事实证明它之所以失败是因为我在ArrayList中有一个空值,所以我这样做是为了解决它:
if (currentItem != null) {
keyNumbersList.add(itemResultSet.getString(0));
System.out.println("Test: " + itemResultSet.getString(0));
}
过滤掉任何空值。效果很好。