Android ContextThemeWrapper cannot be cast to android.app.Activity

weixin_34050427發表於2018-01-02

java.lang.ClassCastException: android.view.ContextThemeWrapper cannot be cast to android.app.Activity Dialog中的context,在強制裝換Activity出現。 將(Activity) mContext,變成:scanForActivity(mContext)

private static Activity scanForActivity(Context cont) {  
    if (cont == null)  
        return null;  
    else if (cont instanceof Activity)  
        return (Activity)cont;  
    else if (cont instanceof ContextWrapper)  
        return scanForActivity(((ContextWrapper)cont).getBaseContext());  
  
    return null;  
}

相關文章