How to fix "java.lang.ClassCastException: com.android.layoutlib.bridge.android.BridgeContext could not be added to android.app.Activity exception"

When I create a custom View , where in some cases I need to pass the Context class passed through the constructor for the Activity class to perform some tasks, such as inflating the View directly inside my custom View , I get the following error:

 java.lang.ClassCastException: com.android.layoutlib.bridge.android.BridgeContext cannot be cast to android.app.Activity 

This is the line throwing this error:

 View headerView = ((Activity) context).getLayoutInflater().inflate(R.layout.fragment_history_list_header, null); 

It seems that this error only occurs when Eclipse tries to inflate the view that will be displayed in the XML editor (does not occur at run time).

Does anyone know how to fix it?

Thanks in advance.

+6
source share
1 answer

Change the call as follows. The reason you get a class exception is because BridgeContext is not of type Activity.

 View headerView = LayoutInflater.from(context).inflate(R.layout.fragment_history_list_header, null); 
+7
source

Source: https://habr.com/ru/post/954907/


All Articles