I tried everything but it is not working. The apps get crashed again and again. Please, help.
It is an earning app code for java and xml.The error is Caused by: java.lang.ClassCastException: androidx.cardview.widget.CardView cannot be cast to androidx.appcompat.widget.Toolbar at com.aarush.cashkamao.MainActivity.init(MainActivity.java:58) at com.aarush.cashkamao.MainActivity.onCreate(MainActivity.java:35)
My code is as follow for 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();
}
});
}
}
Code for activity_main.xml is
<?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>
You're trying to initialize the toolbar
with the card
widget. You have passed the wrong widget (card type) id to the toolbar. I guess you have done that by mistake.
The correct initialization should be done with the correct widget (toolbar) id as:
toolbar = findViewById(R.id.dailyCheckCard);
should be toolbar = findViewById(R.id.toolbar);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.