好的。简单的背景。Activity 启动 Fragment1,它加载从数据库 (getAllRows) 提供的名称列表视图。当我从 Fragment1 中选择一个名称时,每个名称都有我想要加载到 Fragment2 的相关信息。但是,当我进行选择并调用 Fragment2 中的 getRow 以将信息加载到 listView 中进行显示时,它会出错。我不知道出了什么问题。
片段 1
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState){
openDB();
View rootView = inflater.inflate(R.layout.list_view, container, false);
myList = (ListView) rootView.findViewById(R.id.list);
Cursor cursor = myDB.getAllRows();
String[] fromFieldNames = new String[]
{DataBaseHelper.KEY_NAME};
int[] toViewIDs = new int[]
{R.id.elListText};
SimpleCursorAdapter myCursorAdapter =
new SimpleCursorAdapter(
rootView.getContext(),
R.layout.el_list_item,
cursor,
fromFieldNames,
toViewIDs
);
myList.setAdapter(myCursorAdapter);
myList.setOnItemClickListener(new ItemClickListener());
return rootView;
}
public class ItemClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectElement(position);
}
}
// selectElement Method
// passes the information from the selected element
// into the elementFragment fragment
public void selectElement(int position) {
Cursor c = myDB.getRow(++position);
Fragment fragment = new elementFragment();
Bundle args = new Bundle();
// define arguments to provide to next fragment
args.putInt(elementFragment.ARG_ELEMENT_NUMBER, position);
fragment.setArguments(args);
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(table.periodic.R.id.content_frame, fragment).commit();
closeDB();
}
片段2
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
openDB();
View viewElement = inflater.inflate(R.layout.list_view_element, container, false);
viewList = (ListView) viewElement.findViewById(R.id.list_view);
Bundle args = getArguments();
int i = args.getInt(elementFragment.ARG_ELEMENT_NUMBER);
40 Cursor c = myDB.getRow(i);
String[] fromFieldNames = new String[]
{DataBaseHelper.KEY_NAME};
int[] toViewIDs = new int[]{
R.id.elRowDesc
};
SimpleCursorAdapter myCursorAdapter =
new SimpleCursorAdapter(
viewElement.getContext(),
R.layout.el_list_item,
c,
fromFieldNames,
toViewIDs
);
viewList.setAdapter(myCursorAdapter);
String name = args.getString(DataBaseHelper.KEY_NAME);
getActivity().setTitle(name);
return viewElement;
}
数据库助手
public Cursor getRow(int rowId) {
String where = KEY_ROWID + "=" + rowId;
168 Cursor c = myDataBase.query(true, DB_TABLE, ALL_KEYS,
where, null, null, null, null, null, null);
if (c != null) {
c.moveToFirst();
}
return c;
}
日志猫
10-24 12:07:07.864: E/AndroidRuntime(29534): FATAL EXCEPTION: main
10-24 12:07:07.864: E/AndroidRuntime(29534): java.lang.NullPointerException
10-24 12:07:07.864: E/AndroidRuntime(29534): at table.periodic.DataBaseHelper.getRow(DataBaseHelper.java:168)
10-24 12:07:07.864: E/AndroidRuntime(29534): at table.periodic.elementFragment.onCreateView(elementFragment.java:40)
10-24 12:07:07.864: E/AndroidRuntime(29534): at android.app.Fragment.performCreateView(Fragment.java:1785)
10-24 12:07:07.864: E/AndroidRuntime(29534): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:887)
10-24 12:07:07.864: E/AndroidRuntime(29534): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1059)
10-24 12:07:07.864: E/AndroidRuntime(29534): at android.app.BackStackRecord.run(BackStackRecord.java:682)
10-24 12:07:07.864: E/AndroidRuntime(29534): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1437)
10-24 12:07:07.864: E/AndroidRuntime(29534): at android.app.FragmentManagerImpl$1.run(FragmentManager.java:443)
10-24 12:07:07.864: E/AndroidRuntime(29534): at android.os.Handler.handleCallback(Handler.java:725)
10-24 12:07:07.864: E/AndroidRuntime(29534): at android.os.Handler.dispatchMessage(Handler.java:92)
10-24 12:07:07.864: E/AndroidRuntime(29534): at android.os.Looper.loop(Looper.java:158)
10-24 12:07:07.864: E/AndroidRuntime(29534): at android.app.ActivityThread.main(ActivityThread.java:5751)
10-24 12:07:07.864: E/AndroidRuntime(29534): at java.lang.reflect.Method.invokeNative(Native Method)
10-24 12:07:07.864: E/AndroidRuntime(29534): at java.lang.reflect.Method.invoke(Method.java:511)
10-24 12:07:07.864: E/AndroidRuntime(29534): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1083)
10-24 12:07:07.864: E/AndroidRuntime(29534): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:850)
10-24 12:07:07.864: E/AndroidRuntime(29534): at dalvik.system.NativeStart.main(Native Method)