从自定义适配器类刷新ListView - Android

时间:2013-11-01 23:47:16

标签: java android sqlite listview nullpointerexception

我想从我的自定义适配器类刷新ListFragment中的数据,但得到错误NullPointerException。

这是Adaper类方法:

b1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            BasketFragment basketFragment = new BasketFragment();
            basketFragment.getBsketList();

        }
    });

当我在Fragment类中调用它时,我在mDataBase = dbHelper.getWritableDatabase();

获取java.lang.NullPointerException

getBasketList():

public void refreshList() {

    dbAdaper.setArrayMyData(selectAll());
    dbAdaper.notifyDataSetChanged();
}

以下是我收到错误的部分:

 public ArrayList<BasketData> selectAll() {

    Cursor mCursor = null;
    dbHelper = new DBHelper(getActivity());
    mDataBase = dbHelper.getWritableDatabase();//NullPointerException
    mCursor = mDataBase.query("orderTable", null, null, null, null, null, null);

logcat的:

 java.lang.NullPointerException
        at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)
        at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
        at com.epon.fragments.BasketFragment.selectAll(BasketFragment.java:100)
        at com.epon.fragments.BasketFragment.getBsketList(BasketFragment.java:142)
        at com.epon.adapters.DBBasketAdapter$2.onClick(DBBasketAdapter.java:177)
        at android.view.View.performClick(View.java:4240)
        at android.view.View$PerformClick.run(View.java:17721)
        at android.os.Handler.handleCallback(Handler.java:730)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5103)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
        at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

您是否设置了上下文?

我认为他的问题是他的问题。

如果这不能帮助发布getActivity()的内容可能会有帮助吗?

修改

你的logcat让我一直认为它是一个上下文错误!