我在操作栏中有一个带有可折叠视图的编辑文本。当我在编辑文本中输入基于文本观察者的名称时,它必须显示名称。它显示空指针。任何人指导我在哪里犯了错误
MainActivity.java
package com.example.filterlistview;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MenuItem.OnActionExpandListener;
import android.view.inputmethod.InputMethodManager;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
@SuppressLint("NewApi")
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class MainActivity extends Activity{
ListView lv;
CustomAdapter adapter;
ArrayList<String> name;
ArrayList<String> comp;
ArrayList<String> mobile;
ArrayList<String> address,pincode;
List<HashMap<String, String>> list;
ArrayList<MyModel> a_list = new ArrayList<MyModel>();
EditText searchView;
EditText editsearch;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final LinearLayout rel = (LinearLayout)findViewById(R.id.myrelative);
rel.setVisibility(View.VISIBLE);
name = new ArrayList<String>();
comp = new ArrayList<String>();
mobile = new ArrayList<String>();
address = new ArrayList<String>();
pincode = new ArrayList<String>();
name.add("Giri");
name.add("palani");
name.add("Lawrence");
name.add("Giri");
name.add("palani");
name.add("Lawrence");
name.add("Giri");
name.add("palani");
name.add("Lawrence");
comp.add("sri ram");
comp.add("sri ram");
comp.add("sri ram");
comp.add("sri ram");
comp.add("sri ram");
comp.add("sri ram");
comp.add("sri ram");
comp.add("sri ram");
comp.add("sri ram");
mobile.add("900121233");
mobile.add("900121233");
mobile.add("900121233");
mobile.add("900121233");
mobile.add("900121233");
mobile.add("900121233");
mobile.add("900121233");
mobile.add("900121233");
mobile.add("900121233");
for(int i=0;i<name.size();i++)
{
MyModel mp = new MyModel(name.get(i), comp.get(i), mobile.get(i));
a_list.add(mp);
}
lv = (ListView) findViewById(R.id.list);
CustomAdapter adapter= new CustomAdapter(MainActivity.this, a_list);
adapter.notifyDataSetChanged();
lv.setAdapter(adapter);
lv.setTextFilterEnabled(true);
}
@SuppressLint("NewApi")
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
editsearch = (EditText) menu.findItem(R.id.menu_search).getActionView();
// Capture Text in EditText
editsearch.addTextChangedListener(textWatcher);
// Show the search menu item in menu.xml
MenuItem menuSearch = menu.findItem(R.id.menu_search);
menuSearch.setOnActionExpandListener(new OnActionExpandListener() {
// Menu Action Collapse
@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
// Empty EditText to remove text filtering
editsearch.setText("");
editsearch.clearFocus();
return true;
}
// Menu Action Expand
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
// Focus on EditText
editsearch.requestFocus();
// Force the keyboard to show on EditText focus
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
return true;
}
});
return super.onCreateOptionsMenu(menu);
}
private TextWatcher textWatcher = new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,int arg3) {
// TODO Auto-generated method stub
}
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
String text = editsearch.getText().toString();
modellist.clear();
if (text.length() == 0) {
CustomAdapter.modellist.addAll(CustomAdapter.arraylist);
}
else
{
for (MyModel wp : CustomAdapter.arraylist)
{
if (wp.getName().toLowerCase(Locale.getDefault()).contains(text))
{
CustomAdapter.modellist.add(wp);
}
}
}
MainActivity.this.adapter.notifyDataSetChanged();
}
};
}
CustomAdapter.java
package com.example.filterlistview;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class CustomAdapter extends BaseAdapter{
ArrayList<String> name,comp,mobile,addr,pin;
Activity context;
static List<MyModel> modellist;
static ArrayList<MyModel> arraylist;
public CustomAdapter(Activity context,List<MyModel> modellist) {
super();
this.context = context;
this.modellist = modellist;
this.arraylist = new ArrayList<MyModel>();
this.arraylist.addAll(modellist);
}
private class ViewHolder {
TextView Name;
TextView Company;
TextView Mobile;
TextView Address;
TextView Pincode;
}
public int getCount() {
return modellist.size();
}
public Object getItem(int position) {
return modellist.get(position);
}
public long getItemId(int position) {
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
LayoutInflater inflater = context.getLayoutInflater();
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_row, null);
holder = new ViewHolder();
holder.Name = (TextView) convertView.findViewById(R.id.name);
holder.Company = (TextView) convertView.findViewById(R.id.company);
holder.Mobile = (TextView) convertView.findViewById(R.id.Mobile);
holder.Address = (TextView) convertView.findViewById(R.id.Address);
holder.Pincode = (TextView) convertView.findViewById(R.id.pincode);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.Name.setText(modellist.get(position).getName());
holder.Company.setText(modellist.get(position).getCom());
holder.Mobile.setText(modellist.get(position).getMob());
// holder.Address.setText(addr.get(position));
// holder.Pincode.setText(pin.get(position));
return convertView;
}
public void filter(String charText) {
charText = charText.toLowerCase(Locale.getDefault());
// modellist.clear();
if (charText.length() == 0) {
modellist.addAll(arraylist);
}
else
{
for (MyModel wp : arraylist)
{
if (wp.getName().toLowerCase(Locale.getDefault()).contains(charText))
{
modellist.add(wp);
}
}
}
notifyDataSetChanged();
}
}
MyModel.java
package com.example.filterlistview;
public class MyModel {
private String name;
private String com;
private String mob;
public MyModel( String name, String com,String mob)
{
this.name = name;
this.com = com;
this.mob = mob;
}
public String getName() {
return this.name;
}
public String getMob() {
return this.mob;
}
public String getCom() {
return this.com;
}
}
错误:
05-14 15:30:02.123: E/AndroidRuntime(21775): FATAL EXCEPTION: main
05-14 15:30:02.123: E/AndroidRuntime(21775): java.lang.NullPointerException
05-14 15:30:02.123: E/AndroidRuntime(21775): at com.example.filterlistview.MainActivity$1.onTextChanged(MainActivity.java:194)
05-14 15:30:02.123: E/AndroidRuntime(21775): at android.widget.TextView.sendOnTextChanged(TextView.java:7671)
05-14 15:30:02.123: E/AndroidRuntime(21775): at android.widget.TextView.handleTextChanged(TextView.java:7718)
05-14 15:30:02.123: E/AndroidRuntime(21775): at android.widget.TextView$ChangeWatcher.onTextChanged(TextView.java:8081)
05-14 15:30:02.123: E/AndroidRuntime(21775): at android.text.SpannableStringBuilder.sendTextChange(SpannableStringBuilder.java:892)
05-14 15:30:02.123: E/AndroidRuntime(21775): at android.text.SpannableStringBuilder.change(SpannableStringBuilder.java:352)
05-14 15:30:02.123: E/AndroidRuntime(21775): at android.text.SpannableStringBuilder.change(SpannableStringBuilder.java:266)
05-14 15:30:02.123: E/AndroidRuntime(21775): at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:443)
05-14 15:30:02.123: E/AndroidRuntime(21775): at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:420)
05-14 15:30:02.123: E/AndroidRuntime(21775): at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:29)
05-14 15:30:02.123: E/AndroidRuntime(21775): at android.view.inputmethod.BaseInputConnection.replaceText(BaseInputConnection.java:663)
05-14 15:30:02.123: E/AndroidRuntime(21775): at android.view.inputmethod.BaseInputConnection.setComposingText(BaseInputConnection.java:425)
05-14 15:30:02.123: E/AndroidRuntime(21775): at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:333)
05-14 15:30:02.123: E/AndroidRuntime(21775): at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:77)
05-14 15:30:02.123: E/AndroidRuntime(21775): at android.os.Handler.dispatchMessage(Handler.java:99)
05-14 15:30:02.123: E/AndroidRuntime(21775): at android.os.Looper.loop(Looper.java:154)
05-14 15:30:02.123: E/AndroidRuntime(21775): at android.app.ActivityThread.main(ActivityThread.java:4624)
05-14 15:30:02.123: E/AndroidRuntime(21775): at java.lang.reflect.Method.invokeNative(Native Method)
05-14 15:30:02.123: E/AndroidRuntime(21775): at java.lang.reflect.Method.invoke(Method.java:511)
05-14 15:30:02.123: E/AndroidRuntime(21775): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
05-14 15:30:02.123: E/AndroidRuntime(21775): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
05-14 15:30:02.123: E/AndroidRuntime(21775): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:2)
adapter
为null,因为您没有初始化它,您有以下代码:
CustomAdapter adapter= new CustomAdapter(MainActivity.this, a_list);
adapter.notifyDataSetChanged();
在上面的代码中,您在本地创建了adapter
但是您尝试在onTextChanged
的类顶部声明的调用适配器:
CustomAdapter adapter;
所以改变:
CustomAdapter adapter= new CustomAdapter(MainActivity.this, a_list);
到
adapter= new CustomAdapter(MainActivity.this, a_list);