0

嗨,我自己是 android 的新手,我已经完成了从数据库中搜索特定记录并使用简单的光标适配器显示它的列表的代码,以使用我开始使用自定义适配器的数据来获取图像,当我尝试检索时代码工作得很好来自 db 的所有数据,但是当我将参数传递给 db 中的方法以搜索特定记录并将其显示在列表中时出现错误

这是我的代码,当用户选择要显示的数据时传递

    Intent i= new Intent(SearchByBed.this,SearchBedView.class);
    Bundle bbed= new Bundle();
    bbed.putString("bedValue", spinn_Bed.getSelectedItem().toString());
    i.putExtras(bbed);
    startActivity(i);

在这部分中,从上一个类传递的参数是 catch,然后传递给 db 并想要检索记录并将其绑定到 custom-adapter

my asynctask code


public class loadSomeStuff extends AsyncTask<String, Integer, String> {
    ProgressDialog dialog;

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        dialog = new ProgressDialog(SearchBedView.this);
        dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        dialog.setMax(100);
        dialog.setMessage("Loading Data");
        dialog.show();

    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        // TODO Auto-generated method stub
        dialog.incrementProgressBy(values[0]);
    }

    @Override
    protected String doInBackground(String... params) {
        // TODO Auto-generated method stub
        for (int i = 0; i < 20; i++) {
            publishProgress(5);
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
        dialog.dismiss();

        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
        displaydemo();

    }


}

public void displaydemo() {
    // TODO Auto-generated method stub

    Bundle bbedview = this.getIntent().getExtras();

     name = bbedview.getString("name").toString();    
     //data.setText(name);   //as per log here i was getting null pointer exception but  

     //know commented as it was line from my previous code 

    info = new PropertyInfo(this);
    info.open();
    cursor = info.getBed(name);
    String[] from = new String[] { PropertyInfo.KEY_ROWID,
            PropertyInfo.KEY_TYPE, PropertyInfo.KEY_BEDROOMS,
            PropertyInfo.KEY_STATUS, PropertyInfo.KEY_CITY,
            PropertyInfo.KEY_FURNISHING, PropertyInfo.KEY_TOTPRICE };
    int[] to = new int[] { R.id.rowId, R.id.rowtype, R.id.rowBed,
            R.id.rowStatus, R.id.rowCity, R.id.rowFurnish,
            R.id.rowTotalPrice };
    custAdapter = new CustomContactsAdapterBedView(this, R.layout.row, cursor,
            from, to);

    this.setListAdapter(custAdapter);
           }

虽然这是 mycustomadapter 类

 public class CustomContactsAdapterBedView extends SimpleCursorAdapter {

    private int layout;
    LayoutInflater inflator;
    TextView tid,ttype,tbed,tstatus,tcity,tfurnish,ttotprice;

    public CustomContactsAdapterBedView(Context context, int layout, Cursor                    c,
            String[] from, int[] to) {
        super(context, layout, c, from, to, 0);
        this.layout = layout;
        inflator = LayoutInflater.from(context);
    }


    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        View v = inflator.inflate(layout, parent, false);
        return v;
    }


    @Override
    public void bindView(View v, final Context context, Cursor c) {
        final String id = c.getString(c.getColumnIndex(PropertyInfo.KEY_ROWID));
        final String type = c
                .getString(c.getColumnIndex(PropertyInfo.KEY_TYPE));
        final String bed = c.getString(c
                .getColumnIndex(PropertyInfo.KEY_BEDROOMS));
        final String status = c.getString(c
                .getColumnIndex(PropertyInfo.KEY_STATUS));
        final String city = c
                .getString(c.getColumnIndex(PropertyInfo.KEY_CITY));
        final String furnish = c.getString(c
                .getColumnIndex(PropertyInfo.KEY_FURNISHING));
        final String totprice = c.getString(c
                .getColumnIndex(PropertyInfo.KEY_TOTPRICE));
        final byte[] image = c.getBlob(c.getColumnIndex(PropertyInfo.IMAGE));
        ImageView iv = (ImageView) v.findViewById(R.id.photo);

        if (image != null) {
            if (image.length > 3) {
                iv.setImageBitmap(BitmapFactory.decodeByteArray(image, 0,
                        image.length));
            }
        }

        tid = (TextView) v.findViewById(R.id.rowId);
        tid.setText(id);

        ttype = (TextView) v.findViewById(R.id.rowtype);
        ttype.setText(type);

        tbed = (TextView) v.findViewById(R.id.rowBed);
        tbed.setText(bed);

        tstatus = (TextView) v.findViewById(R.id.rowStatus);
        tstatus.setText(status);

        tcity = (TextView) v.findViewById(R.id.rowCity);
        tcity.setText(city);

        tfurnish = (TextView) v.findViewById(R.id.rowFurnish);
        tfurnish.setText(furnish);

        ttotprice = (TextView) v.findViewById(R.id.rowTotalPrice);
        ttotprice.setText(totprice);

    }
}

