NullPointErexception不知道为什么?

时间:2010-09-22 16:08:41

标签: android

请查看此代码,看看是否可以帮助我。 viewItem从数据库填充,方法fillRemindersData(long rowId)仅从提醒类调用。我不知道为什么我在调用viewitems.fillRemindersData()方法时得到nullPointerException。如果我评论该行,代码工作正常,rowId是正确的。可能是什么原因?感谢

// this is a reminder class
public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.view_list);
        adapter = new DBAdapter(this);
        viewItems = new ViewItems();
        fillReminder();     
    }

    private void fillReminder() {
        Bundle extra = getIntent().getExtras();
        if(extra != null){
            rowId = extra.getLong(DBAdapter.KEY_ID);


            message = extra.getString(NewItem.Test);
        }
        Toast.makeText(this, message + "its ok here" + rowId, Toast.LENGTH_LONG).show();
        viewItems.fillRemindersData(rowId); // having a null pointer exception here.
}

viewItems类的重要部分是:

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.view_list);
        adapter = new DBAdapter(this);
        adapter.open();
        fillData();
        list = (ListView)findViewById(android.R.id.list);
        list.setOnItemClickListener(this);


    }

 protected void fillData() {

 Cursor c = adapter.retrieveItems(); // method in sqlitedatabase
 startManagingCursor(c);

String[] from = new String[]{DBAdapter.NAME, DBAdapter.START_DATE, DBAdapter.START_TIME};
int[] to = new int[]{R.id.viewNameId, R.id.viewDateId, R.id.viewTimeId};

customCursorAdapter items = new customCursorAdapter(this, R.layout.view_items, c, from, to);
                setListAdapter(items);          

    }

 protected void fillRemindersData(long Id) {
 long row = Id;
 Cursor c = adapter.retrieveRow(row); // method in sqlitedatabase
 startManagingCursor(c);

String[] from = new String[]{DBAdapter.NAME, DBAdapter.START_DATE, DBAdapter.START_TIME};

int[] to = new int[]{R.id.RemindNameId, R.id.remindDateId, R.id.remindTimeId};

customCursorAdapter items = new customCursorAdapter(this, R.layout.remind_viewer, c, from, to);
setListAdapter(items);          

    }
logcat引用的

方法:

public Cursor retrieveRow(long rowId){
String[] resultColumns = new String[] {NAME,START_DATE,START_TIME};
Cursor row = db.query(true,DATABASE_TABLE, resultColumns, KEY_ID +"=" +rowId, null, null, null, null,null);
     if(row != null){
    row.moveToNext();
          return row;
    }
    return row;
     }

Logcat输出:

09-22 15:19:00.346: ERROR/AndroidRuntime(808): Uncaught handler: thread main exiting due to uncaught exception
09-22 15:19:00.386: ERROR/AndroidRuntime(808): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.MemoBuzz/com.MemoBuzz.RemindViewer}: java.lang.NullPointerException  


    09-22 15:19:00.386: ERROR/AndroidRuntime(808): Caused by: java.lang.NullPointerException

    09-22 15:19:00.386: ERROR/AndroidRuntime(808):     at com.MemoBuzz.ViewItems.fillRemindersData(ViewItems.java:57)

    09-22 15:19:00.386: ERROR/AndroidRuntime(808):     at com.MemoBuzz.RemindViewer.fillReminder(RemindViewer.java:51)

    09-22 15:19:00.386: ERROR/AndroidRuntime(808):     at com.MemoBuzz.RemindViewer.onCreate(RemindViewer.java:36)

    09-22 15:19:00.386: ERROR/AndroidRuntime(808):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)

    09-22 15:19:00.386: ERROR/AndroidRuntime(808):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2231)

    09-22 15:19:00.386: ERROR/AndroidRuntime(808):     ... 11 more

3 个答案:

答案 0 :(得分:0)

您是否尝试使用viewItems = new ViewItems();开始活动? ViewItems是一个活动类吗?你有一个onCreate()。您需要使用intent启动活动,而不是使用new

答案 1 :(得分:0)

在您的情况下,似乎永远不会调用onCreate() ViewItems方法。因此适配器为空,您将获得异常。可能你必须将数据库对象放在ViewItems类之外,因为它现在看起来像一个活动或服务(如果就是这样调用它上面的构造函数,就像你一样,这绝对是个坏主意。)

答案 2 :(得分:0)

在我看来,你没有将自己的活动添加到清单中。