简体   繁体   中英

App crashes when passing data from activity to fragment

I'm trying to pass some data from fragmentA through an activity to start an action in fragmentB. The way it should work is, user presses an item ia listview(listFragment), items data gets send to another fragment (which is visible and running already) to start a new event/action using the data from 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);

Through activity:

@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);

}

retrieve data in fragmentB (this is where is crashes):

public void onTrackSelected() {

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

}

I've checked and the data gets passes fine from fragmentA to activity , but then it crashes when trying to receive it fragmentB .

What to do?

(fragmentB's parrent activity is not the same as fragmentA's. Can this have something to do with it?)

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)

UPDATE:

I updated my code like this, and found out that getArguments() returns null.. Why is that?

    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();
    }

UPDATE

I updated the onTrackSelected() in the activity like this, but now it crashes and says that fragment is already active. How do you fix this?

@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();

    }

In your Activity's onTrackSelected() method you use topBarFragment twice. The first one probably from your layout file (or from a previous transaction). Anyway, this first fragment doesn't have the arguments yet, but you call onTrackSelected() anyway. You should put the call at the very end.

And check why you created it anew at the end of the method as well (not related to this problem, but probably unnecessary).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM