gpt4 book ai didi

android在 fragment 中加载sqlite数据

转载 作者:搜寻专家 更新时间:2023-11-01 08:05:11 24 4
gpt4 key购买 nike

我遇到无法在 Fragment 中加载 sqlite 数据的问题,如下所示

public class ListFragment extends Fragment {

DatabaseUtil dbUtil = new DatabaseUtil(this.getActivity());
private ArrayList <HashMap<String, Object>> getNews;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View v = inflater.inflate(R.layout.activity_list, container, false);


new LoadNewsLists().execute("");

return v;
}

这里是从sqlite加载数据

Cursor cursor = dbUtil.fetchAllData(arg[0]);

遇到以下错误。

03-08 12:21:52.900: E/AndroidRuntime(25895): Caused by: java.lang.NullPointerException
03-08 12:21:52.900: E/AndroidRuntime(25895): at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)

最佳答案

你不能在定义它的同时初始化你的数据库对象。您应该将初始化代码移动到 onCreateView() 回调:

DatabaseUtil dbUtil ;
private ArrayList <HashMap<String, Object>> getNews;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View v = inflater.inflate(R.layout.activity_list, container, false);

dbUtil = new DatabaseUtil(getActivity());
new LoadNewsLists().execute("");

return v;
}

关于android在 fragment 中加载sqlite数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15288156/

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