Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

When I create a custom View, where in some cases I need to cast the Context class passed via the constructor for the Activity class, for making some tasks like inflate a View directly inside my custom View class, I'm getting 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 like this error only occurs when Eclipse tries to inflate the view to be showed in the XML editor (not occurs in runtime).

Does someone know how to fix it?

Thanks in advance.

like image 694
Jorge Gil Avatar asked Sep 30 '13 01:09

Jorge Gil


1 Answers

Change the call to the following. The reason you are getting a class cast exception is because BridgeContext is not of type Activity.

View headerView = LayoutInflater.from(context).inflate(R.layout.fragment_history_list_header, null);
like image 135
Bobbake4 Avatar answered Oct 08 '22 15:10

Bobbake4