khoi khoi - 2 months ago 19
Android Question

android: receive exception when view next tab

I want to add Recyclerview inside fragment then after that want to add fragment inside viewpager.So i follow tutorial on youtube.after compile..recyclerview is display but when i go to next tabs,exception appear.Hope you guys can help me solve this problem.

ERROR

D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.user.recyclerviewpagerfragment, PID: 3705
java.lang.ClassCastException: android.support.v7.widget.LinearLayoutCompat cannot be cast to android.support.v7.widget.RecyclerView
at com.example.user.recyclerviewpagerfragment.Fragments.DocumentaryFragment.onCreateView(DocumentaryFragment.java:30)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2080)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1108)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1290)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:801)
at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:1638)
at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:679)
at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:143)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1240)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1088)
at android.support.v4.view.ViewPager$3.run(ViewPager.java:275)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
at android.view.Choreographer.doCallbacks(Choreographer.java:670)
at android.view.Choreographer.doFrame(Choreographer.java:603)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
I/Process: Sending signal. PID: 3705 SIG: 9
Application terminated.


MainActivity.java

package com.example.user.recyclerviewpagerfragment;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;

import com.example.user.recyclerviewpagerfragment.Fragments.CrimeFragment;
import com.example.user.recyclerviewpagerfragment.Fragments.DocumentaryFragment;
import com.example.user.recyclerviewpagerfragment.Fragments.DramaFragment;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

//initialize view pager.
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager_id);
this.addPages(viewPager);

TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_id);
//it gonna occupy the whole screen..
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
tabLayout.setupWithViewPager(viewPager);
tabLayout.addOnTabSelectedListener(listener(viewPager));
}

// add all pages
private void addPages(ViewPager pager){
FragPagerAdapter adapter = new FragPagerAdapter(getSupportFragmentManager());
adapter.addPage(new CrimeFragment());
adapter.addPage(new DramaFragment());
adapter.addPage(new DocumentaryFragment());

//set adapter to pager
pager.setAdapter(adapter);
}

private TabLayout.OnTabSelectedListener listener(final ViewPager pager){
return new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
pager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(TabLayout.Tab tab) {

}

@Override
public void onTabReselected(TabLayout.Tab tab) {

}
};

}

}


FragPagerAdapter.java

package com.example.user.recyclerviewpagerfragment;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

import java.util.ArrayList;

/**
* Created by User on 10/4/2016.
*/

public class FragPagerAdapter extends FragmentPagerAdapter{

//create arraylist..where its gonna hol fragment.

ArrayList<Fragment> pages = new ArrayList<>();

public FragPagerAdapter(FragmentManager fm) {
super(fm);
}

@Override
public Fragment getItem(int position) {
return pages.get(position);
}

@Override
public int getCount() {
return pages.size();
}

// add a page
public void addPage(Fragment f){
pages.add(f);
}

//set title for tab

@Override
public CharSequence getPageTitle(int position) {
return pages.get(position).toString();
//return super.getPageTitle(position);
}
}


MyRecyclerAdapter.java

package com.example.user.recyclerviewpagerfragment.Recycler;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.user.recyclerviewpagerfragment.R;

import java.util.ArrayList;

/**
* Created by User on 10/4/2016.
*/

public class MyRecyclerAdapter extends RecyclerView.Adapter<MyViewHolder> {

Context context;
ArrayList<Movie> movies;

public MyRecyclerAdapter(Context context, ArrayList<Movie> movies) {
this.context = context;
this.movies = movies;
}

//initialize holder.
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.model,null);
MyViewHolder holder = new MyViewHolder(view);
return holder;
//return null;
}


//bind data to views
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.nameTxt.setText(movies.get(position).getImage());
holder.img.setImageResource(movies.get(position).getImage());

//listener
holder.setItemClickListener(new ItemClickListener() {
@Override
public void onItemClick(View view, int position) {
Toast.makeText(context,movies.get(position).getName(),Toast.LENGTH_SHORT).show();
}
});
}

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


MyViewHolder.java

package com.example.user.recyclerviewpagerfragment.Recycler;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.user.recyclerviewpagerfragment.R;

