Android中的可搜索活动

时间:2014-05-07 17:40:18

标签: android listview adapter searchable

这是我第一次尝试通过Android中的文字进行搜索,但我的listview适配器出现问题。 这是我搜索时调用的空白。我查找记录并为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

1 个答案:

答案 0 :(得分:0)

请显示错误日志以严重错误。 但我认为你必须这样做。在setAdapter到listview之前

if(places != null && places.size()>0){
AdapterPlaces adapter = new AdapterPlaces(this, places);

    lv1.setAdapter(adapter);
}