从sqlite填充数据时,游标适配器上的Android NullPointerException

时间:2015-02-19 23:16:15

标签: android android-listview nullpointerexception android-cursor

我收到了NullpointerException。 以下是我的课程:

public class showrecordsBetweenDates extends Activity {


SQLiteDatabase db;
DBAdapter dbh;
  .........
  .........
  .........
public Cursor getMeterRecords(){

    dbh = new DBAdapter(showrecordsBetweenDates.this);
 db = dbh.getWritableDatabase();

            Cursor c;
            String[] All_Columns = new String[] { DBAdapter.COLUMN_METER_ID, DBAdapter.COLUMN_METER_DATE, DBAdapter.COLUMN_METER_START, DBAdapter.COLUMN_METER_END };
            c = db.query(DBAdapter.TABLE_METER, All_Columns, null, null, null, null, null);
            if (c != null) {
                c.moveToNext();
            }
            return c;
        }

我的适配器类是

公共类MeterAdapter扩展了Activity {

SimpleCursorAdapter mycursor;
showrecordsBetweenDates showRecords;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list_view_records);
    showRecords= new showrecordsBetweenDates();
    populateListview();
}

public void populateListview() {

    Cursor cursor;
    cursor = showRecords.getMeterRecords();
    startManagingCursor(cursor);
    String[] fromfields = new String[] { 
            DBAdapter.COLUMN_METER_DATE,
            DBAdapter.COLUMN_METER_START, 
            DBAdapter.COLUMN_METER_END,
            DBAdapter.COLUMN_METER_CAR_ID };
    int[] tofields = new int[] { 
            R.id.meterdate, 
            R.id.meterstart,
            R.id.meterend,
            R.id.fk};
    mycursor = new SimpleCursorAdapter(this, R.layout.row_meter, cursor,
            fromfields, tofields, 0);
    ListView mylist = (ListView) findViewById(R.id.list);

这是我的错误

02-20 03:31:12.823: E/AndroidRuntime(31264):    at dalvik.system.NativeStart.main(Native Method)
02-20 03:31:12.823: E/AndroidRuntime(31264): Caused by: java.lang.NullPointerException
 02-20 03:31:12.823: E/AndroidRuntime(31264):   at com.munawwar.sultan.showrecordsdatewise.showrecordsBetweenDates.getMeterRecords(showrecordsBetweenDates.java:183)
 02-20 03:31:12.823: E/AndroidRuntime(31264):   at com.munawwar.sultan.cursoradapter.MeterAdapter.populateListview(MeterAdapter.java:39)
 02-20 03:31:12.823: E/AndroidRuntime(31264):   at com.munawwar.sultan.cursoradapter.MeterAdapter.onCreate(MeterAdapter.java:31)
 02-20 03:31:12.823: E/AndroidRuntime(31264):   at android.app.Activity.performCreate(Activity.java:5020)
 02-20 03:31:12.823: E/AndroidRuntime(31264):   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
 02-20 03:31:12.823: E/AndroidRuntime(31264):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
 02-20 03:31:12.823: E/AndroidRuntime(31264):   ... 11 more

我从DBAdapter类中的方法填充listview但现在我需要在另一个类(showrecordsBetweenDates)中使用该方法,并且我得到了该错误 在此先感谢

0 个答案:

没有答案