我有一个单独运行的ListView,但由于某些原因添加搜索栏似乎毁了一切。我可以打开ListView并单击某个项目以进入新页面,但是按一个键键入搜索栏会导致“不幸的是,myapp已停止”。我从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 < / strong>即可。这可能有所帮助,可能不会!
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)
感谢您的帮助!
答案 0 :(得分:0)
This answer应该对你有帮助,它有一个很好的例子。我相信如果您使用的getFilter()
看起来不像您,则需要覆盖适配器中的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()
是您定义的函数。这是一个关于我的变量的例子,你的变量会有所不同。
您可能还会发现this answer和this one有用。我希望这有帮助