1

我想填写一个列表并从活动中打开的数据库中执行此操作。我制作了一个自定义适配器,并且那个“助手”类将另一个“助手”类称为 listview 包装器。在那个 listview 包装器中,我想打开另一个(不同的)数据库,但我得到一个空指针异常。

所以在我打电话的活动中:

 adapter = new ListViewAdapter(this, ListViewAdapter.TPL_HISTORY, historydatasource.getAllCalls());
             list.setAdapter(adapter);

比 historydatasource 类打开一个 listviewwrapper (helper) 类来处理我为 listview 创建的模板。在那里我想打开另一个数据库,但我得到一个空指针异常。

有人知道我该怎么做吗?

        DB_DataSource cardsdatasource=new DB_DataSource(myContext);
cardsdatasource.open(); (GIVES NULLPOINTER, because myContext=null)

有没有办法获取上下文?

错误:

03-20 14:03:51.919: E/AndroidRuntime(15492): FATAL EXCEPTION: main
03-20 14:03:51.919: E/AndroidRuntime(15492): java.lang.NullPointerException
03-20 14:03:51.919: E/AndroidRuntime(15492):    at com.myapp.testapp.ListViewWrapper.populateFrom(ListViewWrapper.java:91)
03-20 14:03:51.919: E/AndroidRuntime(15492):    at com.myapp.testapp.ListViewAdapter.getView(ListViewAdapter.java:121)
03-20 14:03:51.919: E/AndroidRuntime(15492):    at android.widget.AbsListView.obtainView(AbsListView.java:1536)
03-20 14:03:51.919: E/AndroidRuntime(15492):    at android.widget.ListView.makeAndAddView(ListView.java:1793)
03-20 14:03:51.919: E/AndroidRuntime(15492):    at android.widget.ListView.fillDown(ListView.java:718)
03-20 14:03:51.919: E/AndroidRuntime(15492):    at android.widget.ListView.fillFromTop(ListView.java:775)
03-20 14:03:51.919: E/AndroidRuntime(15492):    at android.widget.ListView.layoutChildren(ListView.java:1646)
03-20 14:03:51.919: E/AndroidRuntime(15492):    at android.widget.AbsListView.onLayout(AbsListView.java:1366)
03-20 14:03:51.919: E/AndroidRuntime(15492):    at android.view.View.layout(View.java:7175)
03-20 14:03:51.919: E/AndroidRuntime(15492):    at android.widget.RelativeLayout.onLayout(RelativeLayout.java:912)
03-20 14:03:51.919: E/AndroidRuntime(15492):    at android.view.View.layout(View.java:7175)
03-20 14:03:51.919: E/AndroidRuntime(15492):    at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
03-20 14:03:51.919: E/AndroidRuntime(15492):    at android.view.View.layout(View.java:7175)
03-20 14:03:51.919: E/AndroidRuntime(15492):    at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
03-20 14:03:51.919: E/AndroidRuntime(15492):    at android.view.View.layout(View.java:7175)
03-20 14:03:51.919: E/AndroidRuntime(15492):    at android.view.ViewRoot.performTraversals(ViewRoot.java:1146)
03-20 14:03:51.919: E/AndroidRuntime(15492):    at android.view.ViewRoot.handleMessage(ViewRoot.java:1866)
03-20 14:03:51.919: E/AndroidRuntime(15492):    at android.os.Handler.dispatchMessage(Handler.java:99)
03-20 14:03:51.919: E/AndroidRuntime(15492):    at android.os.Looper.loop(Looper.java:123)
03-20 14:03:51.919: E/AndroidRuntime(15492):    at android.app.ActivityThread.main(ActivityThread.java:3687)
03-20 14:03:51.919: E/AndroidRuntime(15492):    at java.lang.reflect.Method.invokeNative(Native Method)
03-20 14:03:51.919: E/AndroidRuntime(15492):    at java.lang.reflect.Method.invoke(Method.java:507)
03-20 14:03:51.919: E/AndroidRuntime(15492):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
03-20 14:03:51.919: E/AndroidRuntime(15492):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
03-20 14:03:51.919: E/AndroidRuntime(15492):    at dalvik.system.NativeStart.main(Native Method)
4

2 回答 2

1

这确实是一个上下文问题,我将 activity.getApplicationContext() 从 CustomAdapter 传递给了包装器并得到了解决。

于 2013-03-20T17:35:48.927 回答
0

DataSource上课的方式如下:

public class StudyManagerDataSource {

    public static final String LOG_TAG = "studymanager";

    SQLiteOpenHelper dbHelper;
    SQLiteDatabase database;        

    // --------------------------------------------------------------------------------------------

    public StudyManagerDataSource(Context context) {
        dbHelper = new DBOpenHelper(context);

    }

    // --------------------------------------------------------------------------------------------

    public void open() {
        database = dbHelper.getWritableDatabase();
        Log.i(LOG_TAG, "Database Opened");
    } // end method open

    public void close() {
        Log.i(LOG_TAG, "Database Closed");
        dbHelper.close();
    } // end method close
    /*
    Other methods for manipulating tables goes here...
       */    
}

告诉我它是否解决了你的问题!

于 2013-03-20T13:43:48.977 回答