Recycler View崩溃应用程序

时间:2017-03-01 16:10:04

标签: android android-recyclerview

我创建了一个崩溃整个应用程序的回收站视图。我在主要活动

以外的新包中创建了这个回收站视图

这是我的MainActivity.java文件

a[href*="//"]{
  display:none;
}

这是我的Data.java文件,它为回收站视图提供数据

package bk.acs;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import bk.acs.AddingToDatabase.MyDialog;
import bk.acs.RecyclerView.Data;
import bk.acs.RecyclerView.MyAdapter;
import bk.acs.databases.Main;

public class MainActivity extends AppCompatActivity {
    FloatingActionButton fab;
    RecyclerView recview;
    MyAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        fab=(FloatingActionButton)findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MyDialog dialog=new MyDialog();
                dialog.show(getFragmentManager(),"myDialog");
            }
        });
        recview=(RecyclerView)findViewById(R.id.recview);
        adapter=new MyAdapter(Data.getList(),this);
        recview.setLayoutManager(new LinearLayoutManager(this));
        recview.setAdapter(adapter);
    }
}

这是MyAdapter.java文件

package bk.acs.RecyclerView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by koteswarao on 01-03-2017.
 */

public class Data {
    public static String sample[]={"First subject", "second subject","third subject","fourth subject","fifth subject","subject"};
    public static List<ListItem> list=new ArrayList<>();
    public static List getList()
    {
        for(int i=0;i<sample.length;i++)
        {
            list.add(new ListItem(sample[i],(i+1)));
        }
        return list;
    }
}

我已将recview的id给了recyclerview,所以我没有保留main_recyclerview.xml

这是我的subject_li.xml正在膨胀

package bk.acs.RecyclerView;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

import bk.acs.R;

/**
 * Created by koteswarao on 01-03-2017.
 */

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.Holder>{
    List<ListItem> listdata;
    LayoutInflater inflater;
    public MyAdapter(List<ListItem> list, Context c)
    {
        this.inflater=LayoutInflater.from(c);
        listdata=list;
    }

    @Override
    public Holder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view=inflater.inflate(R.layout.subjects_li,parent,false);
        return new Holder(view);
    }

    @Override
    public void onBindViewHolder(Holder holder, int position) {
        ListItem item=listdata.get(position);
        holder.subjectName.setText(item.subName);
        holder.serialNo.setText(item.sno);
    }

    @Override
    public int getItemCount() {
        return listdata.size();
    }

    class Holder extends RecyclerView.ViewHolder
    {
        TextView subjectName;
        TextView serialNo;
        View container;
        public Holder(View itemView) {
            super(itemView);
            subjectName=(TextView)itemView.findViewById(R.id.subjectTitle);
            serialNo=(TextView)itemView.findViewById(R.id.sno);
            container=itemView.findViewById(R.id.sub_li);
        }
    }

}

这是我的activity_main.xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:id="@+id/cont_root"
    android:layout_height="wrap_content"
    android:layout_marginBottom="10dp"
    >



    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/white"
        android:id="@+id/sub_li"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="S.NO"
            android:textColor="@android:color/white"
            android:gravity="center"
            android:id="@+id/sno"
            android:padding="15dp"
            android:background="@android:color/holo_blue_dark"
            />
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            >
            <TextView
                android:layout_width="match_parent"
                android:padding="30dp"
                android:gravity="center"
                android:textColor="@android:color/black"
                android:id="@+id/subjectTitle"
                android:layout_height="match_parent"
                android:text="SUBJECT NAME"
                />
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

ListItem.java只是一个类,它有两个数据类型字符串和int,带有一个构造函数来初始化它们。

请帮帮我。提前谢谢......

这是我的logcat详细信息

<?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"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:background="#d3d3d3"
    >
    <android.support.design.widget.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:id="@+id/fab"
        android:src="@drawable/add"
        android:layout_marginRight="20dp"
        android:layout_marginBottom="20dp"
        />
    <include layout="@layout/main_recyclerview"/>
</android.support.design.widget.CoordinatorLayout>

3 个答案:

答案 0 :(得分:2)

我在任何xml文件中找不到回收站视图声明。无论如何,您需要为回收者视图分配视图 -

recview = (RecyclerView) findViewById(R.id.<your_view_id>);

答案 1 :(得分:0)

在MyAdapter serial中更改此

holder.serialNo.setText("" + item.sno);

答案 2 :(得分:0)

RecyclerView持有者只能承受字符串,因为我给了int值我的应用程序崩溃..