简体   繁体   English

java.lang.ClassCastException:android.widget.LinearLayout 无法转换为 android.widget

[英]java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget

import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.view.View;
import android.widget.TextView;
import androidx.recyclerview.widget.RecyclerView;

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> { // <-- line 9
private String[] mDataset;


public static class MyViewHolder extends RecyclerView.ViewHolder {
    // each data item is just a string in this case
    public TextView textView;
    public MyViewHolder(View v) {
        super(v);
        textView = (TextView) v.findViewById(R.id.mTextView);
        this.textView = textView;
    }
}


public MyAdapter(String[] myDataset) {
    mDataset = myDataset;
}


@Override
public MyAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent,
                                                 int viewType) {
    // create a new view
    TextView v = (TextView) LayoutInflater.from(parent.getContext())
            .inflate(R.layout.my_text_view, parent, false); // <-- line 36

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


@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    // - get element from your dataset at this position
    // - replace the contents of the view with that element
    holder.textView.setText(mDataset[position]);

}


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

} }

this is the code of MyAdapter class and when i run it, The app keeps shut down so i have checked Logcat and it says这是 MyAdapter class 的代码,当我运行它时,应用程序一直关闭,所以我检查了 Logcat,它说

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.t5online.chat.hanbattalk, PID: 593
    java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.TextView
        at com.t5online.chat.hanbattalk.MyAdapter.onCreateViewHolder(MyAdapter.java:36)
        at com.t5online.chat.hanbattalk.MyAdapter.onCreateViewHolder(MyAdapter.java:9)

However, I can't find what the problem is in line 36 and 9.但是,我在第 36 行和第 9 行找不到问题所在。

You are not inflating view properly in onCreateViewHolder .您没有在onCreateViewHolder中正确膨胀视图。 It should be something like this,应该是这样的

 View view;
 LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
 view = layoutInflater.inflate(R.layout.my_text_view,parent,false);
 return new MyAdapter.MyViewHolder(view);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.lang.ClassCastException:android.widget.LinearLayout无法转换为android.widget.Button - java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.Button java.lang.ClassCastException:android.widget.LinearLayout $ LayoutParams无法转换为android.widget.AbsListView $ LayoutParams - java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams java.lang.ClassCastException:android.widget.LinearLayout无法强制转换为android.support.v7.widget.Toolbar - java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.support.v7.widget.Toolbar java.lang.ClassCastException:android.widget.LinearLayout $ LayoutParams无法转换为android.widget.FrameLayout - java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.FrameLayout java.lang.ClassCastException:android.widget.RelativeLayout $ LayoutParams无法转换为android.widget.LinearLayout $ LayoutParams - java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.widget.LinearLayout$LayoutParams java.lang.ClassCastException:android.widget.LinearLayout无法强制转换为android.widget.ListView - java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.ListView java.lang.ClassCastException:androidx.constraintlayout.widget.ConstraintLayout 无法转换为 android.widget.LinearLayout - java.lang.ClassCastException: androidx.constraintlayout.widget.ConstraintLayout cannot be cast to android.widget.LinearLayout ClassCastException:java.lang.ClassCastException:android.widget.LinearLayout $ LayoutParams无法强制转换为android.support.v4.widget.DrawerLayout - ClassCastException : java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.support.v4.widget.DrawerLayout Android java.lang.ClassCastException:android.widget.LinearLayout $ LayoutParams无法强制转换为android.widget.AbsListView $ LayoutParams - Android java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams 不能转换为 android.support.v7.widget.RecyclerView$LayoutParams - java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.support.v7.widget.RecyclerView$LayoutParams
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM