【发布时间】:2023-03-07 21:55:02
【问题描述】:
我正在尝试从“MainActivity.java”中的片段访问 SQLite 数据库,但我的数据库函数位于另一个文件“Database.java”中,因此在“MainActivity”的“onCreateView”中调用了 database_fragment.xml " 无法访问那些函数...我是否应该将所有这些数据库函数直接放入片段类中以便它可以看到那些?
这是我的代码的重要部分:
(IN MainActivity.java)
/**
* "fragment_database", shows the database list
*/
public static class subFragment extends Fragment {
public static final String ARG_MENU_NUMBER = "menu_number"; // Which sub-menu
public subFragment() {
// Empty constructor required for fragment subclasses
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
int i = getArguments().getInt(ARG_MENU_NUMBER);
View rootView;
if(i==0){
rootView = inflater.inflate(R.layout.fragment_database, container, false);
}else{
rootView = inflater.inflate(R.layout.fragment_help, container, false);
}
return rootView;
}
}
这里是 XML 无法达到的功能:
(IN 数据库.java)
public void onClick_DisplayRecords(View v) {
displayText("Clicked display record!");
Cursor cursor = myDb.getAllRows();
displayRecordSet(cursor);
}
错误信息:
java.lang.IllegalStateException:在 id 为“btnDisplay”的视图类 android.widget.Button 上的 onClick 处理程序的活动类 com.mtl.android.spca.MainActivity 中找不到方法 onClick_DisplayRecords(View)
【问题讨论】:
标签: android database sqlite android-fragments fragment