Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/225.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在替换转换中使用fragment_容器_Android_Android Fragments - Fatal编程技术网

Android 在替换转换中使用fragment_容器

Android 在替换转换中使用fragment_容器,android,android-fragments,Android,Android Fragments,我正在尝试从games.xml到levels.xml的两个片段之间进行转换 它就像愤怒的小鸟游戏菜单: 游戏: 级别: games.java: public class Games extends Fragment implements OnClickListener{ public Games(){} public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInst

我正在尝试从games.xml到levels.xml的两个片段之间进行转换 它就像愤怒的小鸟游戏菜单: 游戏: 级别:

games.java:

public class Games extends Fragment implements OnClickListener{

public Games(){}

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.games, container, false);
    return rootView;
}

@Override
public void onClick(View v) {
    Fragment f = null;
    switch (v.getId()) {
    case R.id.s4:{
        f = new Levels();
        break;
    default:
        break;
    }
    if (f != null) {
        FragmentTransaction transaction = getFragmentManager().beginTransaction();
        transaction.replace(R.id.lvl_container, f);
        transaction.addToBackStack(null);
        transaction.commit();
    } 
        }
 ImageButton im = (ImageButton) rootView.findViewById(R.id.s4);
 im.setOnClickListener(this);
level.java:

public class Levels extends Fragment {

public Levels(){}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.levels, container, false);

    return rootView;
}
}

levels.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/lvl_container"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<TextView
    android:id="@+id/txtLabel"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:textSize="16dp"
    android:text="@string/s4"/>

<ImageButton android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/txtLabel"
    android:src="@drawable/lvlgray_1"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="10dp"/>
但我不知道是什么。有什么想法吗? 我已经看过了(developer.android.com/guide/components/fragments),但是没有帮助!我错过了什么? 有没有其他方法可以在两个片段之间转换?
谢谢

在Games.java的onCreateView中添加这一行:

public class Games extends Fragment implements OnClickListener{

public Games(){}

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.games, container, false);
    return rootView;
}

@Override
public void onClick(View v) {
    Fragment f = null;
    switch (v.getId()) {
    case R.id.s4:{
        f = new Levels();
        break;
    default:
        break;
    }
    if (f != null) {
        FragmentTransaction transaction = getFragmentManager().beginTransaction();
        transaction.replace(R.id.lvl_container, f);
        transaction.addToBackStack(null);
        transaction.commit();
    } 
        }
 ImageButton im = (ImageButton) rootView.findViewById(R.id.s4);
 im.setOnClickListener(this);

这将引用您的imagebutton并在其上设置onClickListener以调用您的onClick方法。

id
lvl\u容器
属于哪个xml?levels.xml(第三行代码,是否有错?)您没有为imagebutton设置侦听器。初始化它并在onCreateViewthank u中为其设置侦听器,但出现错误后:java.lang.IllegalArgumentException:找不到id为0x7f0a0008(info.androidhive.slidingmenu:id/lvl_container)的片段级别{419490c8#1 id=0x7f0a0008}的视图,这两个片段都将由同一活动托管。如果是这样,您可以在活动中使用容器。将接口用作对活动的回调。然后替换片段。碎片1-->活动-->碎片2