v3 mini 控制器的向后兼容性:错误膨胀类片段

Posted

技术标签:

【中文标题】v3 mini 控制器的向后兼容性:错误膨胀类片段【英文标题】:Backwards compatibility of the v3 mini controller: Error inflating class fragment 【发布时间】:2016-08-07 11:55:51 【问题描述】:

在 Googles CastVideos 参考 App 中,minSdkVersion 为 10。尝试在 API 16 Emulator Instance (android 4.1 x86) 上运行 App 时,App 立即崩溃,我收到此错误:

java.lang.RuntimeException: Unable to start activity ComponentInfocom.google.sample.cast.refplayer/com.google.sample.cast.refplayer.VideoBrowserActivity: android.view.InflateException: Binary XML file line #41: Error inflating class fragment
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
                  at android.app.ActivityThread.access$600(ActivityThread.java:130)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
                  at android.os.Handler.dispatchMessage(Handler.java:99)
                  at android.os.Looper.loop(Looper.java:137)
                  at android.app.ActivityThread.main(ActivityThread.java:4745)
                  at java.lang.reflect.Method.invokeNative(Native Method)
                  at java.lang.reflect.Method.invoke(Method.java:511)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
                  at dalvik.system.NativeStart.main(Native Method)
               Caused by: android.view.InflateException: Binary XML file line #41: Error inflating class fragment
                  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
                  at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
                  at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:280)
                  at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
                  at com.google.sample.cast.refplayer.VideoBrowserActivity.onCreate(VideoBrowserActivity.java:111)
                  at android.app.Activity.performCreate(Activity.java:5008)
                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 
                  at android.app.ActivityThread.access$600(ActivityThread.java:130) 
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 
                  at android.os.Handler.dispatchMessage(Handler.java:99) 
                  at android.os.Looper.loop(Looper.java:137) 
                  at android.app.ActivityThread.main(ActivityThread.java:4745) 
                  at java.lang.reflect.Method.invokeNative(Native Method) 
                  at java.lang.reflect.Method.invoke(Method.java:511) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
                  at dalvik.system.NativeStart.main(Native Method) 
               Caused by: java.lang.RuntimeException: com.google.android.gms.internal.zzsb$zza: No acceptable module found. Local version is 0 and remote version is 0.
                  at com.google.android.gms.internal.zzni.zzbg(Unknown Source)
                  at com.google.android.gms.internal.zzni.zza(Unknown Source)
                  at com.google.android.gms.cast.framework.CastContext.<init>(Unknown Source)
                  at com.google.android.gms.cast.framework.CastContext.getSharedInstance(Unknown Source)
                  at com.google.android.gms.cast.framework.media.uicontroller.UIMediaController.<init>(Unknown Source)
                  at com.google.android.gms.cast.framework.media.widget.MiniControllerFragment.onCreateView(Unknown Source)
                  at android.support.v4.app.Fragment.performCreateView(Fragment.java:2074)
                  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1061)
                  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1264)
                  at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1366)
                  at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2409)
                  at android.support.v4.app.FragmentController.onCreateView(FragmentController.java:120)
                  at android.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:374)
                  at android.support.v4.app.BaseFragmentActivityHoneycomb.onCreateView(BaseFragmentActivityHoneycomb.java:33)
                  at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:75)
                  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
                  at android.view.LayoutInflater.rInflate(LayoutInflater.java:746) 
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:489) 
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:396) 
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:352) 
                  at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:280) 
                  at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) 
                  at com.google.sample.cast.refplayer.VideoBrowserActivity.onCreate(VideoBrowserActivity.java:111) 
                  at android.app.Activity.performCreate(Activity.java:5008) 
                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079) 
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023) 
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 
                  at android.app.ActivityThread.access$600(ActivityThread.java:130) 
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 
                  at android.os.Handler.dispatchMessage(Handler.java:99) 
                  at android.os.Looper.loop(Looper.java:137) 
                  at android.app.ActivityThread.main(ActivityThread.java:4745) 
                  at java.lang.reflect.Method.invokeNative(Native Method) 
                  at java.lang.reflect.Method.invoke(Method.java:511) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
                  at dalvik.system.NativeStart.main(Native Method) 
               Caused by: com.google.android.gms.internal.zzsb$zza: No acceptable module found. Local version is 0 and remote version is 0.
                  at com.google.android.gms.internal.zzsb.zza(Unknown Source)
                  at com.google.android.gms.internal.zzni.zzbg(Unknown Source) 
                  at com.google.android.gms.internal.zzni.zza(Unknown Source) 
                  at com.google.android.gms.cast.framework.CastContext.<init>(Unknown Source) 
                  at com.google.android.gms.cast.framework.CastContext.getSharedInstance(Unknown Source) 
                  at com.google.android.gms.cast.framework.media.uicontroller.UIMediaController.<init>(Unknown Source) 
                  at com.google.android.gms.cast.framework.media.widget.MiniControllerFragment.onCreateView(Unknown Source) 
                  at android.support.v4.app.Fragment.performCreateView(Fragment.java:2074) 
                  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1061) 
                  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1264) 
                  at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1366) 
                  at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2409) 
                  at android.support.v4.app.FragmentController.onCreateView(FragmentController.java:120) 
                  at android.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:374) 
                  at android.support.v4.app.BaseFragmentActivityHoneycomb.onCreateView(BaseFragmentActivityHoneycomb.java:33) 
                  at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:75) 
                  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680) 
                  at android.view.LayoutInflater.rInflate(LayoutInflater.java:746) 
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:489) 
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:396) 
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:352) 
                  at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:280) 
                  at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) 
                  at com.google.sample.cast.refplayer.VideoBrowserActivity.onCreate(VideoBrowserActivity.java:111) 
                  at android.app.Activity.performCreate(Activity.java:5008) 
                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079) 
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023) 
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 
                  at android.app.ActivityThread.access$600(ActivityThread.java:130) 
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 
                  at android.os.Handler.dispatchMessage(Handler.java:99) 
                  at android.os.Looper.loop(Looper.java:137) 
                  at android.app.ActivityThread.main(ActivityThread.java:4745) 
                  at java.lang.reflect.Method.invokeNative(Native Method) 
                  at java.lang.reflect.Method.invoke(Method.java:511) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
                  at dalvik.system.NativeStart.main(Native Method) 

