Nescafemix / hintcase

HintCase is a library for Android that will help you create really awesome hints/tips for your apps
Apache License 2.0
402 stars 54 forks source link

java.lang.ClassCastException: android.view.ContextThemeWrapper cannot be cast to android.app.Activity #20

Closed oneroman closed 7 years ago

oneroman commented 7 years ago

code.txt Hello there!

I wish to use the library inside the android.support.design.widget.BottomSheetDialogFragment. And I am passing the proper view into constructor for HintCase, but for Nexus 6 L OS I do have the following issue:

AndroidRuntime: FATAL EXCEPTION: main java.lang.ClassCastException: android.view.ContextThemeWrapper cannot be cast to android.app.Activity at com.joanfuentes.hintcase.utils.DimenUtils.getRealScreenSize(DimenUtils.java:81) at com.joanfuentes.hintcase.utils.DimenUtils.getNavigationBarSizeIfExistAtTheBottom(DimenUtils.java:42) at com.joanfuentes.hintcase.HintCaseView.init(HintCaseView.java:81) at com.joanfuentes.hintcase.HintCaseView.(HintCaseView.java:63) at com.joanfuentes.hintcase.HintCase.(HintCase.java:40)

Therefore, I am suggesting to modify the code slightly in the following manner. Please consider it because it shall work perfectly.

public static Point getRealScreenSize(Context context) { Point size = new Point(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); Display display = windowManager.getDefaultDisplay(); display.getRealSize(size); } else { View decorView = null; if(context instanceof Activity) { decorView = ((Activity) context).getWindow().getDecorView(); } else if(context instanceof ContextThemeWrapper) { Context baseContext = ((ContextThemeWrapper) context).getBaseContext(); if(baseContext instanceof Activity) { decorView = ((Activity) baseContext).getWindow().getDecorView(); } } if(decorView != null) { size.x = decorView.getWidth(); size.y = decorView.getHeight(); } } return size; }

JustTrust commented 7 years ago

Yes, the same problem. If I use this library with DialogFragment I get the same cast error.

Nescafemix commented 7 years ago

@oneroman Thanks! I'm going to investigate it :D @JustTrust I'll see if the proposed solution by @oneroman fix the problem :) Thx!

oneroman commented 7 years ago

@Nescafemix thanks for your attention to that issue! really appreciate it. would you like to get a pull request from me?

Nescafemix commented 7 years ago

@oneroman every help is welcome, but please, if you do, do it against develop branch 👍

oneroman commented 7 years ago

21

@Nescafemix looks good?

oneroman commented 7 years ago

@Nescafemix any dates for 1.0.4 release?

Nescafemix commented 7 years ago

@oneoman thanks. Looks good! Please, give me some time (1-2 days) to check if I need to add some extra case to fix the problem with DialogFragment that has @JustTrust

on the other hand, @JustTrust, could you show me the code that you use to call your Hint?

JustTrust commented 7 years ago

@Nescafemix yes, with pleasure

void showHint(ImageView view, String text) {
        if (mHintCase != null) return;
        CustomHintContentHolder blockInfo = new CustomHintContentHolder.Builder(view.getContext())
                .setContentText(text)
                .setBorder(R.dimen.bubble_border, R.color.colorPrimaryDark)
                .setArrowSize(R.dimen.arrow_width, R.dimen.arrow_height)
                .setBackgroundColor(Color.WHITE)
                .setMarginByResourcesId(R.dimen.side_margin,
                        R.dimen.activity_horizontal_margin,
                        R.dimen.side_margin,
                        R.dimen.activity_horizontal_margin)
                .setContentPaddingByResourcesId(R.dimen.small_margin,
                        R.dimen.small_margin,
                        R.dimen.small_margin,
                        R.dimen.small_margin)
                .setContentStyle(R.style.InformationHint)
                .build();
        if (getView()==null) return;

         mHintCase = new HintCase(getView())
                .setTarget(view, R.dimen.zero_margin)
                .setOnClosedListener(new HintCase.OnClosedListener() {
                    @Override
                    public void onClosed() {
                        hideHintCase();
                    }
                })
                .setBackgroundColor(0x00000000)
                .setHintBlock(blockInfo, new FadeInContentHolderAnimator(),
                        new FadeOutContentHolderAnimator())
                .setCloseOnTouchView(true);
        mHintCase.show();
    }

