当前位置: 首页 > 知识库问答 >
问题:

错误AndroidX.CardView.Widget.CardView不能强制转换为AndroidX.AppCompat.Widget.Toolbar

常雪风
2023-03-14

我试了所有的办法,但都不起作用。应用程序一次又一次崩溃。拜托,救命。

这是用于Java和XML的应用程序代码。错误为由以下原因引起:java.lang.ClassCastException:AndroidX.CardView.Widget.CardView无法强制转换为位于com.aarush.cashkamao.mainactivity.init(mainactivity.java:58)的com.aarush.cashkamao.mainactivity.oncreate(mainactivity.java:35)

我的mainactivity.java代码如下所示

package com.aarush.cashkamao;

import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.widget.Toolbar;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.cardview.widget.CardView;
import com.aarush.cashkamao.model.ProfileModel;
import com.bumptech.glide.Glide;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import de.hdodenhof.circleimageview.CircleImageView;

public class MainActivity extends AppCompatActivity {

    private CardView dailyCheckCard, luckyCard, taskCard, redeemCard, referCard, watchCard, aboutCard;
    private CircleImageView profileImage;
    private TextView coinsTv, nameTv, emailTv;
    Toolbar toolbar;
    DatabaseReference reference;
    private FirebaseUser user;

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

        init();

        setSupportActionBar(toolbar);

        FirebaseAuth  auth = FirebaseAuth.getInstance();
        user = auth.getCurrentUser();
        reference = FirebaseDatabase.getInstance().getReference().child("Users");
        getDataFromDatabase();

    }

    private void init(){
        dailyCheckCard = findViewById(R.id.dailyCheckCard);
        luckyCard = findViewById(R.id.luckySpinCard);
        taskCard = findViewById(R.id.taskCard);
        redeemCard = findViewById(R.id.redeemCard);
        referCard = findViewById(R.id.referCard);
        watchCard = findViewById(R.id.watchCard);
        aboutCard = findViewById(R.id.aboutCard);
        profileImage = findViewById(R.id.profileImage);
        coinsTv = findViewById(R.id.coinsTv);
        nameTv = findViewById(R.id.nameTv);
        emailTv = findViewById(R.id.emailTv);
        toolbar = findViewById(R.id.dailyCheckCard);
    }

    private void getDataFromDatabase(){
        reference.child(user.getUid()).addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                ProfileModel model = snapshot.getValue(ProfileModel.class);
                nameTv.setText(model.getName());
                emailTv.setText(model.getEmail());
                coinsTv.setText(String.valueOf(model.getCoins()));
                Glide.with(getApplicationContext())
                        .load(model.getImage())
                        .timeout(6000)
                        .placeholder(R.drawable.profile)
                        .into(profileImage);
            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {
                Toast.makeText(MainActivity.this, "Error: " + error.getMessage(), Toast.LENGTH_SHORT).show();
                finish();
            }
        });
    }
}

activity_main.xml的代码是

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <RelativeLayout
        android:id="@+id/relativeLayout"
        android:layout_width="match_parent"
        android:layout_height="250dp"
        android:background="@drawable/main_bg"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:ignore="MissingConstraints">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:layout_alignParentTop="true"
            android:background="@android:color/transparent">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <TextView
                    android:id="@+id/coinsTv"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_alignParentEnd="true"
                    android:layout_marginEnd="12dp"
                    android:drawablePadding="12dp"
                    android:gravity="center_vertical"
                    android:text="4500"
                    android:textColor="#fff"
                    android:textSize="18sp"
                    android:textStyle="bold"
                    app:drawableEndCompat="@drawable/ic_coins" />

            </RelativeLayout>

        </androidx.appcompat.widget.Toolbar>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true">

            <de.hdodenhof.circleimageview.CircleImageView
                android:id="@+id/profileImage"
                android:layout_width="90dp"
                android:layout_height="90dp"
                android:background="@drawable/profile"
                app:civ_border_color="#39497A"
                app:civ_border_width="2dp" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_toEndOf="@+id/profileImage"
                android:gravity="center_vertical"
                android:orientation="vertical"
                android:padding="8dp">

                <TextView
                    android:id="@+id/nameTv"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Username"
                    android:textColor="#fff"
                    android:textSize="20sp"
                    android:textStyle="bold" />

                <TextView
                    android:id="@+id/emailTv"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Mail"
                    android:textColor="#fff"
                    android:textSize="20sp"
                    android:textStyle="bold" />
            </LinearLayout>

        </RelativeLayout>

    </RelativeLayout>

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintBottom_toBottomOf="@+id/relativeLayout"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/relativeLayout"
        tools:ignore="MissingConstraints">

        <androidx.cardview.widget.CardView
            android:id="@+id/dailyCheckCard"
            android:layout_width="0dp"
            android:layout_height="120dp"
            android:layout_weight="1"
            app:cardCornerRadius="12dp"
            app:cardUseCompatPadding="true">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <ImageView
                    android:layout_width="50dp"
                    android:layout_height="50dp"
                    android:layout_centerInParent="true"
                    android:src="@drawable/daily_check" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentBottom="true"
                    android:layout_centerHorizontal="true"
                    android:padding="8dp"
                    android:text="Daily Check In"
                    android:textColor="#437C95"
                    android:textStyle="bold" />

            </RelativeLayout>

        </androidx.cardview.widget.CardView>

        <androidx.cardview.widget.CardView
            android:id="@+id/luckySpinCard"
            android:layout_width="0dp"
            android:layout_height="120dp"
            android:layout_weight="1"
            app:cardCornerRadius="12dp"
            app:cardUseCompatPadding="true">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <ImageView
                    android:layout_width="70dp"
                    android:layout_height="70dp"
                    android:layout_centerInParent="true"
                    android:src="@drawable/ic_spin" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentBottom="true"
                    android:layout_centerHorizontal="true"
                    android:padding="8dp"
                    android:text="Lucky Spin"
                    android:textColor="#437C95"
                    android:textStyle="bold" />
            </RelativeLayout>

        </androidx.cardview.widget.CardView>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintTop_toBottomOf="@+id/linearLayout">


        <LinearLayout
            android:id="@+id/linearLayout1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            tools:ignore="MissingConstraints">

            <androidx.cardview.widget.CardView
                android:id="@+id/taskCard"
                android:layout_width="0dp"
                android:layout_height="120dp"
                android:layout_weight="1"
                app:cardCornerRadius="12dp"
                app:cardUseCompatPadding="true">

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                    <ImageView
                        android:layout_width="55dp"
                        android:layout_height="55dp"
                        android:layout_centerInParent="true"
                        android:src="@drawable/ic_task" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentBottom="true"
                        android:layout_centerHorizontal="true"
                        android:padding="8dp"
                        android:text="Daily Check In"
                        android:textColor="#437C95"
                        android:textStyle="bold" />

                </RelativeLayout>

            </androidx.cardview.widget.CardView>

            <androidx.cardview.widget.CardView
                android:id="@+id/referCard"
                android:layout_width="0dp"
                android:layout_height="120dp"
                android:layout_weight="1"
                app:cardCornerRadius="12dp"
                app:cardUseCompatPadding="true">

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                    <ImageView
                        android:layout_width="51dp"
                        android:layout_height="51dp"
                        android:layout_centerInParent="true"
                        android:src="@drawable/ic_refer" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentBottom="true"
                        android:layout_centerHorizontal="true"
                        android:padding="12dp"
                        android:text="Refer and Earn"
                        android:textColor="#437C95"
                        android:textStyle="bold" />
                </RelativeLayout>

            </androidx.cardview.widget.CardView>

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            tools:ignore="MissingConstraints">

            <androidx.cardview.widget.CardView
                android:id="@+id/redeemCard"
                android:layout_width="0dp"
                android:layout_height="120dp"
                android:layout_weight="1"
                app:cardCornerRadius="12dp"
                app:cardUseCompatPadding="true">

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                    <ImageView
                        android:layout_width="55dp"
                        android:layout_height="55dp"
                        android:layout_centerInParent="true"
                        android:src="@drawable/ic_task" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentBottom="true"
                        android:layout_centerHorizontal="true"
                        android:padding="8dp"
                        android:text="Task an Survey"
                        android:textColor="#437C95"
                        android:textStyle="bold" />

                </RelativeLayout>

            </androidx.cardview.widget.CardView>

            <androidx.cardview.widget.CardView
                android:id="@+id/watchCard"
                android:layout_width="0dp"
                android:layout_height="120dp"
                android:layout_weight="1"
                app:cardCornerRadius="12dp"
                app:cardUseCompatPadding="true">

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                    <ImageView
                        android:layout_width="51dp"
                        android:layout_height="51dp"
                        android:layout_centerInParent="true"
                        android:src="@drawable/ic_watchearn" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentBottom="true"
                        android:layout_centerHorizontal="true"
                        android:padding="12dp"
                        android:text="Watch and Earn"
                        android:textColor="#437C95"
                        android:textStyle="bold" />
                </RelativeLayout>

            </androidx.cardview.widget.CardView>

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            tools:ignore="MissingConstraints">

            <androidx.cardview.widget.CardView
                android:id="@+id/aboutCard"
                android:layout_width="0dp"
                android:layout_height="120dp"
                android:layout_weight="1"
                app:cardCornerRadius="12dp"
                app:cardUseCompatPadding="true">

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                    <ImageView
                        android:layout_width="55dp"
                        android:layout_height="55dp"
                        android:layout_centerInParent="true"
                        android:src="@drawable/ic_info" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentBottom="true"
                        android:layout_centerHorizontal="true"
                        android:padding="8dp"
                        android:text="About"
                        android:textColor="#437C95"
                        android:textStyle="bold" />

                </RelativeLayout>

            </androidx.cardview.widget.CardView>

        </LinearLayout>

    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

共有1个答案

景永春
2023-03-14

您正在尝试使用小部件初始化工具栏。您向工具栏传递了错误的小部件(卡片类型)id。我想你做错了。

正确的初始化应该使用正确的小部件(工具栏)id进行:

Toolbar=findViewById(r.id.DailyCheckCard);应为Toolbar=findViewById(r.id.Toolbar);

 类似资料: