我正在android (sdk 27)中开发一个应用程序,我必须创建一个应该是动态的条目数据布局。我想是零碎的。首先,我已经有3个选项卡(一般,详细,注释),我想使“详细”选项卡内容变量。我有3张这个标签的布局。
我已经创建了两个片段,但是当我运行该程序时,我得到了以下错误:
Process: ar.com.codigose.atmobile, PID: 13014
java.lang.RuntimeException: ar.com.codigose.atmobile.CargaDataEnsayoActivity@89c8433 must implement OnFragmentInteractionListener
at ar.com.codigose.atmobile.CobbFragment.onAttach(CobbFragment.java:86)
...
...问题是:我是否必须在主活动(管理选项卡的活动)上添加: OnFragmentInteractionListener?还是有别的办法?
我的想法是,我将以编程的方式(使用旋转器)更改“细节”选项卡中的片段,因此我认为不需要添加侦听器.
这是我想以编程方式更改片段的布局:
fragment_repeticion_enfermedad_var.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
tools:context=".RepeticionEnfermedadVarFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:id="@+id/tv_enfermedad"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/hello_blank_fragment"
android:textSize="18sp"
android:textStyle="bold" />
<Spinner
android:id="@+id/spinner_enfermedad"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:drawable/spinner_dropdown_background" />
<FrameLayout
android:id="@+id/frag_enfermedad"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</LinearLayout>这就是我如何动态改变碎片。
...
Fragment fragment = new CobbFragment(); // Luego seria variable
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.replace(R.id.frag_enfermedad, fragment).commit();
...我使用getChildFragmentManager(),因为当您想要在另一个片段中使用片段时,需要阅读它。
有什么建议吗?
诚挚的问候
发布于 2019-06-22 16:20:14
您应该在OnFragmentInteractionListener类中实现CargaDataEnsayoActivity,或者在片段的onAttach中删除此检查。
https://stackoverflow.com/questions/56717087
复制相似问题