gpt4 book ai didi

java - 无法获取 fragment 中的参数

转载 作者:行者123 更新时间:2023-12-01 12:25:34 26 4
gpt4 key购买 nike

我遇到了不便,希望我能提供帮助。我有一个扩展 Fragment 的类,您在其中创建了一个实例来接收参数

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

}

我有 Activity 类,我可以通过 ListView 中的事件单击调用 Fragment

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

....

}

如上所述,我提出调用 Fragment 的问题,它会生成错误,特别是在调用 Fragment 的 onCreate 事件时

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)

对于这一切,我注意到删除 Fragmen 的 onCreate 方法我没有错误,但无法获取参数的值。

非常感谢您的帮助。

PD:抱歉我的英语不好

最佳答案

这是您的问题:

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

您的 newInstance() 方法返回一个 FragmentPlaySong 实例。因此,本质上,在这一行中,您分配了一个新的 FragmentPlaySong 实例并将其丢弃。相反,捕获该结果并使用该实例:

FragmentPlaySong fragment = FragmentPlaySong.newInstance(songs, randomList, position);

关于java - 无法获取 fragment 中的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26371395/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com