java - 1 两个FrameLayout中的类

标签 java android xml android-fragments

我试图将同一个类放在两个不同的“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>

我的主要 Activity :

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: Can't change container ID of fragment Sketch{3b88ed7f id=0x7f0c0050}: was 2131492944 now 2131492945

最佳答案

您需要有 2 个要显示的 fragment 实例:

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

关于java - 1 两个FrameLayout中的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35068547/

相关文章:

html - 使用 XSLT 将 XML 转换为 HTML

java - JDBC SQLite 不强制唯一主键约束

java - 在大型 CSV 中查找行的最简单、最快的方法

c# - 从 Android 中的 webview 获取 html 内容(使用 Xamarin 和 C#)

java - 如何避免在 AsyncTask 中传递过多的输入参数?

c# - 如何使用 XmlSerializer 将自定义值设置为 xml header 中的 xmlns

java - Java中类之间的方法可见性

java - 表单验证无法正常工作

android - 手机休眠时的文字转语音 - Android

java - "Fatal Error"以下 nutch 教程 "markup in the document following the root must be well formed"