/**
* Created by User on 10/4/2016.
*/

public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

ImageView img;
TextView nameTxt;
ItemClickListener itemClickListener;
public MyViewHolder(View itemView) {
super(itemView);

nameTxt = (TextView) itemView.findViewById(R.id.nameTxt);
img = (ImageView) itemView.findViewById(R.id.movieImage);

//above...if want to make click on nameTxt,can change itemView to nameTxt
itemView.setOnClickListener(this);
}

public void setItemClickListener(ItemClickListener itemClickListener){
this.itemClickListener = itemClickListener;
}

@Override
public void onClick(View view) {
this.itemClickListener.onItemClick(view,getLayoutPosition());
}
}


DocumentaryFragment.java

package com.example.user.recyclerviewpagerfragment.Fragments;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.user.recyclerviewpagerfragment.R;
import com.example.user.recyclerviewpagerfragment.Recycler.Movie;
import com.example.user.recyclerviewpagerfragment.Recycler.MyRecyclerAdapter;

import java.util.ArrayList;

/**
* Created by User on 10/4/2016.
*/

public class DocumentaryFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

//call view
View view = inflater.inflate(R.layout.documentary_fragment,null);
//recyclerview
RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.mRecyclerDocumentary);
recyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity()));
//set Adapter
recyclerView.setAdapter(new MyRecyclerAdapter(this.getActivity(),getDocumentaryMovies()));
return view;
//return super.onCreateView(inflater, container, savedInstanceState);
}

private ArrayList<Movie> getDocumentaryMovies() {
//collection of crime movies.
ArrayList<Movie> movies = new ArrayList<>();
//single movie
Movie movie = new Movie("Crime:tank5",R.drawable.e);
//add to collection..
movies.add(movie);

movie = new Movie("Crime:tank6",R.drawable.f);
movies.add(movie);
return movies;
/*
Movie movie = new Movie("tank1",R.drawable.a);
movies.add(movie);

Movie movie = new Movie("tank1",R.drawable.a);
movies.add(movie);
*/
}


//set title for the fragment

@Override
public String toString() {
return "Documentary";
}
}


activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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="com.example.user.recyclerviewpagerfragment.MainActivity">

<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">

<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/AppTheme.PopupOverlay" />

<!-- add above for adding tablayout -->
<android.support.design.widget.TabLayout
android:id="@+id/tab_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

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

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

<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@android:drawable/ic_dialog_email" />

<!-- add above for adding viewpager -->
<android.support.v4.view.ViewPager
android:id="@+id/viewpager_id"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />

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


Documentary_fragment.xml



<android.support.v7.widget.LinearLayoutCompat
android:id="@+id/mRecyclerDocumentary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></android.support.v7.widget.LinearLayoutCompat> </LinearLayout>


Model.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_margin="10dp"
card_view:cardCornerRadius="10dp"
card_view:cardElevation="10dp"
android:layout_height="wrap_content">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/movieImage"
android:padding="10dp"
android:src="@drawable/a"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Name"
android:id="@+id/nameTxt"
android:padding="10dp"
android:textColor="@color/colorAccent"
android:layout_below="@+id/movieImage"
android:layout_alignParentLeft="true"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="this tanks is awsome no need to jurge it."
android:id="@+id/descTxt"
android:layout_below="@+id/nameTxt"
android:layout_alignParentLeft="true"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="TV Show"
android:id="@+id/posTxt"
android:padding="10dp"
android:layout_below="@id/movieImage"
android:layout_alignParentRight="true"/>

<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/chk"
android:layout_alignParentRight="true"/>
</RelativeLayout>
</android.support.v7.widget.CardView>

Answer

the error is

java.lang.ClassCastException: android.support.v7.widget.LinearLayoutCompat cannot be cast to android.support.v7.widget.RecyclerView
                  at com.example.user.recyclerviewpagerfragment.Fragments.DocumentaryFragment.onCreateView(DocumentaryFragment.java:30)

As it says the problem is in DocumentaryFragment, you are casting a LinearLayout to a ReciclerView, you should provide us that Fragment. Hope it helps, I would like to comment but I do not have the requisites yet.