gpt4 book ai didi

java - Android NullPointerException(getDatabaseLocked 和 getWritableDatabase)

转载 作者:太空宇宙 更新时间:2023-11-04 14:14:27 25 4
gpt4 key购买 nike

我想使用Listview来显示SQLite数据库中的数据。我使用 Fragment 来获取 ListView 。但现在,我遇到了一个问题:

This is the Log info:
E/AndroidRuntime(12816): FATAL EXCEPTION: main
E/AndroidRuntime(12816): java.lang.NullPointerException
E/AndroidRuntime(12816): at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)
E/AndroidRuntime(12816): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
E/AndroidRuntime(12816): at com.tax_ui.sql.Inform_dbHandler.getAllInform(Inform_dbHandler.java:80)
E/AndroidRuntime(12816): at com.tax_ui.fragment.InformFragment.init(InformFragment.java:72)
E/AndroidRuntime(12816): at com.tax_ui.fragment.InformFragment.onCreateView(InformFragment.java:55)

这是列表适配器代码:

public class InformListAdapter extends BaseAdapter {

private Activity activity;
private LayoutInflater inflater;
private List<Inform_item> inform_item_list;

public InformListAdapter(Activity activity,
List<Inform_item> inform_item_list) {
this.activity = activity;
this.inform_item_list = inform_item_list;
}

@Override
public int getCount() {
// TODO Auto-generated method stub
return inform_item_list.size();
}

@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return inform_item_list.get(position);
}

@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if (inflater == null)
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
convertView = inflater.inflate(R.layout.fragment_inform_item, null);

TextView title = (TextView) convertView.findViewById(R.id.inform_title);
TextView timestamp = (TextView) convertView
.findViewById(R.id.inform_timestamp);
TextView content = (TextView) convertView
.findViewById(R.id.inform_content);

Inform_item inform_item = inform_item_list.get(position);

title.setText(inform_item.get_title());
timestamp.setText(inform_item.get_timestamp());
content.setText(inform_item.get_content());

return null;
}

}

然后,这是我的 fragment 代码:

public class InformFragment extends Fragment {

private View mBaseView;
private TitleBarView mTitleBarView;
private ListView listView;
private InformListAdapter listAdapter;
private List<Inform_item> inform_item_list;

private Inform_dbHandler inform_dbHandler = new Inform_dbHandler(getActivity());

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub

getActivity();
mBaseView=inflater.inflate(R.layout.fragment_inform_list, null);
findView();
init();

return mBaseView;
}

private void findView(){
mTitleBarView=(TitleBarView) mBaseView.findViewById(R.id.title_bar);
listView = (ListView)mBaseView.findViewById(R.id.inform_list);
}

private void init(){
mTitleBarView.setCommonTitle(View.GONE, View.VISIBLE, View.GONE);
mTitleBarView.setTitleText(R.string.inform);

inform_item_list = new ArrayList<Inform_item>();
inform_item_list = inform_dbHandler.getAllInform();

listAdapter = new InformListAdapter(getActivity(), inform_item_list);
listView.setAdapter(listAdapter);
}

}

我认为这行可能是错误的:

private Inform_dbHandler inform_dbHandler  = 
new Inform_dbHandler(getActivity());

但我不知道如何解决这个问题?你能帮助我吗?谢谢~

最佳答案

修改如下:

private Inform_dbHandler inform_dbHandler ;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub

getActivity();
mBaseView=inflater.inflate(R.layout.fragment_inform_list, null);
findView();
init();
Inform_dbHandler inform_dbHandler = new Inform_dbHandler(getActivity());

return mBaseView;
}

问题是您正在为 dbhandler 创建对象,但该对象将在附加到 Activity 之前进行初始化。在这种情况下 getActivity() 方法返回 null,因此数据库将无法计算出来。

那么最佳实践是如果您希望 dbhelper 类对象全局初始化该对象

OnAttach() fragment 。

关于java - Android NullPointerException(getDatabaseLocked 和 getWritableDatabase),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27921591/

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