它的作用就像 AppCompatActivity 不会膨胀 MiniController Fragment,它由 Cast v3 框架提供。

这是 Cast Framework 的问题还是某种错误配置的模拟器的问题?

同样的情况发生在我自己的应用程序中,我也将迷你控制器片段添加到 AppCompatActivity。 在 API 23 上,这两个应用程序都能完美运行。

谢谢

【问题讨论】:

【参考方案1】:

问题在于 Emulator 映像没有 Google API(没有实际的支持库和应用程序崩溃的播放服务)

【讨论】:

【参考方案2】:

您需要检查google-play-services版本,如果版本与您正在开发的应用程序不一样会崩溃。为避免这种情况,您可以使用以下代码 sn-p 检查设备上当前安装的版本并启动对话框以更新 google-play-services

/**
 * Check play services version and go for update if not updated
 * @return
 */
private boolean checkPlayServices() 
    GoogleApiAvailability apiAvailability = GoogleApiAvailability.getInstance();
    int resultCode = apiAvailability.isGooglePlayServicesAvailable(this);
    if (resultCode != ConnectionResult.SUCCESS) 
        if (apiAvailability.isUserResolvableError(resultCode)) 
            apiAvailability.getErrorDialog(this, resultCode, PLAY_SERVICES_RESOLUTION_REQUEST)
                    .show();
         else
            Toast.makeText(this, "This device is not supported", Toast.LENGTH_LONG).show();
            finish();
        
        return false;
    
    return true;

【讨论】:

以上是关于v3 mini 控制器的向后兼容性:错误膨胀类片段的主要内容,如果未能解决你的问题,请参考以下文章

使用 Core Data 时的向后兼容性

Android Studio 中的向后兼容性是啥?

升级 Newtonsoft.JSON 是不是存在任何已知的向后兼容性问题?

Android Studio 上的向后兼容性 (AppCompat)

Mac SDK:使用最新的SDK,但确保与早期部署目标的向后兼容性

xml 保持与XML的向后兼容性