-1

I get that exception when i try to switch from one activity to another, as the app crashes. All other posts referencing this error point to id duplicates, but i cannot find a single instance of this.

Here's my main 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>

Here's my second 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>

And here's my third 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>

And here's my main activity class, 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();
    }

And finally, my adapter class, which is creatively named 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();
    }
}

Sorry about the absurdly long post, I just have no idea where this error is coming from after having so much trouble with this code. Thank you so much in advance.

Willzoy
  • 37
  • 1
  • 7
  • It's in `onCreateViewHolder()`. The root `View` in the `list_row` layout is a `RelativeLayout`, but you're trying to cast it to a `TextView` after inflation. "I just have no idea where this error is coming from" - If you read through the stack trace, it will tell you where the Exception is being thrown. – Mike M. Jan 22 '18 at 01:33
  • @Mike M. Sorry dude, I'm new to this and couldn't get my head around the debugger and just used it as reference as I searched for a solution – Willzoy Jan 22 '18 at 02:00
  • Well, I wasn't really talking about the live debugger. You found the stack trace, evidently. If you read through that, you'll see lines that reference your classes and methods, and the numbers at the end of those indicate the line numbers in your code. [This post](http://stackoverflow.com/questions/3988788) might be of some help to you. – Mike M. Jan 22 '18 at 02:05
  • 1
    @Mike M. Awh man, thanks so much, that's bookmarked now! – Willzoy Jan 22 '18 at 02:35

1 Answers1

0

Your onCreateViewHolder should be like this. Error is you are type casting list_row layout as 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;
        }
Hein Htet Aung
  • 844
  • 4
  • 16