2

I have an inconvenience and hope me can help. I have a class that extends Fragment in which you have created an instance to receive parameters

public class FragmentPlaySong extends Fragment  {

....

private ArrayList<Song> songList;
private int randomList = 0;
private int currentSongPosition = 0;

....


public static FragmentPlaySong newInstance(ArrayList<Song> songList, int randomList, int position){
    FragmentPlaySong fragmentPlaySong = new FragmentPlaySong();
    Bundle args = new Bundle();
    args.putSerializable("songs",songList);
    args.putInt("randomList",randomList);
    args.putInt("position",position);
    fragmentPlaySong.setArguments(args);
    return fragmentPlaySong;
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    final View view = inflater.inflate(R.layout.fragment_play_song, container, false);

    ......

    return view;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    songList = (ArrayList<Song>) getArguments().getSerializable("songs");
    randomList = getArguments().getInt("randomList");
    currentSongPosition = getArguments().getInt("position");
}

@Override
public void onActivityCreated(Bundle state) {
    super.onActivityCreated(state);
    ....

}

I have the Activity class from where I invoke the Fragment from event click in ListView

public class MainActivity extends Activity {

private ListView lstSong;    
private ArrayList<Song> songs = new ArrayList<Song>();
private int randomList = 0;    

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ....

    lstSong = (ListView) findViewById(R.id.lstsong);

    lstSong.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
            FragmentPlaySong fragmentPlaySong = new FragmentPlaySong();
            fragmentPlaySong.newInstance(songs,randomList,position);                
            getFragmentManager().beginTransaction().replace(R.id.FrgPlaySong, fragmentPlaySong).commit();
        }
    });        
}

....

}

Lines as described above, the problem I presented to invoke the Fragment, it generates an error, specifically when invoking the onCreate event Fragment

10-14 16:52:37.291  20744-20744/pe.com.mkella.simpleamp E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: pe.com.mkella.simpleamp, PID: 20744
java.lang.RuntimeException: Unable to start activity ComponentInfo{pe.com.mkella.simpleamp/pe.com.mkella.simpleamp.MainActivity}: android.view.InflateException: Binary XML file line #31: Error inflating class fragment
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2212)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2271)
        at android.app.ActivityThread.access$800(ActivityThread.java:144)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5146)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)

To all this I have noticed that removing the onCreate method of Fragmen I not have error, but could not get the value of the arguments.

I would appreciate your help.

PD: Sorry for my bad english

Brando T.
  • 173
  • 1
  • 7

4 Answers4

2

Here is your problem:

FragmentPlaySong fragmentPlaySong = new FragmentPlaySong();
fragmentPlaySong.newInstance(songs,randomList,position);                

Your newInstance() method returns a FragmentPlaySong instance. So essentially at this line, you're allocating a new FragmentPlaySong instance and throwing it away. Instead, capture that result, and use that instance:

FragmentPlaySong fragment = FragmentPlaySong.newInstance(songs, randomList, position);
Kevin Coppock
  • 133,643
  • 45
  • 263
  • 274
1

You do not need to get your arguments in onCreate (in fact, I don't think that's the right way in a Fragment). Try moving the argument getting logic to the top of onCreateView() instead, and remove onCreate().

emerssso
  • 2,376
  • 18
  • 24
0

After struggling with this, I could avoid this error that turned out to be so simple, here the solution:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Bundle bundle = getArguments();
    if (bundle != null) {
        currentSongPosition = bundle.getInt("position", 0);
        songList = (ArrayList<Song>) bundle.getSerializable("songs");
        randomList = bundle.getInt("randomList");
    }
}

Thanks for you attention

Brando T.
  • 173
  • 1
  • 7
  • This is actually a good answer. Just move `arguments` into `onCreateView` and do not use `requireArguments()` in kotlin for this, as it throws a `IllegalStateException` due to no arguments present. – Hmerman6006 Apr 23 '23 at 20:16
-2

Try to use FragmentActivity instead of Activity.

Does it help?

Rodrigo Borges
  • 474
  • 3
  • 10