Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
molodoychelovek opened this issue Jun 7, 2024 · 1 comment

Comments

@molodoychelovek
Copy link

molodoychelovek commented Jun 7, 2024

🐛 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
Copy link

Have you solved this? I encountered the same problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants