我想使用列表
中的onItemClickListener将片段的布局替换为片段@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch (position) {
case 0:
intent = new Intent(this, Answer.class);
startActivity(intent);
Answer_0 answer_0=new Answer_0();
android.app.FragmentManager fragmentManager=getFragmentManager();
android.app.FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragment_show,answer_0);
fragmentTransaction.commit();
break;
当我点击项目时,应用程序会显示ANR并关闭。
答案 0 :(得分:0)
将此功能添加到您的活动
public void openFragment(Fragment fragment) {
android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.container, fragment).commit();
}
并像这样使用
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch (position) {
case 0:
openFragment(new yourFragment1());
break;
case 1:
openFragment(new yourFragment2());
break;
case 2:
openFragment(new yourFragment3());
break;
}
并确保导入android.support.v4
而不是android.app
在你开启活动后这样做
如果您想在活动开始时加载片段,请将openFragment(new yourFragment())
放在该活动的onCreate
方法上