Caused by: java.lang.IllegalStateException: RecyclerView has no LayoutManager androidx.recyclerview.widget.RecyclerView
............
at com.cfsuman.me.batterychargemefast.AdapterRecyclerSystemApps.onCreateViewHolder(AdapterRecyclerSystemApps.java:52)
at com.cfsuman.me.batterychargemefast.AdapterRecyclerSystemApps.onCreateViewHolder(AdapterRecyclerSystemApps.java:22)
遇到一个这样子的错误,网络上没有查出来原因。于是我分析代码。
public class FragmentSystemApps extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_system_apps, container, false);
mRecyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
mLayoutManager = new GridLayoutManager(mActivity,1);
mRecyclerView.setLayoutManager(mLayoutManager);
mAdapter = new AdapterRecyclerSystemApps(
mContext,
mSystemPackagesList
);
mRecyclerView.setAdapter(mAdapter);
return view;
}
}
fragment_system_apps.xml这么写的
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/coordinator_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
>
</androidx.recyclerview.widget.RecyclerView>
<TextView
android:id="@+id/tv_info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:elevation="3dp"
android:background="#7dffffff"
android:padding="8dp"
android:layout_gravity="top|end"
/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
上面代码没问题,继续看报错的地方
public class AdapterRecyclerSystemApps extends RecyclerView.Adapter<AdapterRecyclerSystemApps.ViewHolder>{
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
View v = LayoutInflater.from(mContext).inflate(R.layout.custom_view_system_apps,parent,false);
ViewHolder vh = new ViewHolder(v);
return vh;
}
报错就是这行代码
View v = LayoutInflater.from(mContext).inflate(R.layout.custom_view_system_apps,parent,false);
再看custom_view_system_apps.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.recyclerview.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardCornerRadius="@dimen/cardCornerRadius"
card_view:cardElevation="@dimen/cardElevation"
card_view:cardMaxElevation="@dimen/cardMaxElevation"
card_view:contentPadding="@dimen/cardContentPadding"
android:layout_margin="@dimen/cardLayoutMargin">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<ImageView
android:id="@+id/app_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/AppIconStyle"
/>
<TextView
android:id="@+id/app_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/app_icon"
android:layout_toEndOf="@id/app_icon"
style="@style/AppLabelStyle"
/>
<TextView
android:id="@+id/app_package"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/app_label"
android:layout_toRightOf="@id/app_icon"
android:layout_toEndOf="@id/app_icon"
style="@style/AppPackageStyle"
/>
<TextView
android:id="@+id/app_size"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/app_package"
android:layout_toRightOf="@id/app_icon"
android:layout_toEndOf="@id/app_icon"
style="@style/AppSizeStyle"
/>
</RelativeLayout>
</androidx.recyclerview.widget.RecyclerView>
后来发现是这custom_view_system_apps.xml这里搞错了,就是说本来要写CardView,但是我搞错了。于是就报上面的错误了。正确代码如下
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardCornerRadius="@dimen/cardCornerRadius"
card_view:cardElevation="@dimen/cardElevation"
card_view:cardMaxElevation="@dimen/cardMaxElevation"
card_view:contentPadding="@dimen/cardContentPadding"
android:layout_margin="@dimen/cardLayoutMargin">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<ImageView
android:id="@+id/app_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/AppIconStyle"
/>
<TextView
android:id="@+id/app_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/app_icon"
android:layout_toEndOf="@id/app_icon"
style="@style/AppLabelStyle"
/>
<TextView
android:id="@+id/app_package"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/app_label"
android:layout_toRightOf="@id/app_icon"
android:layout_toEndOf="@id/app_icon"
style="@style/AppPackageStyle"
/>
<TextView
android:id="@+id/app_size"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/app_package"
android:layout_toRightOf="@id/app_icon"
android:layout_toEndOf="@id/app_icon"
style="@style/AppSizeStyle"
/>
</RelativeLayout>
</androidx.cardview.widget.CardView>