java.lang.ClassCastException:android.support.v7.widget.AppCompatButton无法强制转换为android.support.design.widget.FloatingActionButton

时间:2017-09-03 02:26:44

标签: java android xml classcastexception floating-action-button

这不是一个干净和重建的问题! Make模块不会 解决这个问题。这是唯一的 FlotingActionButton我在我的代码中使用。一个解决方案只说这个 使用锚参数发生,但正如您所看到的,我没有使用它 所以我不知道该怎么做。

    <?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/coverpic"
    tools:context="com.android.nohiccupsbeta.MainActivity">

    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/effects_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_gravity="top|end"
            android:layout_marginTop="400dp"
            android:layout_marginRight="15dp"
            android:src="@drawable/ic_book"
            app:backgroundTint="@color/primary_color" />

    </android.support.design.widget.CoordinatorLayout>

    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/take_test"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|end"
            android:layout_margin="@dimen/fab_margin"
            app:backgroundTint="@color/primary_color"
            android:src="@drawable/ic_handcuffs" />

    </android.support.design.widget.CoordinatorLayout>

</RelativeLayout>

这是JAVA代码。我尝试导入协调器布局但是 这是不可能的。我也尝试过不同地宣布fab var 只是为了得到同样的错误:

    package com.android.nohiccupsbeta;

import android.content.Intent;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {

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


        FloatingActionButton fab = (FloatingActionButton)findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, pageone.class));
            }
        });


        FloatingActionButton fab2 = (FloatingActionButton)findViewById(R.id.fab2);
        fab2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, BacTest.class));
            }
        });
    }

}

1 个答案:

答案 0 :(得分:2)

您的第二个FloatingActionButton没有id,而您尝试强制转换为FAB的是CoordinatorLayout。

因此,您应该在布局中为您的工厂设置一个id

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/coverpic">

    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/effects_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_gravity="top|end"
            android:layout_marginTop="400dp"
            android:layout_marginRight="15dp"
            android:src="@drawable/ic_book"
            app:backgroundTint="@color/primary_color" />

    </android.support.design.widget.CoordinatorLayout>

    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/take_test"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|end"
            android:layout_margin="@dimen/fab_margin"
            app:backgroundTint="@color/primary_color"
            android:src="@drawable/ic_handcuffs" />

    </android.support.design.widget.CoordinatorLayout>

</RelativeLayout>

和您的工厂匹配像这样的新ID

FloatingActionButton fab = (FloatingActionButton)findViewById(R.id.fab);
FloatingActionButton fab2 = (FloatingActionButton)findViewById(R.id.fab2);