0

I tried to add on my layout "com.google.android.material.card.MaterialCardView" instead of "List view". Now when I try to access this layout in my application, it crashes and I don't know how to add Material Cardview without app crashes... Please help.

This is the activiy_whitelist.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"
    xmlns:fancy="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.d4rk.cleaner.WhitelistActivity">
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="30dp"
        android:layout_marginEnd="8dp"
        android:text="@string/whitelist"
        android:textColor="@color/colorGoogleGreen"
        android:textSize="24sp"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>
    <ImageView
        android:id="@+id/boostView"
        android:layout_width="69dp"
        android:layout_height="69dp"
        android:src="@drawable/ic_boost"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:ignore="ContentDescription"/>
    <ImageView
        android:id="@+id/dustpanView"
        android:layout_width="69dp"
        android:layout_height="69dp"
        android:src="@drawable/ic_dustpan"
        tools:ignore="ContentDescription,MissingConstraints"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>
    <com.google.android.material.card.MaterialCardView
        android:id="@+id/whitelistView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:cardUseCompatPadding="true"
        android:layout_marginStart="24dp"
        app:cardElevation="5dp"
        android:layout_marginTop="10dp"
        app:cardCornerRadius="10dp"
        android:layout_marginEnd="24dp"
        android:layout_marginBottom="8dp"
        android:background="@color/colorPrimaryDark"
        app:layout_constraintBottom_toTopOf="@+id/recommendedButton"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView"/>
    <mehdi.sakout.fancybuttons.FancyButton
        android:id="@+id/resetWhiteList"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_marginEnd="8dp"
        android:onClick="emptyWhitelist"
        android:paddingLeft="0dp"
        android:paddingTop="0dp"
        android:paddingRight="0dp"
        android:paddingBottom="0dp"
        app:layout_constraintBottom_toBottomOf="@+id/textView"
        app:layout_constraintEnd_toStartOf="@+id/textView"
        fancy:fb_borderWidth="1dp"
        fancy:fb_defaultColor="@color/colorPrimaryDark"
        fancy:fb_focusColor="@color/colorAccent"
        fancy:fb_radius="100dp"
        fancy:fb_text=""
        fancy:fb_textColor="@color/colorAccent"
        tools:ignore="UsingOnClickInXml">
        <ImageView
            android:id="@+id/imageView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginBottom="0dp"
            android:layout_weight="1"
            android:contentDescription="@string/todo"
            android:src="@drawable/ic_delete"/>
    </mehdi.sakout.fancybuttons.FancyButton>
    <mehdi.sakout.fancybuttons.FancyButton
        android:id="@+id/addWhiteList"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_marginStart="8dp"
        android:onClick="addToWhiteList"
        android:paddingLeft="0dp"
        android:paddingTop="0dp"
        android:paddingRight="0dp"
        android:paddingBottom="0dp"
        app:layout_constraintBottom_toBottomOf="@+id/textView"
        app:layout_constraintStart_toEndOf="@+id/textView"
        fancy:fb_borderWidth="1dp"
        fancy:fb_defaultColor="@color/colorPrimaryDark"
        fancy:fb_focusColor="@color/colorAccent"
        fancy:fb_radius="100dp"
        fancy:fb_textColor="@color/colorAccent"
        tools:ignore="UsingOnClickInXml"
        fancy:fb_text="">
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginBottom="0dp"
            android:layout_weight="1"
            android:contentDescription="@string/todo"
            android:src="@drawable/ic_plus_4"/>
    </mehdi.sakout.fancybuttons.FancyButton>
    <mehdi.sakout.fancybuttons.FancyButton
        android:id="@+id/recommendedButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="12dp"
        android:onClick="addRecommended"
        android:paddingLeft="20dp"
        android:paddingTop="10dp"
        android:paddingRight="20dp"
        android:paddingBottom="10dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        fancy:fb_defaultColor="@color/colorPrimary"
        fancy:fb_focusColor="@color/colorAndroidGreen"
        fancy:fb_radius="100dp"
        fancy:fb_text="@string/add_defaults"
        fancy:fb_useSystemFont="true"
        fancy:fb_textColor="@color/colorAndroidGreen"
        tools:ignore="UsingOnClickInXml"/>
</androidx.constraintlayout.widget.ConstraintLayout>

And here is java code:

package com.d4rk.cleaner;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import com.fxn.stash.Stash;
import java.io.File;
import java.util.List;
public class WhitelistActivity extends AppCompatActivity {
    ListView listView;
    BaseAdapter adapter;
    private static List<String> whiteList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_whitelist);
        Stash.init(getApplicationContext());
        listView = findViewById(R.id.whitelistView);
        adapter = new ArrayAdapter<>(this, R.layout.custom_textview, getWhiteList());
        listView.setAdapter(adapter);
    }
    /**
     * Clears the whitelist, then sets it up again without loading saved one from stash
     * @param view the view that is clicked
     */
    public final void emptyWhitelist(View view) {
        new AlertDialog.Builder(WhitelistActivity.this,R.style.MyAlertDialogTheme)
                .setTitle(R.string.reset_whitelist)
                .setMessage(R.string.are_you_reset_whitelist)
                .setPositiveButton(R.string.reset, (dialog, whichButton) -> {
                    whiteList.clear();
                    Stash.put("whiteList", whiteList);
                    refreshListView();
                })
                .setNegativeButton(R.string.cancel, (dialog, whichButton) -> { }).show();
    }
    public void addRecommended(View view) {
        File externalDir = Environment.getExternalStorageDirectory();
        if (!whiteList.contains(new File(externalDir, "Music").getPath())) {
            whiteList.add(new File(externalDir, "Music").getPath());
            whiteList.add(new File(externalDir, "Podcasts").getPath());
            whiteList.add(new File(externalDir, "Ringtones").getPath());
            whiteList.add(new File(externalDir, "Alarms").getPath());
            whiteList.add(new File(externalDir, "Notifications").getPath());
            whiteList.add(new File(externalDir, "Pictures").getPath());
            whiteList.add(new File(externalDir, "Movies").getPath());
            whiteList.add(new File(externalDir, "Download").getPath());
            whiteList.add(new File(externalDir, "DCIM").getPath());
            whiteList.add(new File(externalDir, "Documents").getPath());
            Stash.put("whiteList", whiteList);
            refreshListView();
        } else
            Toast.makeText(this, "Already added.",
                    Toast.LENGTH_LONG).show();
    }
    /**
     * Creates a dialog asking for a file/folder name to add to the whitelist
     * @param view the view that is clicked
     */
    public final void addToWhiteList(View view) {
        final EditText input = new EditText(WhitelistActivity.this);
        new AlertDialog.Builder(WhitelistActivity.this,R.style.MyAlertDialogTheme)
                .setTitle(R.string.add_to_whitelist)
                .setMessage(R.string.enter_file_name)
                .setView(input)
                .setPositiveButton(R.string.add, (dialog, whichButton) -> {
                    whiteList.add(String.valueOf(input.getText()));
                    Stash.put("whiteList", whiteList);
                    refreshListView();
                })
                .setNegativeButton(R.string.cancel, (dialog, whichButton) -> { }).show();
    }
    public void refreshListView() {
        runOnUiThread(() -> {
            adapter.notifyDataSetChanged();
            listView.invalidateViews();
            listView.refreshDrawableState();
        });
    }
    public static synchronized List<String> getWhiteList() {
            whiteList=Stash.getArrayList("whiteList", String.class);
        return whiteList;
    }
}

Here is full source code: https://github.com/D4rK7355608/com.d4rk.cleaner

D4rK
  • 1
  • 1
  • Use Logcat to examine the stack trace associated with your crash: https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this – CommonsWare Dec 07 '20 at 14:54

1 Answers1

0

You cannot cast a MaterialCardView to ListView. Variable listView must be of type MaterialCardView.

You may also need to apply the following attribute to your MaterialCardView:

android:theme="@style/Theme.MaterialComponents"