Android.widget.framelayout cannot be cast to android.support.v4.view.viewpager
android, android-fragments, tabs
Solution
Can you post your xml layout file? And also the stacktrace?
If I had to guess I would say double check that your container `R.id.container` is declared as a `ViewPager` in your layout file.
EDIT: after your comment
In your java code you use ' viewPager = (ViewPager)findViewById(R.id.container);' Which means you try to cast the element called 'container' as a viewPager.
But in your xml file, the element named container is a 'FrameLayout'.
If you want to use a ViewPage, you need to change your layout, instead of:
<FrameLayout xmlns:android="schemas.android.com/apk/res/android"; xmlns:tools="schemas.android.com/tools"; android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="edu.wmich.lab4_jjohns1119.MainActivity" tools:ignore="MergeRootFrame" />
you need:
<FrameLayout xmlns:android="schemas.android.com/apk/res/android"
xmlns:tools="schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="edu.wmich.lab4_jjohns1119.MainActivity"
tools:ignore="MergeRootFrame" >
<android.support.v4.view.ViewPager
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</FrameLayout>
Or if you do want to use a FrameLayout, you need to change your cast. Instead of:
viewPager = (ViewPager)findViewById(R.id.container);
use
FrameLayout layout = (FrameLayout)findViewById(R.id.container);
Problem
My app keeps crashing. I'm trying to create a tab swype navigation setup. here is my code. adapter class ``` package tabsswipe.adapter; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; //import fragments import edu.wmich.lab4_jjohns1119.MainFragment; //import edu.wmich.tabswithswipe.PostFragment; //import edu.wmich.tabswithswipe.SuggestFragment; public class TabsPagerAdapter extends FragmentPagerAdapter { public TabsPagerAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int index) { switch (index) { case 0: // Main fragment activity return new MainFragment(); case 1: // Suggest fragment activity //return new SuggestFragment(); case 2: // Post fragment activity //return new PostFragment(); } return null; } @Override public int getCount() { // get item count - equal to number of tabs return 3; } ``` } Main activity ``` package edu.wmich.lab4_jjohns1119; import tabsswipe.adapter.TabsPagerAdapter; import android.app.ActionBar.Tab; import android.app.ActionBar.TabListener; import android.app.ActionBar; import android.app.FragmentTransaction; import android.os.Bundle; import android.support.v4.app.FragmentActivity; import android.support.v4.view.ViewPager; public class MainActivity extends FragmentActivity implements TabListener { private ViewPager viewPager; private TabsPagerAdapter mAdapter; private ActionBar actionBar; //tab titles private String[] tabs = {"Main", "Photo", "Share"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //initialization viewPager = (ViewPager)findViewById(R.id.container); actionBar = getActionBar(); mAdapter = new TabsPagerAdapter(getSupportFragmentManager()); viewPager.setAdapter(mAdapter); actionBar.setHomeButtonEnabled(false); actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); //adding the tabs to the action bar for(String tab_name : tabs) { actionBar.addTab(actionBar.newTab().setText(tab_name) .setTabListener(this)); } //on viewpager swiping, make the respective tab selected viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageSelected(int position) { // TODO Auto-generated method stub //on changing the page, make tab selected actionBar.setSelectedNavigationItem(position); } @Override public void onPageScrolled(int arg0, float arg1, int arg2) { // TODO Auto-generated method stub } @Override public void onPageScrollStateChanged(int arg0) { // TODO Auto-generated method stub } }); } @Override public void onTabSelected(Tab tab, FragmentTransaction ft) { // TODO Auto-generated method stub viewPager.setCurrentItem(tab.getPosition()); } @Override public void onTabUnselected(Tab tab, FragmentTransaction ft) { // TODO Auto-generated method stub } @Override public void onTabReselected(Tab tab, FragmentTransaction ft) { // TODO Auto-generated method stub } ``` } Main fragment ``` package edu.wmich.lab4_jjohns1119; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class MainFragment extends Fragment { public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_main, container,false); return rootView; } } ``` Manifest ``` <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="edu.wmich.lab4_jjohns1119" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="19" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="edu.wmich.lab4_jjohns1119.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name = "edu.wmich.lab4_jjohns1119.MainFragment" android:label = "@string/main_fragment_name"> </activity> </application> ``` the goal is to add more tabs so i can swype to navigate between fragments. it looks like i just have a casting error but nothing looks wrong to me. Can anyone help? EDIT: Here is my main XML ``` <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="edu.wmich.lab4_jjohns1119.MainActivity" tools:ignore="MergeRootFrame" /> ``` Fragment 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" > ```