两个FrameLayout

时间:2016-01-28 17:53:37

标签: java android xml android-fragments

我试图将同一个班级放在两个不同的#34; FrameLayout"上。我的目标是可以将同一个对象(Processing图形)可视化两次。

我的Sketch课程:

public class Sketch extends PApplet {
    BarChart barChart;

    public void setup(){

    }
    public void draw(){

    }

    public void settings() {
        size(width,height);
        smooth();
    }
}

我的XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.unicaribe.proyecto.barras.MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1">
            <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/contenedor">
            </FrameLayout>
        </LinearLayout>

        <LinearLayout
            android:layout_width="10dp"
            android:layout_height="match_parent" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1">
            <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:paddingRight="5dp"
                android:id="@+id/contenedor2">
            </FrameLayout>
        </LinearLayout>

    </LinearLayout>
</RelativeLayout>

我的主要活动:

public class MainActivity extends Activity {

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

    Fragment fragment= new Sketch();

    FragmentManager fragmentManager= getFragmentManager();
    fragmentManager.beginTransaction()
            .replace(R.id.contenedor, fragment)
            .commit();

    FragmentManager fragmentManager2= getFragmentManager();
    fragmentManager2.beginTransaction()
            .replace(R.id.contenedor2, fragment)
            .commit();
}

我的错误就是这个

  

java.lang.IllegalStateException:无法更改片段Sketch {3b88ed7f id = 0x7f0c0050}的容器ID:现在是2131492944 2131492945

1 个答案:

答案 0 :(得分:0)

您需要有2个您尝试显示的片段实例:

Fragment firstFragment= new Sketch();
Fragment secondFragment= new Sketch();

    FragmentManager fragmentManager= getFragmentManager();
    fragmentManager.beginTransaction()
            .replace(R.id.contenedor, firstFragment)
            .commit();

    fragmentManager.beginTransaction()
            .replace(R.id.contenedor2, secondFragment)
            .commit();