gpt4 book ai didi

sqlite - Nullpointer database.open(), getwritabledatabase

转载 作者:行者123 更新时间:2023-12-03 19:52:24 25 4
gpt4 key购买 nike

我想填写一个列表并从事件中打开的数据库中执行此操作。我制作了一个自定义适配器,并且那个“助手”类将另一个“助手”类称为 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)

最佳答案

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

关于sqlite - Nullpointer database.open(), getwritabledatabase,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15524826/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com