Irmaan Irmaan - 1 month ago 8
Android Question

Error inflating RecyclerView in fragment

I want to use a recyclerview in fragment but i got this error :
java.lang.RuntimeException: Unable to start activity ComponentInfo: android.view.InflateException: Binary XML file line #7: Error inflating class android.support.v7.widget.RecyclerView at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)

Main Activity :

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
init();
}

public void init() {
setContentView(R.layout.main);

Fragment fr;
fr = new AuctionsList();
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.fragment_place, fr);
fragmentTransaction.commit();

}


Fragment :

public class AuctionsList extends Fragment {
private List<Goods> goodsList = new ArrayList<>();
private RecyclerView recyclerView;
private GoodsAdapter mAdapter;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

//Inflate the layout for this fragment
final View rootView = inflater.inflate(
R.layout.frg_auction_list, container, false);

recyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_view);
mAdapter = new GoodsAdapter(goodsList);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(this.getActivity());
recyclerView.setLayoutManager(mLayoutManager);


recyclerView.setAdapter(mAdapter);
recyclerView.setItemAnimator(new DefaultItemAnimator());

recyclerView.addItemDecoration(new DividerItemDecoration(this.getActivity(), LinearLayoutManager.VERTICAL));
prepareGoodsData();
mAdapter.setOnItemClickListener(new GoodsAdapter.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
Goods good = goodsList.get(position);
Toast.makeText(getActivity(), good.getTitle() + " was clicked!", Toast.LENGTH_SHORT).show();
}
});



return rootView;
}


Main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"

android:gravity="center_horizontal">

<Button android:id="@+id/but_terms"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/main_but_terms"/>

<Button android:id="@+id/but_guide"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/main_but_guide"/>

<Button android:id="@+id/but_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="80dp"
android:text="@string/main_but_login"/>

</LinearLayout>

<fragment android:name="com.ods.activity.AuctionsList"
android:id="@+id/fragment_place"
android:layout_width="match_parent"
android:layout_height="fill_parent"/>
</LinearLayout>


Fragment layout xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical" />


</RelativeLayout>


RecyclerView item xml :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:clickable="true"
android:orientation="vertical">

<ImageView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"
android:layout_alignParentLeft="true"/>

<TextView
android:id="@+id/title"
android:text="عنوان"
android:textColor="@color/title"
android:textSize="16dp"
android:textStyle="bold"
android:layout_alignParentTop="true"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<TextView
android:id="@+id/lastBidPrice"
android:layout_below="@id/title"
android:gravity="right"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="356500"/>

<TextView
android:id="@+id/remainingTime"
android:layout_below="@id/lastBidPrice"
android:textColor="@color/year"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:layout_height="wrap_content"
android:text="24:15:12"/>

</RelativeLayout>

Answer

I figured it out. I should add whole RecyclerView library (Whole folder from Android SDK) to my modules as Library module and then add it as a dependency to my main project and adding it's jar library as well.

Comments