遇到一个坑货问题:
使用 leanback V17 的包,添加控件到页面上,结果出现了crash, crash信息如下:
android.view.InflateException: Binary XML file line #21 in org.droidtv.musicplayer:layout/lb_playback_controls_row: Binary XML file line #21 in org.droidtv.musicplayer:layout/lb_playback_controls_row: Error inflating class android.support.v17.leanback.widget.PlaybackControlsRowView
Caused by: android.view.InflateException: Binary XML file line #21 in org.droidtv.musicplayer:layout/lb_playback_controls_row: Error inflating class android.support.v17.leanback.widget.PlaybackControlsRowView
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
at android.view.LayoutInflater.createView(LayoutInflater.java:854)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:1006)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:961)
at android.view.LayoutInflater.inflate(LayoutInflater.java:659)
at android.view.LayoutInflater.inflate(LayoutInflater.java:534)
at android.support.v17.leanback.widget.PlaybackControlsRowPresenter.createRowViewHolder(PlaybackControlsRowPresenter.java:333)
at android.support.v17.leanback.widget.RowPresenter.onCreateViewHolder(RowPresenter.java:325)
at android.support.v17.leanback.widget.ItemBridgeAdapter.onCreateViewHolder(ItemBridgeAdapter.java:324)
at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:6493)
at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5680)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5563)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5559)
at android.support.v17.leanback.widget.GridLayoutManager.getViewForPosition(GridLayoutManager.java:1044)
at android.support.v17.leanback.widget.GridLayoutManager$2.createItem(GridLayoutManager.java:1571)
at android.support.v17.leanback.widget.SingleRow.appendVisibleItems(SingleRow.java:113)
at android.support.v17.leanback.widget.Grid.appendOneColumnVisibleItems(Grid.java:388)
at android.support.v17.leanback.widget.GridLayoutManager.appendOneColumnVisibleItems(GridLayoutManager.java:1797)
at android.support.v17.leanback.widget.GridLayoutManager.onLayoutChildren(GridLayoutManager.java:2225)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3693)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3410)
at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3962)
at android.view.View.layout(View.java:21934)
at android.view.ViewGroup.layout(ViewGroup.java:6260)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
at android.view.View.layout(View.java:21934)
at android.view.ViewGroup.layout(ViewGroup.java:6260)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
at android.view.View.layout(View.java:21934)
at android.view.ViewGroup.layout(ViewGroup.java:6260)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
at android.view.View.layout(View.java:21934)
at android.view.ViewGroup.layout(ViewGroup.java:6260)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1829)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1673)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1582)
at android.view.View.layout(View.java:21934)
at android.view.ViewGroup.layout(ViewGroup.java:6260)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
at android.view.View.layout(View.java:21934)
at android.view.ViewGroup.layout(ViewGroup.java:6260)
2021-07-19 15:50:02.669 8832-8832/org.droidtv.musicplayer E/AndroidRuntime: at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1829)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1673)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1582)
at android.view.View.layout(View.java:21934)
at android.view.ViewGroup.layout(ViewGroup.java:6260)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
at com.android.internal.policy.DecorView.onLayout(DecorView.java:779)
at android.view.View.layout(View.java:21934)
at android.view.ViewGroup.layout(ViewGroup.java:6260)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:3077)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2587)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1718)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7595)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:966)
at android.view.Choreographer.doCallbacks(Choreographer.java:790)
at android.view.Choreographer.doFrame(Choreographer.java:725)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:951)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
Caused by: java.lang.UnsupportedOperationException: Failed to resolve attribute at index 68: TypedValue{t=0x2/d=0x7f0301f0 a=-1}
at android.content.res.TypedArray.getDimensionPixelSize(TypedArray.java:782)
at android.view.View.<init>(View.java:5305)
at android.view.ViewGroup.<init>(ViewGroup.java:675)
at android.widget.LinearLayout.<init>(LinearLayout.java:254)
at android.widget.LinearLayout.<init>(LinearLayout.java:250)
at android.widget.LinearLayout.<init>(LinearLayout.java:246)
at android.support.v17.leanback.widget.PlaybackControlsRowView.<init>(PlaybackControlsRowView.java:40)
... 71 more
反正就这一长串异常信息,大概意思就是反射异常了,可是控件用的是V17包底下的,咋就出异常了。懵圈。。
找了一堆资料后,终于发现了解决方法:
配置文件上,在使用V17 控件的 Activity 上添加这一行:
android:theme="@style/Theme.Leanback"