【问题标题】:1 Class in two FrameLayout两个FrameLayout中的1个类
【发布时间】:2016-05-06 05:53:18
【问题描述】:

我试图将同一个类放在两个不同的“FrameLayout”上。我的目标是可以将同一个对象(Processing 图形)可视化两次。

我的素描课:

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 的容器 ID{3b88ed7f id=0x7f0c0050}:原为 2131492944 现在为 2131492945

【问题讨论】:

    标签: java android xml android-fragments


    【解决方案1】:

    您需要有 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();
    

    【讨论】:

      猜你喜欢
      • 2018-03-26
      • 1970-01-01
      • 2015-08-27
      • 1970-01-01
      • 2021-10-31
      • 1970-01-01
      • 2022-11-10
      • 2013-02-08
      • 1970-01-01
      相关资源
      最近更新 更多