如何fixandroid.support.v7.widget.AppCompatButton无法强制转换为android.view.ViewGroup

时间:2017-06-17 06:47:22

标签: java android android-fragments

我正在开发一个APP,但是我发现执行错误,当我点击按钮时尝试启动一个新片段时,我尝试修复它,但我不能。

我的目标是在单击按钮时打开一个新片段

SpaceFragment1.java

    public class Space1Fragment extends Fragment{


        private Button dss;



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

            View v = inflater.inflate(R.layout.fragment_space1, container, false);



            dss = (Button) v.findViewById(R.id.dss);

            dss.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(v.getContext(),"dss",Toast.LENGTH_LONG).show();
                    LogrosFragment fragment2 = new LogrosFragment();
                    FragmentManager fragmentManager = getFragmentManager();
                    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                    fragmentTransaction.replace(R.id.dss, fragment2);
                    fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
                    fragmentTransaction.addToBackStack(null);
                    fragmentTransaction.commit();
                }
            });

            return v;
        }

    }

fragment_space1

<android.support.constraint.ConstraintLayout 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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    tools:context="com.tfm.miguel.planetaria.Fragments.Space1Fragment"
    tools:layout_editor_absoluteY="25dp"
    tools:layout_editor_absoluteX="0dp">
    <Button
        android:id="@+id/dessi"
        android:visibility="gone"
        android:layout_width="59dp"
        android:layout_height="91dp"
        android:src="@drawable/naranja"
        android:clickable="true"
        tools:ignore="MissingConstraints"
        app:layout_constraintRight_toLeftOf="@+id/ic"
        tools:layout_constraintTop_creator="1"
        tools:layout_constraintRight_creator="1"
        android:layout_marginStart="39dp"
        android:layout_marginEnd="37dp"
        android:layout_marginTop="19dp"
        tools:layout_constraintLeft_creator="1"
        app:layout_constraintLeft_toLeftOf="@+id/conquista1"
        app:layout_constraintTop_toTopOf="@+id/conquista1"
        app:layout_constraintHorizontal_bias="0.285"
        android:layout_marginRight="37dp" />

    <Button
        android:id="@+id/dss"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@drawable/naranja"
        android:clickable="true"
        tools:ignore="MissingConstraints"
        android:layout_marginEnd="97dp"
        tools:layout_constraintRight_creator="1"
        tools:layout_constraintBottom_creator="1"
        android:layout_marginStart="97dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        tools:layout_constraintLeft_creator="1"
        app:layout_constraintLeft_toRightOf="@+id/dessi"
        app:layout_constraintHorizontal_bias="0.583"
        android:layout_marginBottom="16dp"
        android:onClick="onClick"/>

    <ImageButton
        android:id="@+id/pgpi"
        android:layout_width="67dp"
        android:layout_height="87dp"
        android:src="@drawable/naranja"
        android:clickable="true"
        tools:ignore="MissingConstraints"
        android:layout_marginEnd="109dp"
        tools:layout_constraintRight_creator="1"
        tools:layout_constraintBottom_creator="1"
        app:layout_constraintBottom_toTopOf="@+id/tid"
        app:layout_constraintRight_toRightOf="@+id/conquista2"
        android:layout_marginBottom="26dp"
        tools:layout_editor_absoluteX="208dp" />

    <ImageButton
        android:id="@+id/ic"
        android:layout_width="72dp"
        android:layout_height="106dp"
        android:src="@drawable/naranja"
        android:clickable="true"
        android:layout_marginStart="55dp"
        tools:ignore="MissingConstraints"
        android:layout_marginEnd="53dp"
        tools:layout_constraintTop_creator="1"
        tools:layout_constraintRight_creator="1"
        tools:layout_constraintBottom_creator="1"
        app:layout_constraintBottom_toBottomOf="@+id/dss"
        app:layout_constraintRight_toRightOf="@+id/pgpi"
        android:layout_marginTop="135dp"
        tools:layout_constraintLeft_creator="1"
        android:layout_marginBottom="138dp"
        app:layout_constraintTop_toTopOf="@+id/pgpi"
        app:layout_constraintLeft_toRightOf="@+id/cc" />

    <ImageButton
        android:id="@+id/tid"
        android:layout_width="117dp"
        android:layout_height="111dp"
        android:src="@drawable/naranja"
        android:clickable="true"
        tools:ignore="MissingConstraints"
        tools:layout_constraintRight_creator="1"
        tools:layout_constraintBottom_creator="1"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        tools:layout_constraintLeft_creator="1"
        android:layout_marginBottom="156dp"
        app:layout_constraintLeft_toRightOf="@+id/pgpi"
        tools:layout_editor_absoluteX="267dp" />

    <ImageButton
        android:id="@+id/cc"
        android:layout_width="69dp"
        android:layout_height="153dp"
        android:src="@drawable/naranja"
        android:clickable="true"
        android:layout_marginStart="26dp"
        tools:ignore="MissingConstraints"
        tools:layout_constraintTop_creator="1"
        android:layout_marginTop="19dp"
        app:layout_constraintTop_toBottomOf="@+id/ic"
        tools:layout_constraintLeft_creator="1"
        app:layout_constraintLeft_toLeftOf="parent" />

    <ImageView
        android:background="@color/colorPrimary"
        android:id="@+id/conquista1"
        android:scaleType="fitXY"
        android:paddingRight="20dp"
        android:paddingBottom="50dp"
        android:scaleX="1.2"
        android:scaleY="1.2"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:src="@drawable/mundo01"
        tools:layout_constraintTop_creator="1"
        tools:layout_constraintRight_creator="1"
        tools:layout_constraintBottom_creator="1"
        app:layout_constraintBottom_toBottomOf="@+id/cc"
        app:layout_constraintRight_toRightOf="parent"

        tools:layout_constraintLeft_creator="1"

        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="@+id/pgpi" />

