1

These are the problem in details :

java.lang.ClassCastException: com.android.layoutlib.bridge.android.BridgeContext cannot be cast to android.app.Activity
at io.jchat.android.view.LoginView.onMeasure_Original(LoginView.java:123);
at io.jchat.android.view.LoginView.onMeasure(LoginView.java) ;

And my code is:

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

    int height = MeasureSpec.getSize(heightMeasureSpec);
    mContext = getContext();
    Activity activity = (Activity)mContext;
        activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);

    int statusBarHeight = rect.top;

    activity.getWindowManager().getDefaultDisplay().getMetrics(dm);

    int screenHeight = dm.heightPixels;
    int diff = (screenHeight - statusBarHeight) - height;

    if(mListener != null){
        mListener.onSoftKeyboardShown(diff);
    }
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}

As a freshman, I am eager for your help. Thank you!

meredrica
  • 2,563
  • 1
  • 21
  • 24
xuange
  • 57
  • 1
  • 10

2 Answers2

1
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

    int height = MeasureSpec.getSize(heightMeasureSpec);

    int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");

    int statusBarHeight = 0;
    if (resourceId > 0) {

         statusBarHeight = getResources().getDimensionPixelSize(resourceId);
    }

    dm = getResources().getDisplayMetrics();

    int screenHeight = dm.heightPixels;
    int diff = (screenHeight - statusBarHeight) - height;
    if(mListener != null){
        mListener.onSoftKeyboardShown(diff);
    }
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
xuange
  • 57
  • 1
  • 10
0

Class com.android.layoutlib.bridge.android.BridgeContext does not extend android.app.Activity, which is the required condition to perform casting. Please check the relationship between those classes. Activity activity = (Activity)mContext; won't work.

VijayD
  • 826
  • 1
  • 11
  • 33
  • Thank you. But I do need to getWindowVisibleDisplayFrame and getgetMetrics. So do you know how to make a substitue? – xuange Feb 21 '17 at 12:22
  • I'm not an android developer but will look for a way to achieve that! – VijayD Feb 21 '17 at 12:26
  • There is a constructor in android.view.Window where you can pass the context. so try `new Window(context).getDecorView().getWindowVisibleDisplayFrame(rect);` . Please try and let me know if it works. – VijayD Feb 21 '17 at 12:36
  • Hi,friend. I have solved the problem. Thanks for your suggestion. Window is an abstract class so it cannot be instantialed. My new code is below.. – xuange Feb 21 '17 at 15:16
  • @xuange, post your new code as the answer of this problem, do not mix up the code, as this will help others in future if they face similar problem – VijayD Feb 21 '17 at 15:21
  • Sure. It is below. – xuange Feb 21 '17 at 15:27