简体   繁体   中英

android.widget.LinearLayout cannot be cast to android.widget.TextView in RecyclerView?

I'm trying to list the data by using the new Recycler View. I've followed this tutorial & now facing issue at at one point.

Here's my

MainActivity.java

package com.example.i.newworkspace;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


public class MainActivity extends ActionBarActivity {

TextView tv;
RecyclerView recyclerView;
RecyclerView.Adapter mAdapter;
String[] myDataset = {"One","Two","Three","Four","Five"};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    recyclerView.setHasFixedSize(true);
    mAdapter = new MyAdapter(myDataset);
    recyclerView.setAdapter(mAdapter);

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

}
 class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder>

{
    String[] mDataset;

    public static class ViewHolder extends RecyclerView.ViewHolder {

        public TextView mTextView;
        public ViewHolder(TextView v) {
            super(v);
            mTextView = v;
        }
    }

    @Override
    public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.my_text_view, null, false);
        ViewHolder vh = new ViewHolder((TextView) v);
        return vh;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {

        holder.mTextView.setText(mDataset[position]);

    }

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

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

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <android.support.v7.widget.RecyclerView

        android:id="@+id/recyclerview"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </android.support.v7.widget.RecyclerView>

</RelativeLayout>

my_text_view.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:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

Logcat:

 java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.TextView
                at com.example.i.newworkspace.MyAdapter.onCreateViewHolder(MainActivity.java:78)
                at com.example.i.nuewworkspace.MyAdapter.onCreateViewHolder(MainActivity.java:59)

Line 78 :

        ViewHolder vh = new ViewHolder((TextView) v);

You should notice that your my_text_view.xml is a LinearLayout so:

public static class ViewHolder extends RecyclerView.ViewHolder {

    public TextView mTextView;
    public ViewHolder(View v) {
        super(v);
        // Find the TextView in the LinearLayout
        mTextView = v.findViewById(R.id.textView));
    }
}

@Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View v = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.my_text_view, parent, false);
    // Give the view as it is
    ViewHolder vh = new ViewHolder(v);
    return vh;
}

The inflated view contains the root element too ,so u need to create your textview by findviewbyid function and then pass it to ViewHolder constructor

@Override
    public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.my_text_view, null, false);
        TextView tv=(TextView)v.findViewById(R.id.textView);
        ViewHolder vh = new ViewHolder(tv);
        return vh;
    }

Good Luck

You are trying to cast a LinearLayout to TextView.

Change this line:

ViewHolder vh = new ViewHolder((TextView) v);

to:

ViewHolder vh = new ViewHolder((TextView) v.findViewById(R.id.textView));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM