由于RecyclerView,Android应用程序崩溃?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (260)

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'
}

堆栈跟踪

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>
提问于
用户回答回答于

实际上应该是:

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作为参数attachToRootinflate()方法代替。

用户回答回答于

你错误地将一个Layout投射到一个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);
}

扫码关注云+社区

领取腾讯云代金券