Leanback 示例 Activity 崩溃

Leanback Sample Activity Crashes

我添加了来自 android 工作室的 Leanback 示例 activity。 activity 因以下堆栈跟踪而崩溃。我没有修改代码,我看到所有默认添加的布局都定义了 layout_width 和 layout_height 参数。

java.lang.RuntimeException: Binary XML file line #21: You must supply a layout_width attribute.
     at android.content.res.TypedArray.getLayoutDimension(TypedArray.java:608)
     at android.view.ViewGroup$LayoutParams.setBaseAttributes(ViewGroup.java:6485)
     at android.view.ViewGroup$MarginLayoutParams.<init>(ViewGroup.java:6654)
     at android.widget.FrameLayout$LayoutParams.<init>(FrameLayout.java:741)
     at android.support.v17.leanback.widget.BaseCardView$LayoutParams.<init>(BaseCardView.java:862)
     at android.support.v17.leanback.widget.BaseCardView.generateLayoutParams(BaseCardView.java:817)
     at android.support.v17.leanback.widget.BaseCardView.generateLayoutParams(BaseCardView.java:54)
     at android.view.LayoutInflater.rInflate(LayoutInflater.java:812)
     at android.view.LayoutInflater.inflate(LayoutInflater.java:483)
     at android.view.LayoutInflater.inflate(LayoutInflater.java:415)
     at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
     at android.support.v17.leanback.widget.ImageCardView.buildImageCardView(ImageCardView.java:165)
     at android.support.v17.leanback.widget.ImageCardView.<init>(ImageCardView.java:156)
     at android.support.v17.leanback.widget.ImageCardView.<init>(ImageCardView.java:282)
     at android.support.v17.leanback.widget.ImageCardView.<init>(ImageCardView.java:275)
     at com.amazon.bueller.photos.activity.CardPresenter.<init>(CardPresenter.java:55)
     at com.amazon.bueller.photos.activity.CardPresenter.onCreateViewHolder(CardPresenter.java:55)
     at android.support.v17.leanback.widget.ItemBridgeAdapter.onCreateViewHolder(ItemBridgeAdapter.java:312)
     at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:6290)
     at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5478)
     at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5363)
     at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5359)
     at android.support.v17.leanback.widget.GridLayoutManager.measureScrapChild(GridLayoutManager.java:1211)
     at android.support.v17.leanback.widget.GridLayoutManager.processRowSizeSecondary(GridLayoutManager.java:1277)
     at android.support.v17.leanback.widget.GridLayoutManager.onMeasure(GridLayoutManager.java:1374)
     at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:3012)
     at android.view.View.measure(View.java:17935)
     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5548)
     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
     at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
     at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
     at android.view.View.measure(View.java:17935)
     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5548)
     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
     at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
     at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
     at android.view.View.measure(View.java:17935)
     at android.support.v17.leanback.widget.GridLayoutManager.measureChild(GridLayoutManager.java:1466)
     at android.support.v17.leanback.widget.GridLayoutManager.createItem(GridLayoutManager.java:1556)
     at android.support.v17.leanback.widget.SingleRow.appendVisibleItems(SingleRow.java:111)
     at android.support.v17.leanback.widget.Grid.appendVisibleItems(Grid.java:371)
     at android.support.v17.leanback.widget.GridLayoutManager.appendVisibleItems(GridLayoutManager.java:1749)
     at android.support.v17.leanback.widget.GridLayoutManager.onLayoutChildren(GridLayoutManager.java:1973)
     at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3506)
     at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3254)
     at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3767)
     at android.view.View.layout(View.java:16046)
                                                                        at android.view.ViewGroup.layout

将 'Theme.Leanback' 作为样式添加到 activity,错误就会消失。

示例:

<activity android:name="com.sample.activity.DetailsActivity"
              android:theme="@style/Theme.Leanback"/>