以前は、以下のコードを使用して、データとともに Fragment から Activity を呼び出していました。
ListCategoryFragment.java :
case categoryFirst:
Intent intent1 = new Intent(getActivity(), ListItemActivity.class);
intent1.putExtra("category", "Category - 1");
startActivity(intent1);
break;
ListItemActivity.java :
Bundle bdl = getActivity().getIntent().getExtras();
KEY_CATEGORY = bdl.getString("category");
しかし今、私は以下のコードを使用して、フラグメントからフラグメントを呼び出しています:
ListCategoryFragment.java :
private static final int categoryFirst = 0;
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long id) {
// Set up different intents based on the item clicked:
switch (position)
{
case categoryFirst:
ListItemFragment fragment1 = new ListItemFragment();
Bundle bundle = new Bundle();
bundle.putString("category", "Category - 1");
fragment1.setArguments(bundle);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(android.R.id.content, fragment1);
fragmentTransaction.commit();
break;
default:
break;
}
ListItemFragment.java :
Bundle bdl = getActivity().getIntent().getExtras();
KEY_CATEGORY = bdl.getString("category"); // Line No. - 67
ログは言う:
10-22 02:06:06.930: W/dalvikvm(914): threadid=1: thread exiting with uncaught exception (group=0x41465700)
10-22 02:06:06.959: E/AndroidRuntime(914): FATAL EXCEPTION: main
10-22 02:06:06.959: E/AndroidRuntime(914): java.lang.NullPointerException
10-22 02:06:06.959: E/AndroidRuntime(914): at com.androidbegin.absviewpagertutorial.ListItemFragment.onCreateView(ListItemFragment.java:67)
10-22 02:06:06.959: E/AndroidRuntime(914): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:870)
10-22 02:06:06.959: E/AndroidRuntime(914): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1080)
10-22 02:06:06.959: E/AndroidRuntime(914): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:622)
10-22 02:06:06.959: E/AndroidRuntime(914): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1416)
10-22 02:06:06.959: E/AndroidRuntime(914): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:420)
10-22 02:06:06.959: E/AndroidRuntime(914): at android.os.Handler.handleCallback(Handler.java:730)
10-22 02:06:06.959: E/AndroidRuntime(914): at android.os.Handler.dispatchMessage(Handler.java:92)
10-22 02:06:06.959: E/AndroidRuntime(914): at android.os.Looper.loop(Looper.java:137)
10-22 02:06:06.959: E/AndroidRuntime(914): at android.app.ActivityThread.main(ActivityThread.java:5103)
10-22 02:06:06.959: E/AndroidRuntime(914): at java.lang.reflect.Method.invokeNative(Native Method)
10-22 02:06:06.959: E/AndroidRuntime(914): at java.lang.reflect.Method.invoke(Method.java:525)
10-22 02:06:06.959: E/AndroidRuntime(914): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-22 02:06:06.959: E/AndroidRuntime(914): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-22 02:06:06.959: E/AndroidRuntime(914): at dalvik.system.NativeStart.main(Native Method)
10-22 02:06:09.900: I/Process(914): Sending signal. PID: 914 SIG: 9