Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialMartin Stryffeler
1,744 PointsError on return new InboxFragment();
At the 6 minute mark in the Modifying Fragments from the Template video, when changing the getItem method of the SectionsPagerAdapter file, an error appears when changing from return fragment to return new InboxFragment();
The error reads "Type mismatch: cannot convert from InboxFragment to Fragment"
I've tried sorting imports and cleaning the project.
Here is InboxFragment.java
package com.stryffeler.ribbit;
import android.os.Bundle; import android.support.v4.app.ListFragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup;
public class InboxFragment extends ListFragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_inbox, container, false); return rootView; } }
Here is fragment_inbox.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" tools:context="com.stryffeler.ribbit.MainActivity$PlaceholderFragment" >
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ListView>
<TextView
android:id="@android:id/empty"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/empty_inbox_label" />
</RelativeLayout>
and here is SectionsPagerAdapter.java
package com.stryffeler.ribbit;
import java.util.Locale;
import android.app.Fragment; import android.app.FragmentManager; import android.content.Context; import android.support.v13.app.FragmentPagerAdapter;
/**
- A {@link FragmentPagerAdapter} that returns a fragment corresponding to
-
one of the sections/tabs/pages. */ public class SectionsPagerAdapter extends FragmentPagerAdapter {
protected Context mContext;
public SectionsPagerAdapter(Context context, FragmentManager fm) { super(fm); mContext = context; }
@Override public Fragment getItem(int position) { // getItem is called to instantiate the fragment for the given page. // Return a PlaceholderFragment (defined as a static inner class below).
return new InboxFragment();
}
@Override public int getCount() { return 2; }
@Override public CharSequence getPageTitle(int position) { Locale l = Locale.getDefault(); switch (position) { case 0: return mContext.getString(R.string.title_section1).toUpperCase(l); case 1: return mContext.getString(R.string.title_section2).toUpperCase(l); } return null; } }
Thanks for your help!
9 Answers
Hiroki Hokari
5,308 PointsI found this post in forum: https://teamtreehouse.com/forum/android-studio-return-new-inboxfragment
Check john appleby's comment starting "After trailing,,,,,". I'm currently building this app by creating the project from "File>New". And I found the default MainActivity.java inherites from:i.e. extends "Activity" not "FragmentActivity". I have a feeling I saw this correction in the video, but in my case I had forgot to do it, so I let you guys know here.
Erin Kabbash
5,210 PointsOk.. I finally figured out my issue. It was a few things so I will explain in case someone else runs into this issue. In my imports I was importing some v4 and some v13, they all need to match in every file in the project as they do not play nice together. Once I had that solved it fixed my issue but created another.. which was the error "The constructor FragmentPagerAdapter(FragmentManager) is undefined" which can be fixed by going back to MainActivity and changing the "mSectionsPagerAdapter = new SectionsPagerAdapter(this, getFragmentManager());" line to: " FragmentManager fragmentManager = getSupportFragmentManager(); mSectionsPagerAdapter = new SectionsPagerAdapter(this, fragmentManager); " and then organize your imports. Hopefully this helps someone because I was definitely stuck for a few days on this one!
Rodrigo Rocha
4,042 PointsI solved this problem as follows:
The PlaceholderFragment
class was removed from class MainActivity
exactly the same way as the class SectionsPagerAdapter
was removed from MainActivity
class. The PlaceholderFragment
class extends FragmentPagerAdapter
. This not changed nothing but it is best for understand the code.
The SectionsPagerAdapter
extends FragmentPagerAdapter
.
These are the imports need in this class:
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.ListFragment;
.
The MainActivity
should extends the FragmentActivity
class , not Activity
class.
These imports from v4 support libraries are needed to this code :
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
.
Please verify if there some v13 or v7 support imports and remove them.
I think that this can help you now.
Karan Ghai
1,382 Pointsadd support.v4. before the app..... in the imports. Example: import android.support.v4.app.FragmentPagerAdapter;
Martin Stryffeler
1,744 PointsThat conflicts with android.support.v13.app.FragmentPagerAdapter, so I get rid of v13. Then super(fm); on line 19 has the error "The constructor FragmentPagerAdapter(FragmentManager) is undefined", public Fragment getItem has the error "The return type is incompatible with FragmentPagerAdapter.getItem(int)". If I choose the quick fixes for these problems, an error pops up in MainActivity, on line 64, mSectionsPagerAdapter = new SectionsPagerAdapter(this, getFragmentManager());, "The constructor SectionsPagerAdapter(MainActivity, FragmentManager) is undefined".
The quickfix for this last error doesn't work, so I don't think that this is the solution.
Karan Ghai
1,382 Pointson line 64, mSectionsPagerAdapter = new SectionsPagerAdapter(this, getFragmentManager()) change it to on line 64, mSectionsPagerAdapter = new SectionsPagerAdapter(this, getSupportFragmentManager())
Martin Stryffeler
1,744 PointsI get "The method getSupportFragmentManager() is undefined for the type MainActivity", even after organizing imports, and with both android.support.v13 and v4.
Erin Kabbash
5,210 PointsWere you able to fix this? I am stuck on the same step. "The return type is incompatible with FragmentPagerAdapter.getItem(int)"
@Override public ListFragment getItem(int position) { // getItem is called to instantiate the fragment for the given page. // Return a PlaceholderFragment (defined as a static inner class below).
return new InboxFragment();
}
Erin Kabbash
5,210 PointsI am still stuck on this.. anyone find a solution?!
Alex Perea
1,960 PointsThank you Martin Stryffeler Your solution solved the issue for me.
Christopher Hall
9,052 PointsSince InboxFragment
extends ListFragment
, not Fragment
, I believe the solution is to change the return type of the getItem(int position)
method in SectionsPagerAdapter
from Fragment
to ListFragment
.
@Override
public ListFragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class below).
return new InboxFragment();
}
Martin Stryffeler
1,744 PointsThis was a suggested quick fix, but if I do this, there is an error on ListFragment stating, "The return type is incompatible with FragmentPagerAdapter.getItem(int)"
Martin Stryffeler
1,744 PointsMartin Stryffeler
1,744 PointsThank you for finding this. It was a huge help.