Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Android Build a Self-Destructing Message Android App Using Fragments for Tabs Modifying Fragments from the Template

Martin Stryffeler
Martin Stryffeler
1,744 Points

Error 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

I 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.

Martin Stryffeler
Martin Stryffeler
1,744 Points

Thank you for finding this. It was a huge help.

Erin Kabbash
Erin Kabbash
5,210 Points

Ok.. 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
Rodrigo Rocha
4,042 Points

Martin Stryffeler

I 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.

add support.v4. before the app..... in the imports. Example: import android.support.v4.app.FragmentPagerAdapter;

Martin Stryffeler
Martin Stryffeler
1,744 Points

That 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.

on line 64, mSectionsPagerAdapter = new SectionsPagerAdapter(this, getFragmentManager()) change it to on line 64, mSectionsPagerAdapter = new SectionsPagerAdapter(this, getSupportFragmentManager())

Martin Stryffeler
Martin Stryffeler
1,744 Points

I get "The method getSupportFragmentManager() is undefined for the type MainActivity", even after organizing imports, and with both android.support.v13 and v4.

Erin Kabbash
Erin Kabbash
5,210 Points

Were 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
Erin Kabbash
5,210 Points

I am still stuck on this.. anyone find a solution?!

Thank you Martin Stryffeler Your solution solved the issue for me.

Christopher Hall
Christopher Hall
9,052 Points

Since 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
Martin Stryffeler
1,744 Points

This 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)"