2016-10-23 6 views
-1

Я хочу использовать recyclerview в фрагменте, но я получил эту ошибку: java.lang.RuntimeException: не удалось запустить Activity ComponentInfo: android.view.InflateException: двоичная строка XML-файла # 7: ошибка раздувания класс android.support.v7.widget.RecyclerView в android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2195)Ошибка раздувания RecyclerView в фрагменте

Основная деятельность:

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(); 

    } 

Фрагмент:

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> 

Фрагмент макета 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 элемент 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> 
+0

Пожалуйста, добавьте здесь код. Неясно, в какой проблеме вы столкнулись здесь. –

+0

Вы добавили поддержку просмотра ресайклера для градации? – Opiatefuchs

+0

Я не использую град! Но я добавил связанные библиотеки для зависимостей проекта. – Irmaan

ответ

0

Я понял это. Я должен добавить всю библиотеку RecyclerView (целую папку из Android SDK) к моим модулям как модуль библиотеки, а затем добавить ее как зависимость от моего основного проекта и добавить ее библиотеку jar.

Смежные вопросы