db中的方法如下

         public Cursor getBed(String l) {
    String[] columns = new String[] { KEY_ROWID, KEY_TITLE, KEY_TYPE,
            KEY_BEDROOMS, KEY_STATUS, KEY_CITY, KEY_LOCALITY, KEY_PRICE,
            KEY_TOTPRICE, KEY_FURNISHING, KEY_BALCONIES, KEY_BATHROOM,
            IMAGE };
    Cursor c = ourDatabase.query(DATABASE_TABLE, columns, KEY_BEDROOMS
            + "=" + l, null, null, null, null);
    return c;

}

logcat中的错误是

          05-16 20:33:43.803: E/AndroidRuntime(31444): FATAL EXCEPTION: main
          05-16 20:33:43.803: E/AndroidRuntime(31444): java.lang.NullPointerException
          05-16 20:33:43.803: E/AndroidRuntime(31444):  at 

          com.project.realestate.SearchBedView.displaydemo(SearchBedView.java:299)
          05-16 20:33:43.803: E/AndroidRuntime(31444):  at 

        com.project.realestate.SearchBedView$loadSomeStuff.onPostExecute(SearchBedView.java:281)
         05-16 20:33:43.803: E/AndroidRuntime(31444):   at 

      com.project.realestate.SearchBedView$loadSomeStuff.onPostExecute(SearchBedView.java:1)
         05-16 20:33:43.803: E/AndroidRuntime(31444):   at 
         android.os.AsyncTask.finish(AsyncTask.java:631)
         05-16 20:33:43.803: E/AndroidRuntime(31444):   at   
         android.os.AsyncTask.access$600(AsyncTask.java:177)
         05-16 20:33:43.803: E/AndroidRuntime(31444):   at  
         android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
         05-16 20:33:43.803: E/AndroidRuntime(31444):   at  
         android.os.Handler.dispatchMessage(Handler.java:99)
         05-16 20:33:43.803: E/AndroidRuntime(31444):   at  
         android.os.Looper.loop(Looper.java:137)
         05-16 20:33:43.803: E/AndroidRuntime(31444):   at   
         android.app.ActivityThread.main(ActivityThread.java:5041)
         05-16 20:33:43.803: E/AndroidRuntime(31444):   at
         java.lang.reflect.Method.invokeNative(Native Method)
         05-16 20:33:43.803: E/AndroidRuntime(31444):   at 
         java.lang.reflect.Method.invoke(Method.java:511)
         05-16 20:33:43.803: E/AndroidRuntime(31444):   at  
        com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
        05-16 20:33:43.803: E/AndroidRuntime(31444):    at
        com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
        05-16 20:33:43.803: E/AndroidRuntime(31444):    at 
        dalvik.system.NativeStart.main(Native Method)

请帮助我的代码

4

2 回答 2

0

您在哪里为 TextView(或任何小部件)定义了“数据”?

它应该被定义为:

TextView data = (TextView) findViewById(R.id. yourTextView );

于 2013-05-21T10:04:38.587 回答
0
data.setText(name);

I guess the data is EditText or A textView have you got the reference to it with findViewById

you have to enclose string value with single quote and escape sequence i have modified the line

Cursor c = ourDatabase.query(DATABASE_TABLE, columns, KEY_BEDROOMS
        + "=\'" +l+"\'", null, null, null, null);
于 2013-05-21T09:34:58.307 回答