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.ContextThemeWrapper cannot be cast to android.app.Activity #4

Open
lucas-ferreira-machado opened this issue Jul 17, 2019 · 2 comments

Comments

@lucas-ferreira-machado
Copy link

I received this error in my project:

2019-07-17 16:45:52.667 14358-14358/com.app E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.app, PID: 14358
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app/com.app.MainActivity}: android.view.InflateException: Binary XML file line #52: Binary XML file line #52: Error inflating class com.evolve.backdroplibrary.BackdropContainer

at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3121)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3260)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1976)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6912)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:860)
Caused by: android.view.InflateException: Binary XML file line #52: Binary XML file line #52: Error inflating class com.evolve.backdroplibrary.BackdropContainer
Caused by: android.view.InflateException: Binary XML file line #52: Error inflating class com.evolve.backdroplibrary.BackdropContainer
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
at android.view.LayoutInflater.createView(LayoutInflater.java:647)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:790)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:866)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at com.app.MainActivity.onCreate(MainActivity.kt:42)
at android.app.Activity.performCreate(Activity.java:7144)
at android.app.Activity.performCreate(Activity.java:7135)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1293)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3101)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3260)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1976)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6912)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:860)
Caused by: java.lang.ClassCastException: android.view.ContextThemeWrapper cannot be cast to android.app.Activity
at com.evolve.backdroplibrary.BackdropContainer.(BackdropContainer.java:45)
at java.lang.reflect.Constructor.newInstance0(Native Method) 
at java.lang.reflect.Constructor.newInstance(Constructor.java:343) 
at android.view.LayoutInflater.createView(LayoutInflater.java:647) 
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:790) 
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730) 
at android.view.LayoutInflater.rInflate(LayoutInflater.java:863) 
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824) 
at android.view.LayoutInflater.rInflate(LayoutInflater.java:866) 
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:515) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469) 
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) 
at com.app.MainActivity.onCreate(MainActivity.kt:42) 
at android.app.Activity.performCreate(Activity.java:7144) 
at android.app.Activity.performCreate(Activity.java:7135) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1293) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3101) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3260) 
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1976) 
at android.os.Handler.dispatchMessage(Handler.java:106) 
at android.os.Looper.loop(Looper.java:193) 
at android.app.ActivityThread.main(ActivityThread.java:6912) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:860) 

My configuration project:

compileSdkVersion 29
defaultConfig {
applicationId "com.app"
minSdkVersion 21
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

implementation 'com.github.kishan2612:Material-Backdrop-Android:v1.1.0'

@lucas-ferreira-machado
Copy link
Author

The CastException happens in the instruction ((Activity)context).getWindowManager().getDefaultDisplay().getMetrics(metrics);

public BackdropContainer(@nonnull Context context, @nullable AttributeSet attrs) {
super(context, attrs);

    this.context=context;

    TypedArray typedArray=context.obtainStyledAttributes(attrs,R.styleable.BackdropContainer,0,0);

    mMenuicon=typedArray.getDrawable(R.styleable.BackdropContainer_menuIcon);
    mCloseicon=typedArray.getDrawable(R.styleable.BackdropContainer_closeIcon);
    duration=typedArray.getInt(R.styleable.BackdropContainer_duration,1000);

    typedArray.recycle();

    DisplayMetrics metrics=new DisplayMetrics();
       ((Activity)context).getWindowManager().getDefaultDisplay().getMetrics(metrics); 
    height=metrics.heightPixels;


}

To fix the error, please change that instruction. Please use this line in constructor of BackdropContainer class:

 **getActivityFromContext(context).getWindowManager().getDefaultDisplay().getMetrics(metrics);**

Add the method below in BackdropContainer class:

 **private Activity getActivityFromContext(Context context) {
     if (context == null)
         return null;
     else if (context instanceof Activity)
         return (Activity)context;
     else if (context instanceof ContextWrapper)
         return getActivityFromContext(((ContextWrapper)context).getBaseContext());
     return null;
 }**

The constructor of the BackdropContainer class would look like this:

**public BackdropContainer(@nonnull Context context, @nullable AttributeSet attrs) {
super(context, attrs);

    this.context=context;

    TypedArray typedArray=context.obtainStyledAttributes(attrs,R.styleable.BackdropContainer,0,0);

    mMenuicon=typedArray.getDrawable(R.styleable.BackdropContainer_menuIcon);
    mCloseicon=typedArray.getDrawable(R.styleable.BackdropContainer_closeIcon);
    duration=typedArray.getInt(R.styleable.BackdropContainer_duration,1000);

    typedArray.recycle();

    DisplayMetrics metrics=new DisplayMetrics();

    getActivityFromContext(context).getWindowManager().getDefaultDisplay().getMetrics(metrics);
    height=metrics.heightPixels;

}**

Please feel free to contact me if you have any doubts.

@FANMixco
Copy link

FANMixco commented Aug 1, 2019

The CastException happens in the instruction ((Activity)context).getWindowManager().getDefaultDisplay().getMetrics(metrics);

public BackdropContainer(@nonnull Context context, @nullable AttributeSet attrs) {
super(context, attrs);

    this.context=context;

    TypedArray typedArray=context.obtainStyledAttributes(attrs,R.styleable.BackdropContainer,0,0);

    mMenuicon=typedArray.getDrawable(R.styleable.BackdropContainer_menuIcon);
    mCloseicon=typedArray.getDrawable(R.styleable.BackdropContainer_closeIcon);
    duration=typedArray.getInt(R.styleable.BackdropContainer_duration,1000);

    typedArray.recycle();

    DisplayMetrics metrics=new DisplayMetrics();
       ((Activity)context).getWindowManager().getDefaultDisplay().getMetrics(metrics); 
    height=metrics.heightPixels;


}

To fix the error, please change that instruction. Please use this line in constructor of BackdropContainer class:

 **getActivityFromContext(context).getWindowManager().getDefaultDisplay().getMetrics(metrics);**

Add the method below in BackdropContainer class:

 **private Activity getActivityFromContext(Context context) {
     if (context == null)
         return null;
     else if (context instanceof Activity)
         return (Activity)context;
     else if (context instanceof ContextWrapper)
         return getActivityFromContext(((ContextWrapper)context).getBaseContext());
     return null;
 }**

The constructor of the BackdropContainer class would look like this:

**public BackdropContainer(@nonnull Context context, @nullable AttributeSet attrs) {
super(context, attrs);

    this.context=context;

    TypedArray typedArray=context.obtainStyledAttributes(attrs,R.styleable.BackdropContainer,0,0);

    mMenuicon=typedArray.getDrawable(R.styleable.BackdropContainer_menuIcon);
    mCloseicon=typedArray.getDrawable(R.styleable.BackdropContainer_closeIcon);
    duration=typedArray.getInt(R.styleable.BackdropContainer_duration,1000);

    typedArray.recycle();

    DisplayMetrics metrics=new DisplayMetrics();

    getActivityFromContext(context).getWindowManager().getDefaultDisplay().getMetrics(metrics);
    height=metrics.heightPixels;

}**

Please feel free to contact me if you have any doubts.
backdroplibrary

What about if you do a fork and a pull request? It will be faster.

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