由于RecyclerView,Android应用程序崩溃

时间:2018-06-26 20:59:46

标签: java android android-recyclerview recycler-adapter

我试图在主屏幕活动中添加一个简单的RecyclerView,每个视图都包含水果名称。在添加RecyclerView代码之前,我的应用程序运行良好。但是添加RecyclerView代码后,它开始崩溃。经过一些调试后,我知道将RecyclerView适配器设置为我的recyclerView对象时,应用程序崩溃了。在此语句中,准确地说:list_container.setAdapter(list_container_adapter);

请帮助我可能会出错的地方。 以下是提供的完整代码。

Mainscreen.java:

public class MainScreen extends AppCompatActivity {


private RecyclerView list_container;
private RecyclerView.Adapter list_container_adapter;
private RecyclerView.LayoutManager list_container_layout_manager;
private String[] fruits = {"Apple","Mango","Banana","Orange","Grapes","Cherry","Grapes","Pineapple","Stawberry","Litchi",
"Custard Apple","Plum","Peaches","Dragonfruit","Chickoo","Sweet Lime"};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_screen);
    Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
    toolbar.setTitle("");
    setSupportActionBar(toolbar);
    myView = findViewById(R.id.my_view);
    list_container = (RecyclerView)findViewById(R.id.list_container);
    list_container.setHasFixedSize(true);
    list_container_layout_manager = new LinearLayoutManager(this);
    list_container.setLayoutManager(list_container_layout_manager);
    list_container_adapter = new ListContainerAdapter(MainScreen.this,fruits);
    list_container.setAdapter(list_container_adapter);
}
}

ListContainerAdapter.java:

public class ListContainerAdapter extends RecyclerView.Adapter<ListContainerAdapter.MyViewHolder> {
private String[] mDataset;
private Context context;
public static class MyViewHolder extends RecyclerView.ViewHolder {
    public TextView mTextView;
    public MyViewHolder(TextView v) {
        super(v);
        mTextView = (TextView) v.findViewById(R.id.texttemp);
    }
}

public ListContainerAdapter(Context context, String[] myDataset) {
    this.context = context;
    mDataset = myDataset;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent,
                                               int viewType) {

    TextView v = (TextView) LayoutInflater.from(parent.getContext())
            .inflate(R.layout.list_container_element, parent, true);

    MyViewHolder vh = new MyViewHolder(v);
    return vh;
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    holder.mTextView.setText(mDataset[position]);

}

@Override
public int getItemCount() {
    return mDataset.length;
}
}

build.gradle:

    apply plugin: 'com.android.application'

android {
    compileSdkVersion 27
    defaultConfig {
        applicationId "com.bismuth.deadline"
        minSdkVersion 21
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    implementation 'com.android.support:support-v4:27.1.1'
    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'
    implementation 'com.android.support:recyclerview-v7:27.1.1'
    implementation 'com.android.support:design:27.1.1'
}

Stacktrace

06-27 02:34:07.906 2711-2711/com.bismuth.deadline E/AndroidRuntime: 
FATAL EXCEPTION: main Process: com.bismuth.deadline, PID: 2711 
java.lang.ClassCastException: android.support.v7.widget.RecyclerView 
cannot be cast to android.widget.TextView at com.bismuth.deadline.ListContainerAdapter.onCreateViewHolder(ListContainerAdapter.java:30) at com.bismuth.deadline.ListContainerAdapter.onCreateViewHolder(ListContainerAdapter.java:9)

list_container_element.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:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:paddingTop="20dp"
        android:paddingBottom="20dp"
        android:paddingStart="10dp"
        android:paddingEnd="10dp"
        android:textSize="25dp"
        android:id="@+id/texttemp"
        android:gravity="center" />
</LinearLayout>

2 个答案:

答案 0 :(得分:2)

实际上应该是:

public MyViewHolder onCreateViewHolder(ViewGroup parent,
                                               int viewType) {

    View v = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.list_container_element, parent, true);

    return new MyViewHolder(v);
}

在您的代码中,您将 View转换为TextView

public MyViewHolder onCreateViewHolder(ViewGroup parent,
                                               int viewType) {

    TextView v = (TextView) LayoutInflater.from(parent.getContext())
            .inflate(R.layout.list_container_element, parent, true);

    MyViewHolder vh = new MyViewHolder(v);
    return vh;
}

您可能还需要查看它是否需要通过false方法将attachToRoot作为inflate()的参数来传递。

答案 1 :(得分:1)

您将布局错误地投射到TextView中,您的onCreateViewHolder应该如下所示:

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

  View v = LayoutInflater.from(parent.getContext())
        .inflate(R.layout.list_container_element, parent, true);

  MyViewHolder vh = new MyViewHolder(v);
  return vh;
}

然后MyViewHolder构造函数应该接受一个View而不是TextView:

public MyViewHolder(View v) {
    super(v);
    mTextView = (TextView) v.findViewById(R.id.texttemp);
}