1

我正在尝试通过活动从fragmentA 传递一些数据以在fragmentB 中启动操作。它应该工作的方式是,用户按下一个项目ia listview(listFragment),项目数据被发送到另一个片段(它是可见的并且已经运行)以使用来自fragmentA的数据开始一个新的事件/动作。

片段A:

@Override
public void onListItemClick(ListView listView, View view, int position,
        long longId) {
    super.onListItemClick(listView, view, position, longId);

    String trackId = SD_PATH + songs.get(position);
    mCallbacks.onTrackSelected(trackId);

通过活动:

@Override
public void onTrackSelected(String trackId) {

            // This starts onTrackSelected() method in fragmentB
    topBarFragment topBarFragment = (topBarFragment)getSupportFragmentManager().findFragmentById(R.id.topBar_fragment);
    topBarFragment.onTrackSelected();

    Bundle bundle = new Bundle();
    bundle.putString("trackId", trackId);
      //set Fragmentclass Arguments
    topBarFragment fragObject = new topBarFragment();
    fragObject.setArguments(bundle);

}

在 fragmentB 中检索数据(这是崩溃的地方):

public void onTrackSelected() {

    // crashes on this line
    String trackId = getArguments().getString("trackId");
    Toast.makeText(getActivity().getApplicationContext(), trackId,Toast.LENGTH_SHORT).show();

}

我已经检查过了,数据从fragmentAto传递得很好activity,但是在尝试接收它时它崩溃了fragmentB

该怎么办?

(fragmentB 的父活动与 fragmentA 的不一样。这可能与它有关吗?)

日志猫:

02-24 23:28:47.757: E/AndroidRuntime(14047): FATAL EXCEPTION: main
02-24 23:28:47.757: E/AndroidRuntime(14047): java.lang.NullPointerException
02-24 23:28:47.757: E/AndroidRuntime(14047):    at com.harteg.fragmentstest.topBarFragment.onTrackSelected(topBarFragment.java:47)
02-24 23:28:47.757: E/AndroidRuntime(14047):    at com.harteg.fragmentstest.ItemListActivity.onTrackSelected(ItemListActivity.java:83)
02-24 23:28:47.757: E/AndroidRuntime(14047):    at com.harteg.fragmentstest.TracksFragment.onListItemClick(TracksFragment.java:136)
02-24 23:28:47.757: E/AndroidRuntime(14047):    at android.support.v4.app.ListFragment$2.onItemClick(ListFragment.java:58)
02-24 23:28:47.757: E/AndroidRuntime(14047):    at android.widget.AdapterView.performItemClick(AdapterView.java:298)
02-24 23:28:47.757: E/AndroidRuntime(14047):    at android.widget.AbsListView.performItemClick(AbsListView.java:1100)
02-24 23:28:47.757: E/AndroidRuntime(14047):    at android.widget.AbsListView$PerformClick.run(AbsListView.java:2749)
02-24 23:28:47.757: E/AndroidRuntime(14047):    at android.widget.AbsListView$1.run(AbsListView.java:3423)
02-24 23:28:47.757: E/AndroidRuntime(14047):    at android.os.Handler.handleCallback(Handler.java:725)
02-24 23:28:47.757: E/AndroidRuntime(14047):    at android.os.Handler.dispatchMessage(Handler.java:92)
02-24 23:28:47.757: E/AndroidRuntime(14047):    at android.os.Looper.loop(Looper.java:137)
02-24 23:28:47.757: E/AndroidRuntime(14047):    at android.app.ActivityThread.main(ActivityThread.java:5039)
02-24 23:28:47.757: E/AndroidRuntime(14047):    at java.lang.reflect.Method.invokeNative(Native Method)
02-24 23:28:47.757: E/AndroidRuntime(14047):    at java.lang.reflect.Method.invoke(Method.java:511)
02-24 23:28:47.757: E/AndroidRuntime(14047):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-24 23:28:47.757: E/AndroidRuntime(14047):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-24 23:28:47.757: E/AndroidRuntime(14047):    at dalvik.system.NativeStart.main(Native Method)

更新:

我像这样更新了我的代码,发现 getArguments() 返回 null .. 为什么会这样?

    Bundle arguments = getArguments();
    if (arguments != null)
    {
        String trackId = getArguments().getString("trackId");
        Toast.makeText(getActivity().getApplicationContext(), trackId,Toast.LENGTH_SHORT).show();

    } else {
        Toast.makeText(getActivity().getApplicationContext(), "arguments returns null", Toast.LENGTH_SHORT).show();
    }

更新

我像这样更新了活动中的 onTrackSelected() ,但现在它崩溃并说片段已经处于活动状态。你如何解决这个问题?

@Override
public void onTrackSelected(String trackId) {
    // TODO Auto-generated method stub

        topBarFragment topBarFragment = (topBarFragment)getSupportFragmentManager().findFragmentById(R.id.topBar_fragment);

        Bundle bundle = new Bundle();
        bundle.putString("trackId", trackId);
          //set Fragmentclass Arguments
//      topBarFragment fragObject = new topBarFragment();
        topBarFragment.setArguments(bundle);

        topBarFragment.onTrackSelected();

    }
4

1 回答 1

1

在您的活动onTrackSelected()方法中,您使用topBarFragment了两次。第一个可能来自您的布局文件(或来自以前的事务)。无论如何,这个第一个片段还没有参数,但你还是调用onTrackSelected()了。你应该把电话放在最后。

并检查为什么在方法结束时重新创建它(与此问题无关,但可能没有必要)。

于 2013-02-25T11:46:06.753 回答