0

早些时候,我使用以下代码从 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");

但现在我正在使用以下代码从 Fragment 调用 Fragment:

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
4

2 回答 2

2

我希望您正在寻找通过捆绑从一个片段发送数据到另一个片段。

Fragment fragment = new Fragment();
Bundle bundle = new Bundle();
bundle.putInt(key, value);
fragment.setArguments(bundle);

Bundle 为许多数据类型提供了 put 方法。请参阅http://developer.android.com/reference/android/os/Bundle.html

然后在您的片段中,使用以下命令检索数据(例如在 onCreate() 中):

Bundle bundle = this.getArguments();
int myInt = bundle.getInt(key, defaultValue);
于 2013-10-22T06:14:21.163 回答
0

要获取发送到 ListItemFragment 的数据:

String s = getArguments().getString("category");

要正确创建 ListItemFragment:

getFragmentManager().beginTransaction().replace(R.id.content_frame, fragment1).commit();

应该可以正常工作。我没有专门测试过这个。您的代码没有切换片段,因为您从未调用过 replace()。

于 2013-10-22T06:05:20.003 回答