出于某种原因,在旧设备(api 23,api 19)上调试我的应用程序时,当我尝试为RecycleView
的孩子们充气ViewHolder时出现错误。
我尝试过以下方法:
我无法找到问题的根源:
错误日志:
android.view.InflateException: Binary XML file line #0: Error inflating class android.support.v7.widget.GridLayout
依赖关系:
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:multidex:1.0.3'
implementation 'com.android.support:appcompat-v7:27.1.0'
implementation 'com.android.support:gridlayout-v7:27.1.0'
implementation 'com.android.support:design:27.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
implementation 'com.github.bumptech.glide:glide:3.7.0'
implementation 'com.android.volley:volley:1.0.0'
implementation 'com.airbnb.android:lottie:2.5.0-rc1'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
// ViewModel and LiveData
implementation 'android.arch.lifecycle:extensions:1.1.0'
implementation 'android.arch.lifecycle:common-java8:1.1.0'
// Room (use 1.1.0-alpha3 for latest alpha)
implementation 'android.arch.persistence.room:runtime:1.0.0'
annotationProcessor "android.arch.persistence.room:compiler:1.0.0"
// Paging
implementation 'android.arch.paging:runtime:1.0.0-alpha6'
// Test helpers for LiveData
testImplementation 'android.arch.core:core-testing:1.1.0'
// Test helpers for Room
testImplementation 'android.arch.persistence.room:testing:1.0.0'
}
ViewHolder:
public class ViewHolder extends RecyclerView.ViewHolder{
private TextView rowTitle = null;
private TextView rowContent = null;
private ImageView rowImage = null;
private TextView deleteText = null;
private TextView completeText = null;
private DataCommunication mData = null;
private GlobalLists associatedItem = null;
private GridLayout background = null;
private GridLayout foreground = null;
public ViewHolder(View itemView) {
super(itemView);
this.mData = (DataCommunication)itemView.getContext();
this.background = itemView.findViewById(R.id.backRow);
this.foreground = itemView.findViewById(R.id.foreRow);
this.rowTitle = itemView.findViewById(R.id.rowTitle);
this.rowContent = itemView.findViewById(R.id.rowContent);
this.deleteText = itemView.findViewById(R.id.deleteText);
this.completeText = itemView.findViewById(R.id.completeText);
this.rowImage = itemView.findViewById(R.id.rowImage);
this.foreground.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("ViewHolder", "Clicked!");
if (associatedItem != null)
{
mData.setItemTitle(associatedItem.getTitle());
if(rowContent.getText().toString().length() > 0) {
mData.setFullContents(associatedItem.getContents());
}
}
}
});
}
public void setRowTitle(String title) {
if (title != null) {
this.rowTitle.setText(title);
}
}
public void setRowContent(String content) {
if (content != null && content.length() > 0) {
this.rowContent.setText(content);
} else {
this.rowContent.setText("");
}
}
public void setAssociatedItem(GlobalLists item){
this.associatedItem = item;
}
public TextView getDeleteText() {
return this.deleteText;
}
public TextView getCompleteText() {
return this.completeText;
}
public GridLayout getForeground() {
return this.foreground;
}
public GridLayout getBackground() {
return this.background;
}
}
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.GridLayout;
//other imports
public class ViewHolder extends RecyclerView.ViewHolder{
private TextView rowTitle = null;
private TextView rowContent = null;
private ImageView rowImage = null;
private TextView deleteText = null;
private TextView completeText = null;
private DataCommunication mData = null;
private GlobalLists associatedItem = null;
private GridLayout background = null;
private GridLayout foreground = null;
public ViewHolder(View itemView) {
super(itemView);
this.mData = (DataCommunication)itemView.getContext();
this.background = itemView.findViewById(R.id.backRow);
this.foreground = itemView.findViewById(R.id.foreRow);
this.rowTitle = itemView.findViewById(R.id.rowTitle);
this.rowContent = itemView.findViewById(R.id.rowContent);
this.deleteText = itemView.findViewById(R.id.deleteText);
this.completeText = itemView.findViewById(R.id.completeText);
this.rowImage = itemView.findViewById(R.id.rowImage);
this.foreground.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("ViewHolder", "Clicked!");
if (associatedItem != null)
{
mData.setItemTitle(associatedItem.getTitle());
if(rowContent.getText().toString().length() > 0) {
mData.setFullContents(associatedItem.getContents());
}
}
}
});
}
public void setRowTitle(String title) {
if (title != null) {
this.rowTitle.setText(title);
}
}
public void setRowContent(String content) {
if (content != null && content.length() > 0) {
this.rowContent.setText(content);
} else {
this.rowContent.setText("");
}
}
public void setAssociatedItem(GlobalLists item){
this.associatedItem = item;
}
public TextView getDeleteText() {
return this.deleteText;
}
public TextView getCompleteText() {
return this.completeText;
}
public GridLayout getForeground() {
return this.foreground;
}
public GridLayout getBackground() {
return this.background;
}
}
CustomRow(ViewHolder布局文件)XML:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_height="110dp"
android:layout_width="match_parent"
android:layout_marginBottom="20dp"
android:clickable="true"
android:focusable="true">
<android.support.v7.widget.GridLayout
android:layout_width="380dp"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
app:alignmentMode="alignMargins"
app:useDefaultMargins="true"
app:columnOrderPreserved="false"
app:rowCount="1"
app:columnCount="3"
android:id="@+id/backRow"
android:background="@drawable/ic_rounded_rectangle_accent"
app:orientation="horizontal">
<TextView
android:id="@+id/deleteText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/deleteTask"
android:textColor="@android:color/white"
android:textStyle="bold"
android:textSize="26sp"
app:layout_column="2"
app:layout_gravity="end|center_vertical"/>
<TextView
android:id="@+id/completeText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/completeTask"
android:textStyle="bold"
android:textColor="@android:color/white"
android:textSize="26sp"
app:layout_column="0"
app:layout_gravity="start|center_vertical"/>
</android.support.v7.widget.GridLayout>
<android.support.v7.widget.GridLayout
android:id="@+id/foreRow"
android:layout_width="380dp"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
app:alignmentMode="alignMargins"
app:columnOrderPreserved="false"
app:rowCount="2"
app:columnCount="3"
app:orientation="horizontal"
android:background="@drawable/ic_rounded_rectangle"
>
<ImageView
android:id="@+id/rowImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_column="0"
app:layout_row="0"
android:layout_marginTop="6dp"
app:srcCompat="@drawable/ic_info_outline"
app:layout_gravity="start|center_vertical"
/>
<TextView
android:id="@+id/rowTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/white"
app:layout_row="0"
app:layout_column="1"
android:textSize="20sp"
app:layout_gravity="start"
/>
<TextView
android:id="@+id/rowContent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/white"
app:layout_row="1"
app:layout_column="1"
android:textSize="20sp"
app:layout_gravity="start"/>
</android.support.v7.widget.GridLayout>
</FrameLayout>
编辑这似乎主要发生在使用API 19等LG G4和仿真器等硬件设备上进行调试时,有时通过错误或我的viewpager发生网格布局会引发错误。?
我已经在下面包含了我的main_activity.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.lab1.ac01220.bloomv2.MainActivity">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/appbar_padding_top"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_weight="1"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:title="@string/app_name">
</android.support.v7.widget.Toolbar>
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.TabItem
android:id="@+id/tabItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/tab_text_1" />
<android.support.design.widget.TabItem
android:id="@+id/tabItem2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/tab_text_2" />
<android.support.design.widget.TabItem
android:id="@+id/tabItem3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/tab_text_3" />
</android.support.design.widget.TabLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:background="@drawable/ic_backing"/>
</android.support.design.widget.CoordinatorLayout>