where getView() we call in DialogFragment. I try to write getActivity() instead of getView() and it works, but hint was showing under DialogFragment in activity )

Nescafemix commented 7 years ago

Thx @JustTrust . I found the problem, and I need to perform a deep refactor to enable the use of the library from Dialogs, so I'm going to launch the fix for @oneroman in the next 2 days and then I'm going to start with the necessary changes to enable the library for dialogs. (version 1.0.5 or maybe even 1.1 or 2.0 XD)

Maybe, that refactor will fix the other bug that you told us on https://github.com/Nescafemix/hintcase/issues/9 Thx again!

oneroman commented 7 years ago

@Nescafemix you mean my fix is not enough to make your lib to work with Dialogs. Except my patch you need to implement something else, am I right?

Nescafemix commented 7 years ago

@oneroman Exactly. I'm going to launch 1.0.4 (with your fix) in next 2 days :)

oneroman commented 7 years ago

@Nescafemix let me know if I can help u to fix Dialog issue, se we could release it faster

JustTrust commented 7 years ago

@Nescafemix when we are could see the 1.0.4 version of your awesome library?

Nescafemix commented 7 years ago

Sorry @JustTrust , my children took all my free time 😭 . I will try to do it today.

Nescafemix commented 7 years ago

@JustTrust published! @oneroman I'm going to begin the next version of the library fixing the problem with dialogs/dialogsfragments and the hint appearing under the dialog. it is gonna take me some time, cause i need to refactor some things.

oneroman commented 7 years ago

@JustTrust hey.Just want to clarify the following. using 1.0.4 version are you able to show hint above a Dialog? or that functionality still does not work for you?

Nescafemix commented 7 years ago

@oneroman No, the problem with the hint under the dialog is still on the last version. I need to refactor the place where I add the view of the hint. Currently, I'm using the Decorview of the activity to put the hintcaseView, but the Dialog is showed in an upper layer so, I need to add the hint in other place with the WindowManager. I know what i need to do (I checked some hardcoded code and I could show the hint above the dialog) but i need some time to do it in a clean way.

JustTrust commented 7 years ago

Hi @oneroman. How @Nescafemix said above problem with DialogFragment is still present. I need 1.0.4 for one reason, that my application doesn't crashed. But I am still waiting for fixed version.

VinitGrover commented 1 year ago

Yes, the same problem. If I use this library with DialogFragment I get the same cast error.

code.txt Hello there!

I wish to use the library inside the android.support.design.widget.BottomSheetDialogFragment. And I am passing the proper view into constructor for HintCase, but for Nexus 6 L OS I do have the following issue:

AndroidRuntime: FATAL EXCEPTION: main java.lang.ClassCastException: android.view.ContextThemeWrapper cannot be cast to android.app.Activity at com.joanfuentes.hintcase.utils.DimenUtils.getRealScreenSize(DimenUtils.java:81) at com.joanfuentes.hintcase.utils.DimenUtils.getNavigationBarSizeIfExistAtTheBottom(DimenUtils.java:42) at com.joanfuentes.hintcase.HintCaseView.init(HintCaseView.java:81) at com.joanfuentes.hintcase.HintCaseView.(HintCaseView.java:63) at com.joanfuentes.hintcase.HintCase.(HintCase.java:40)

Therefore, I am suggesting to modify the code slightly in the following manner. Please consider it because it shall work perfectly.

public static Point getRealScreenSize(Context context) { Point size = new Point(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); Display display = windowManager.getDefaultDisplay(); display.getRealSize(size); } else { View decorView = null; if(context instanceof Activity) { decorView = ((Activity) context).getWindow().getDecorView(); } else if(context instanceof ContextThemeWrapper) { Context baseContext = ((ContextThemeWrapper) context).getBaseContext(); if(baseContext instanceof Activity) { decorView = ((Activity) baseContext).getWindow().getDecorView(); } } if(decorView != null) { size.x = decorView.getWidth(); size.y = decorView.getHeight(); } } return size; }

You are great, Thanks in my case .. if(context instanceOf Activity) worked