imandolatkia / Android-Animated-Theme-Manager

create your custom themes and change them dynamically with ripple animation
Apache License 2.0
704 stars 64 forks source link

java.lang.ClassCastException: android.view.ViewRootImpl cannot be cast to android.view.View #26

Open molodoychelovek opened 5 months ago

molodoychelovek commented 5 months ago

🐛 Describe the bug The issue arises in the ThemeManager class within the application. Specifically, there seems to be an error related to class casting in the getRelativeLeft and getRelativeTop methods.

⚠️ Current behavior When certain conditions are met, the application crashes with a ClassCastException in the ThemeManager class.

Error:

java.lang.ClassCastException: android.view.ViewRootImpl cannot be cast to android.view.View
at com.dolatkia.animatedThemeManager.ThemeManager.getRelativeLeft(ThemeManager.kt:133)
at com.dolatkia.animatedThemeManager.ThemeManager.getRelativeLeft(ThemeManager.kt:133)
at com.dolatkia.animatedThemeManager.ThemeManager.getRelativeLeft(ThemeManager.kt:133)
at com.dolatkia.animatedThemeManager.ThemeManager.getRelativeLeft(ThemeManager.kt:133)
at com.dolatkia.animatedThemeManager.ThemeManager.getRelativeLeft(ThemeManager.kt:133)
at com.dolatkia.animatedThemeManager.ThemeManager.getRelativeLeft(ThemeManager.kt:133)
at com.dolatkia.animatedThemeManager.ThemeManager.getRelativeLeft(ThemeManager.kt:133)
at com.dolatkia.animatedThemeManager.ThemeManager.getRelativeLeft(ThemeManager.kt:133)
at com.dolatkia.animatedThemeManager.ThemeManager.getRelativeLeft(ThemeManager.kt:133)
at com.dolatkia.animatedThemeManager.ThemeManager.getRelativeLeft(ThemeManager.kt:133)
at com.dolatkia.animatedThemeManager.ThemeManager.getRelativeLeft(ThemeManager.kt:133)
at com.dolatkia.animatedThemeManager.ThemeManager.getRelativeLeft(ThemeManager.kt:133)
at com.dolatkia.animatedThemeManager.ThemeManager.getViewCoordinates(ThemeManager.kt:127)
at com.dolatkia.animatedThemeManager.ThemeManager.changeTheme(ThemeManager.kt:48)
at .Fragments.Profile.FgProfileSettings.lambda$onViewCreated$3$....-Fragments-Profile-FgProfileSettings(FgProfileSettings.java:151)
at .Fragments.Profile.FgProfileSettings$$ExternalSyntheticLambda16.onCheckedChanged(D8$$SyntheticClass:0)
at android.widget.CompoundButton.setChecked(CompoundButton.java:222)
at androidx.appcompat.widget.SwitchCompat.setChecked(SwitchCompat.java:1179)
at androidx.appcompat.widget.SwitchCompat.toggle(SwitchCompat.java:1174)
at android.widget.CompoundButton.performClick(CompoundButton.java:144)
at android.view.View.performClickInternal(View.java:7519)
at android.view.View.-$$Nest$mperformClickInternal(Unknown Source:0)
at android.view.View$PerformClick.run(View.java:29476)
at android.os.Handler.handleCallback(Handler.java:942)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loopOnce(Looper.java:201)
at android.os.Looper.loop(Looper.java:288)
at android.app.ActivityThread.main(ActivityThread.java:7918)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)

Fragment:

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.SwitchCompat;

import com.dolatkia.animatedThemeManager.AppTheme;
import com.dolatkia.animatedThemeManager.ThemeFragment;
import com.dolatkia.animatedThemeManager.ThemeManager;

public class FgProfileSettings extends ThemeFragment {
    private SwitchCompat sw_theme;
    private CmpTextView tv_theme;
    private RelativeLayout rl_theme;
    private View v_theme;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View this_view = inflater.inflate(R.layout.fg_profile_settings, container, false);

        rl_theme = this_view.findViewById(R.id.rl_theme);
        tv_theme = this_view.findViewById(R.id.tv_theme);
        v_theme = this_view.findViewById(R.id.v_theme);

        return this_view;
    }

    public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        sw_theme.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if(isChecked) {
                ThemeManager.Companion.getInstance().changeTheme(new ThemesDay(), v_theme, 600);
            } else {
                ThemeManager.Companion.getInstance().changeTheme(new ThemesNight(), v_theme, 600);
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void syncTheme(@NonNull AppTheme appTheme) {
        ITheme myAppTheme = (ITheme) appTheme;
        tv_theme.setTextColor(myAppTheme.setColor(requireContext(), EnumColor.BLACK));
    }
}

Interface:

public interface ITheme extends AppTheme {
    int setColor(@NotNull Context context, EnumColor color);
}

ThemeDay:

public class ThemesDay implements ITheme {
    public int id() { // set unique iD for each theme
        return 0;
    }

    @Override
    public int setColor(@NotNull Context context, EnumColor color) {
        return Color.parseColor(getColor(color));
    }

    private String getColor(EnumColor color){
        switch (color){
            case WHITE:
                return "#FFFFFF";
            default:
                return "#FFFFFF";
        }
    }
}

ThemeNight

public class ThemesNight implements ITheme {
    public int id() { // set unique iD for each theme
        return 0;
    }

    @Override
    public int setColor(@NotNull Context context, EnumColor color) {
        return Color.parseColor(getColor(color));
    }

    private String getColor(EnumColor color){
        switch (color){
            case WHITE:
                return "#181F2A";
            default:
                return "#181F2A";
        }
    }
}
timberou commented 3 weeks ago

Have you solved this? I encountered the same problem.