</android.support.constraint.ConstraintLayout>

错误是:

        06-17 08:33:16.056 2403-2403/com.tfm.miguel.planetaria E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                 Process: com.tfm.miguel.planetaria, PID: 2403
                                                                                 java.lang.ClassCastException: android.support.v7.widget.AppCompatButton cannot be cast to android.view.ViewGroup
                                                                                     at android.support.v4.app.FragmentTransition.configureTransitionsUnoptimized(FragmentTransition.java:284)
                                                                                     at android.support.v4.app.FragmentTransition.startTransitions(FragmentTransition.java:113)
                                                                                     at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2146)
                                                                                     at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2103)
                                                                                     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2013)
                                                                                     at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:710)
                                                                                     at android.os.Handler.handleCallback(Handler.java:751)
                                                                                     at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                     at android.os.Looper.loop(Looper.java:154)
                                                                                     at android.app.ActivityThread.main(ActivityThread.java:6077)
                                                                                     at java.lang.reflect.Method.invoke(Native Method)
                                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
                                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)

2 个答案:

答案 0 :(得分:4)

问题出在这一行

fragmentTransaction.replace(R.id.dss, fragment2);

R.id.dss是你的按钮的id,你正在尝试做的是替换作为View的片段,这就是你得到错误的原因。

答案 1 :(得分:1)

阅读 Nested Fragments

  

使用Android 4.2 APIv17,支持Nested Fragments Where   你可以在其他片段中使用片段。

ConstraintLayout 的结束标记上方创建 FrameLayout

    <FrameLayout
            android:id="@+id/child_fragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

 </android.support.constraint.ConstraintLayout>

然后在 ID 部分中添加 FrameLayout fragmentTransaction.replace child_fragment

 LogrosFragment fragment2 = new LogrosFragment();
                FragmentManager fragmentManager = getFragmentManager();
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.replace(R.id.child_fragment, fragment2);
                fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
                fragmentTransaction.addToBackStack(null);
                fragmentTransaction.commit();