我有一个自己工作良好的ListView,但由于某种原因,添加搜索栏似乎会破坏一切。我可以打开ListView,点击一个项目进入一个新的页面,但是按一个键输入搜索栏的结果,结果是“不幸的是,我的应用程序已经停止了”。我正在从logcat那里得到一个NullPointerException,但是我做的任何事情似乎都无法修复它。任何帮助都将不胜感激!
listview活动:
public class SecondScreenActivity extends Activity
{
// Listview Adapter
ArrayAdapter<String> arrayAdapter;
// Search EditText
EditText inputSearch;
ArrayList<String> RecipeList;
public void onCreate(Bundle saveInstanceState)
{
super.onCreate(saveInstanceState);
setContentView(R.layout.main);
// Get the reference of ListViewRecipes
ListView RecipeListView=(ListView)findViewById(R.id.mainListView);
RecipeList = new ArrayList<String>();
getRecipeNames();
RecipeListView = (ListView) findViewById(R.id.mainListView);
inputSearch = (EditText) findViewById(R.id.inputSearch);
// Adding items to listview
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, RecipeList);
//^^^this is the line that seems to be the problem^^^
// Set The Adapter
RecipeListView.setAdapter(arrayAdapter);
//Search
inputSearch = (EditText) findViewById(R.id.inputSearch);
/**
* Enabling Search Filter
* */
inputSearch.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changes the Text
SecondScreenActivity.this.arrayAdapter.getFilter().filter(cs);
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
// register onClickListener to handle click events on each item
RecipeListView.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View itemClicked,
int position, long id) {
TextView textView = (TextView) itemClicked;
String strText = textView.getText().toString();
Intent intenttwo = new Intent(SecondScreenActivity.this, ThirdScreenActivity.class);
intenttwo.putExtra("position", strText);
startActivity(intenttwo);
}
});
}
void getRecipeNames()
{
RecipeList.add("Recipe1");
RecipeList.add("Recipe2");
RecipeList.add("Recipe3");
RecipeList.add("Recipe4");
RecipeList.add("Recipe5");
RecipeList.add("Recipe6");
RecipeList.add("Recipe7");
RecipeList.add("Recipe8");
RecipeList.add("Recipe9");
RecipeList.add("Recipe10");
}
}编辑:--这是一个稍微修改过的listview活动。这使得搜索栏正常工作,但是当单击ListView中的项时,应用程序被迫关闭,而logcat错误:java.lang.classcastexception: android.widget.linearlayout不能转换为android.widget.textview。这可能有帮助,但可能没有帮助!
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
public class SecondScreenActivity extends Activity
{
// List view
private ListView lv;
// Listview Adapter
ArrayAdapter<String> adapter;
// Search EditText
EditText inputSearch;
// ArrayList for Listview
ArrayList<HashMap<String, String>> recipeList;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Listview Data
String recipes[] = {"Recipe1", "Recipe2", "Recipe3", "Recipe4", "Recipe5",
"Recipe6", "Recipe7", "Recipe8", "Recipe9", "Recipe10"};
lv = (ListView) findViewById(R.id.mainListView);
inputSearch = (EditText) findViewById(R.id.inputSearch);
// Adding items to listview
adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.recipe_name, recipes);
lv.setAdapter(adapter);
// Enabling search filter
inputSearch.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
SecondScreenActivity.this.adapter.getFilter().filter(cs);
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
// register onClickListener to handle click events on each item
lv.setOnItemClickListener(new OnItemClickListener()
{
// argument position gives the index of item which is clicked
public void onItemClick(AdapterView<?> parent, View itemClicked,
int position, long id) {
//TO DO: Send string (position) of recipe name
// TextView textView = (TextView) findViewById(itemClicked);
// String stringName = textView.getText().toString();
String stringName = (String) (lv.getItemAtPosition(position));
Intent intenttwo = new Intent(SecondScreenActivity.this, ThirdScreenActivity.class);
intenttwo.putExtra("position", stringName);
startActivity(intenttwo);
}
});
}
}和我的logcat (当使用第一个活动时):
06-25 22:37:54.353: E/AndroidRuntime(4646): FATAL EXCEPTION: main
06-25 22:37:54.353: E/AndroidRuntime(4646): java.lang.NullPointerException
06-25 22:37:54.353: E/AndroidRuntime(4646): at com.example.myapp.SecondScreenActivity$1.onTextChanged(SecondScreenActivity.java:66)
06-25 22:37:54.353: E/AndroidRuntime(4646): at android.widget.TextView.sendOnTextChanged(TextView.java:7231)
06-25 22:37:54.353: E/AndroidRuntime(4646): at android.widget.TextView.handleTextChanged(TextView.java:7290)
06-25 22:37:54.353: E/AndroidRuntime(4646): at android.widget.TextView$ChangeWatcher.onTextChanged(TextView.java:8880)
06-25 22:37:54.353: E/AndroidRuntime(4646): at android.text.SpannableStringBuilder.sendTextChanged(SpannableStringBuilder.java:962)
06-25 22:37:54.353: E/AndroidRuntime(4646): at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:496)
06-25 22:37:54.353: E/AndroidRuntime(4646): at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:435)
06-25 22:37:54.353: E/AndroidRuntime(4646): at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:30)
06-25 22:37:54.353: E/AndroidRuntime(4646): at android.text.method.QwertyKeyListener.onKeyDown(QwertyKeyListener.java:222)
06-25 22:37:54.353: E/AndroidRuntime(4646): at android.text.method.TextKeyListener.onKeyDown(TextKeyListener.java:136)
06-25 22:37:54.353: E/AndroidRuntime(4646): at android.widget.TextView.doKeyDown(TextView.java:5385)
06-25 22:37:54.353: E/AndroidRuntime(4646): at android.widget.TextView.onKeyDown(TextView.java:5204)
06-25 22:37:54.353: E/AndroidRuntime(4646): at android.view.KeyEvent.dispatch(KeyEvent.java:2609)
06-25 22:37:54.353: E/AndroidRuntime(4646): at android.view.View.dispatchKeyEvent(View.java:7205)
06-25 22:37:54.353: E/AndroidRuntime(4646): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1359)
06-25 22:37:54.353: E/AndroidRuntime(4646): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1359)
06-25 22:37:54.353: E/AndroidRuntime(4646): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1359)
06-25 22:37:54.353: E/AndroidRuntime(4646): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1359)
06-25 22:37:54.353: E/AndroidRuntime(4646): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:1920)
06-25 22:37:54.353: E/AndroidRuntime(4646): at com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1395)
06-25 22:37:54.353: E/AndroidRuntime(4646): at android.app.Activity.dispatchKeyEvent(Activity.java:2370)
06-25 22:37:54.353: E/AndroidRuntime(4646): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1847)
06-25 22:37:54.353: E/AndroidRuntime(4646): at android.view.ViewRootImpl.deliverKeyEventPostIme(ViewRootImpl.java:3701)
06-25 22:37:54.353: E/AndroidRuntime(4646): at android.view.ViewRootImpl.handleImeFinishedEvent(ViewRootImpl.java:3651)
06-25 22:37:54.353: E/AndroidRuntime(4646): at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:2818)
06-25 22:37:54.353: E/AndroidRuntime(4646): at android.os.Handler.dispatchMessage(Handler.java:99)
06-25 22:37:54.353: E/AndroidRuntime(4646): at android.os.Looper.loop(Looper.java:137)
06-25 22:37:54.353: E/AndroidRuntime(4646): at android.app.ActivityThread.main(ActivityThread.java:5041)
06-25 22:37:54.353: E/AndroidRuntime(4646): at java.lang.reflect.Method.invokeNative(Native Method)
06-25 22:37:54.353: E/AndroidRuntime(4646): at java.lang.reflect.Method.invoke(Method.java:511)
06-25 22:37:54.353: E/AndroidRuntime(4646): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-25 22:37:54.353: E/AndroidRuntime(4646): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-25 22:37:54.353: E/AndroidRuntime(4646): at dalvik.system.NativeStart.main(Native Method)谢谢你的帮助!
发布于 2013-06-26 00:44:03
这个答案应该对你有帮助,它有一个很好的例子。我相信,如果您使用的是看起来不像getFilter()的Custom Adapter,则需要重写适配器中的Custom Adapter方法。
在这种情况下,您可以使用setOnQueryTextListener并将其实现如下
searchField = (SearchView) findViewById(R.id.search_plate);
searchField.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
// search goes here !!
setFilters("searchtext", newText);
return false;
}
});其中setFilters()是您定义的函数。这是我的一个例子,所以你的变量会有一点不同。
SearchView文档
https://stackoverflow.com/questions/17308930
复制相似问题