我正在尝试通过一个活动从fragmentA传递一些数据,以便在fragmentB中启动一个动作。 它应该工作的方式是,用户按下listview(listFragment)的项目,项目数据被发送到另一个片段(已经可见并且已经运行),以使用来自fragmentA的数据开始新的事件/动作。
FragmentA:
@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();
}
我已经检查过,数据从fragmentA
传递到activity
,但在尝试接收fragmentB
时会崩溃。
怎么办?
(fragmentB的parrent活动与fragmentA的活动不同。这可以与它有关吗?)
logcat的:
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();
}
答案 0 :(得分:1)
在您的活动的onTrackSelected()
方法中,您使用topBarFragment
两次。第一个可能来自您的布局文件(或来自之前的事务)。无论如何,这第一个片段还没有参数,但无论如何你都会调用onTrackSelected()
。你应该把电话放在最后。
并检查为什么在方法结束时重新创建它(与此问题无关,但可能不必要)。