这是我第一次尝试在Android中通过文本进行搜索,但我的listview适配器有问题。这是搜索时调用的void。我查找记录并为我的listview设置适配器。它工作得很好,直到指令setAdapter引发一个NullPointerException,我不知道为什么。
private void doSearch(String queryStr)
{
AdminSQLiteOpenHelper admin = new AdminSQLiteOpenHelper(this,"db", null, 1);
SQLiteDatabase bd = admin.getWritableDatabase();
Place p=new Place();
ArrayList<Place> places=p.findByName(this, bd,queryStr);
ListView lv1 =(ListView)findViewById(R.id.list);
AdapterPlaces adapter = new AdapterPlaces(this, places);
lv1.setAdapter(adapter);
}
这是我创建的适配器
public class AdapterPlaces extends BaseAdapter
{
protected Activity activity;
protected ArrayList<Place> items;
public AdapterPlaces(Activity activity, ArrayList<Place> items) {
this.activity = activity;
this.items = items;
}
@Override
public int getCount() {
return items.size();
}
@Override
public Object getItem(int arg0) {
return items.get(arg0);
}
@Override
public long getItemId(int position) {
return items.get(position).getIdSitio();
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View v = convertView;
if(convertView == null){
LayoutInflater inf = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inf.inflate(R.layout.item_list, null);
}
Place pl = items.get(position);
TextView name = (TextView) v.findViewById(R.id.tvName);
nombre.setText(sit.getName());
TextView dir = (TextView) v.findViewById(R.id.tvAddress);
dir.setText(pl.getAddress());
return v;
}
日志如下:
05-07 23:40:46.151: W/IInputConnectionWrapper(9983): endBatchEdit on inactive InputConnection
05-07 23:40:46.243: W/dalvikvm(9983): threadid=1: thread exiting with uncaught exception (group=0x4161bd40)
05-07 23:40:46.247: E/AndroidRuntime(9983): FATAL EXCEPTION: main
05-07 23:40:46.247: E/AndroidRuntime(9983): Process: com.example.test, PID: 9983
05-07 23:40:46.247: E/AndroidRuntime(9983): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test/com.example.test.SearchableActivity}: java.lang.NullPointerException
05-07 23:40:46.247: E/AndroidRuntime(9983): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
05-07 23:40:46.247: E/AndroidRuntime(9983): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2269)
05-07 23:40:46.247: E/AndroidRuntime(9983): at android.app.ActivityThread.access$800(ActivityThread.java:139)
05-07 23:40:46.247: E/AndroidRuntime(9983): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
05-07 23:40:46.247: E/AndroidRuntime(9983): at android.os.Handler.dispatchMessage(Handler.java:102)
05-07 23:40:46.247: E/AndroidRuntime(9983): at android.os.Looper.loop(Looper.java:136)
05-07 23:40:46.247: E/AndroidRuntime(9983): at android.app.ActivityThread.main(ActivityThread.java:5102)
05-07 23:40:46.247: E/AndroidRuntime(9983): at java.lang.reflect.Method.invokeNative(Native Method)
05-07 23:40:46.247: E/AndroidRuntime(9983): at java.lang.reflect.Method.invoke(Method.java:515)
05-07 23:40:46.247: E/AndroidRuntime(9983): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
05-07 23:40:46.247: E/AndroidRuntime(9983): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
05-07 23:40:46.247: E/AndroidRuntime(9983): at dalvik.system.NativeStart.main(Native Method)
05-07 23:40:46.247: E/AndroidRuntime(9983): Caused by: java.lang.NullPointerException
05-07 23:40:46.247: E/AndroidRuntime(9983): at com.example.test.SearchableActivity.doSearch(SearchableActivity.java:62)
05-07 23:40:46.247: E/AndroidRuntime(9983): at com.example.test.SearchableActivity.handleIntent(SearchableActivity.java:41)
05-07 23:40:46.247: E/AndroidRuntime(9983): at com.example.test.SearchableActivity.onCreate(SearchableActivity.java:23)
05-07 23:40:46.247: E/AndroidRuntime(9983): at android.app.Activity.performCreate(Activity.java:5248)
05-07 23:40:46.247: E/AndroidRuntime(9983): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110)
05-07 23:40:46.247: E/AndroidRuntime(9983): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2173)
05-07 23:40:46.247: E/AndroidRuntime(9983): ... 11 more
请显示错误日志以接近错误。但我觉得你必须这么做。setAdapter到listview
if(places != null && places.size()>0){
AdapterPlaces adapter = new AdapterPlaces(this, places);
lv1.setAdapter(adapter);
}