简体   繁体   English

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

[英]java.lang.ClassCastException: android.support.v7.widget.AppCompatButton cannot be cast to android.support.design.widget.FloatingActionButton

It is not a clean and rebuild issue AT ALL! 这根本不是一个干净的重建问题! Make module will not resolve this issue either. Make模块也无法解决此问题。 This is the only FlotingActionButton I'm using in my code. 这是我在代码中使用的唯一FlotingActionButton。 One solution said this only occurs with an anchor parameter but as you can see I am not using one so I dont know what to make of it. 一种解决方案说,这仅与锚参数一起发生,但是如您所见,我没有使用锚参数,所以我不知道该怎么做。

    <?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>

This is the JAVA code. 这是JAVA代码。 I tried importing the Coordinator Layout but that is not possible. 我尝试导入协调器布局,但这是不可能的。 I also tried declaring the fab var differently just to get the same error : 我也尝试以不同的方式声明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));
            }
        });
    }

}

Your second FloatingActionButton does not have an id, and the one you are trying to cast into a FAB is the CoordinatorLayout. 您的第二个FloatingActionButton没有id,而您尝试投射到FAB的id是CoordinatorLayout。

So, you should set an id in your layout for your fabs like this 因此,您应该像这样在晶圆厂的布局中设置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>

and your fabs to match the new ids like this 和您的工厂匹配这样的新ID

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.lang.ClassCastException:android.support.design.widget.AppBarLayout无法转换为android.support.v7.widget.Toolbar - java.lang.ClassCastException: android.support.design.widget.AppBarLayout cannot be cast to android.support.v7.widget.Toolbar android.support.v7.widget.AppCompatButton 不能转换为 android.widget.LinearLayout - android.support.v7.widget.AppCompatButton cannot be cast to android.widget.LinearLayout java.lang.ClassCastException:android.widget.LinearLayout无法强制转换为android.support.v7.widget.Toolbar - java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.support.v7.widget.Toolbar ClassCastException:java.lang.ClassCastException:android.widget.LinearLayout $ LayoutParams无法强制转换为android.support.v4.widget.DrawerLayout - ClassCastException : java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.support.v4.widget.DrawerLayout java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams 不能转换为 android.support.v7.widget.RecyclerView$LayoutParams - java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.support.v7.widget.RecyclerView$LayoutParams java.lang.ClassCastException:android.widget.RelativeLayout $ LayoutParams无法强制转换为android.support.v7.widget.RecyclerView $ LayoutParams - java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.support.v7.widget.RecyclerView$LayoutParams java.lang.ClassCastException:android.support.constraint.ConstraintLayout无法转换为android.widget.TextView - java.lang.ClassCastException: android.support.constraint.ConstraintLayout cannot be cast to android.widget.TextView 无法实例化以下类,android.support.design.widget.FloatingActionButton - The following classes could not be instantiated,android.support.design.widget.FloatingActionButton 以下类无法实例化android.support.design.widget.floatingactionbutton - the following classes could not be instantiated android.support.design.widget.floatingactionbutton java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams 不能转换为 android.support.constraint.ConstraintLayout$LayoutParams - java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.support.constraint.ConstraintLayout$LayoutParams
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM