java.lang.ClassCastException:android.widget.RelativeLayout无法强制转换为android.widget.TextView

时间:2018-01-22 01:29:50

标签: java android xml android-layout

当我尝试从一个活动切换到另一个活动时,当应用程序崩溃时,我得到了该异常。引用此错误的所有其他帖子都指向id重复项,但我无法找到此单个实例。

这是我的主要XML activity_work_experience.xml

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/activity_work2"
    tools:context=".WorkExperience">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbars="vertical" />

</RelativeLayout>

这是我的第二个XML,activity_work2.xml

<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".WorkExperience">

    <android.support.design.widget.AppBarLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:theme="@style/Widget.AppCompat.ActionBar">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/Widget.AppCompat.PopupWindow" />

    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/activity_work_experience"/>

</android.support.design.widget.CoordinatorLayout>

这是我的第三个XML list_row.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?android:attr/selectableItemBackground"
    android:clickable="true"
    android:focusable="true"
    android:orientation="vertical"
    android:paddingBottom="@dimen/row_padding_vertical"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/row_padding_vertical">

    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:textColor="@color/title"
        android:textSize="16dp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/genre"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/title" />

    <TextView
        android:id="@+id/year"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:textColor="@color/subtitle" />

</RelativeLayout>

这是我的主要活动课程WorkExperience.java

    import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

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

public class WorkExperience extends Activity {
    private RecyclerView mRecyclerView;
    private RecyclerView.Adapter mAdapter;
    private RecyclerView.LayoutManager mLayoutManager;
    private List<Movie> mDataset = new ArrayList<>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_work_experience);
        mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);

        mRecyclerView.setHasFixedSize(true);

        mLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(mLayoutManager);

        mAdapter = new MyAdapter(mDataset);
        mRecyclerView.setAdapter(mAdapter);

        prepareMovieData();
    }

    private void prepareMovieData() {
        Movie movie = new Movie("Mad Max: Fury Road", "Action & Adventure", "2015");
        mDataset.add(movie);

        movie = new Movie("Inside Out", "Animation, Kids & Family", "2015");
        mDataset.add(movie);

        movie = new Movie("Star Wars: Episode VII - The Force Awakens", "Action", "2015");
        mDataset.add(movie);

        movie = new Movie("Shaun the Sheep", "Animation", "2015");
        mDataset.add(movie);

        movie = new Movie("The Martian", "Science Fiction & Fantasy", "2015");
        mDataset.add(movie);

        movie = new Movie("Mission: Impossible Rogue Nation", "Action", "2015");
        mDataset.add(movie);

        movie = new Movie("Up", "Animation", "2009");
        mDataset.add(movie);

        movie = new Movie("Star Trek", "Science Fiction", "2009");
        mDataset.add(movie);

        movie = new Movie("The LEGO Movie", "Animation", "2014");
        mDataset.add(movie);

        movie = new Movie("Iron Man", "Action & Adventure", "2008");
        mDataset.add(movie);

        movie = new Movie("Aliens", "Science Fiction", "1986");
        mDataset.add(movie);

        movie = new Movie("Chicken Run", "Animation", "2000");
        mDataset.add(movie);

        movie = new Movie("Back to the Future", "Science Fiction", "1985");
        mDataset.add(movie);

        movie = new Movie("Raiders of the Lost Ark", "Action & Adventure", "1981");
        mDataset.add(movie);

        movie = new Movie("Goldfinger", "Action & Adventure", "1965");
        mDataset.add(movie);

        movie = new Movie("Guardians of the Galaxy", "Science Fiction & Fantasy", "2014");
        mDataset.add(movie);

        mAdapter.notifyDataSetChanged();
    }

最后,我的适配器类,创造性地命名为MyAdapter.java

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

import java.util.List;

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
    private List<Movie> mDataset;



    public static class ViewHolder extends RecyclerView.ViewHolder {
        public TextView title, year, genre;
        public ViewHolder(TextView view) {
            super(view);
            title = (TextView) view.findViewById(R.id.title);
            genre = (TextView) view.findViewById(R.id.genre);
            year = (TextView) view.findViewById(R.id.year);
        }
    }

    public MyAdapter(List<Movie> mDataset) {
        this.mDataset = mDataset;
    }

    @Override
    public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                   int viewType) {
        TextView v = (TextView) LayoutInflater.from(parent.getContext())
                .inflate(R.layout.list_row, parent, false);

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

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Movie movie = mDataset.get(position);
        holder.title.setText(movie.getTitle());
        holder.genre.setText(movie.getGenre());
        holder.year.setText(movie.getYear());

    }

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

对于这篇荒谬的长篇帖子感到抱歉,我不知道在这段代码遇到这么多麻烦之后这个错误会从哪里来。非常感谢你。

1 个答案:

答案 0 :(得分:0)

你的onCreateViewHolder应该是这样的。错误是您将 list_row 布局类型转换为 TextView

        @Override
        public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                       int viewType) {
            View v = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.list_row, parent, false);

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