mXX mXX -4 years ago 140
Android Question

Start an activity from a fragment

I have 2 fragments with on both fragments a button. When I press the button I'd like to start a new Activity. But I can't get it to work.

The error I'm getting: ERROR here: Type mismatch: cannot convert from mFragmentFavorite to Fragment

What am I doing wrong?



public class MyFragmentPagerAdapter extends FragmentPagerAdapter{

final int PAGE_COUNT = 3;

/** Constructor of the class */
public MyFragmentPagerAdapter(FragmentManager fm) {

/** This method will be invoked when a page is requested to create */
public Fragment getItem(int arg0) {


case 0:
return new FavoriteActivity();
//ERROR: Type mismatch: cannot convert from FavoriteActivity to Fragment

case 1:
return new SettingsActivity();

return null;


/** Returns the number of pages */
public int getCount() {
return PAGE_COUNT;


import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;

public class FavoriteActivity extends Activity{

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.main_favorite, container, false);

OnClickListener listnr=new OnClickListener() {
public void onClick(View v) {
Intent i= new Intent("aFavorite");

Button btn =(Button) v.findViewById(;

return v;

If FavoriteActivity extends fragments, the error is gone but then I get an error at
and the error is

The method findViewById(int) is undefined for the type

Answer Source

mFragmentFavorite in your code is a FragmentActivity which is not the same thing as a Fragment. That's why you're getting the type mismatch. Also, you should never call new on an Activity as that is not the proper way to start one.

If you want to start a new instance of mFragmentFavorite, you can do so via an Intent.

From a Fragment:

Intent intent = new Intent(getActivity(), mFragmentFavorite.class);

From an Activity

Intent intent = new Intent(this, mFragmentFavorite.class);

If you want to start aFavorite instead of mFragmentFavorite then you only need to change out their names in the created Intent.

Also, I recommend changing your class names to be more accurate. Calling something mFragmentFavorite is improper in that it's not a Fragment at all. Also, class declarations in Java typically start with a capital letter. You'd do well to name your class something like FavoriteActivity to be more accurate and conform to the language conventions. You will also need to rename the file to if you choose to do this since Java requires class names match the file name.


Also, it looks like you actually meant formFragmentFavorite to be a Fragment instead of a FragmentActivity based on your use of onCreateView. If you want mFragmentFavorite to be a Fragment then change the following line of code:

public class mFragmentFavorite extends FragmentActivity{

Make this instead read:

public class mFragmentFavorite extends Fragment {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download