Fragment error: incompatible types, android.app.fragment required, but activity.messagefragment found

@Override public void onDrawerItemSelected(View view, int position) { displayView(position); } private void displayView(int position) { Fragment fragment = null; String title = getString(R.string.app_name); switch (position) { case 0: fragment = new HomeFragment(); title = getString(R.string.title_home); break; case 1: fragment = new FriendsFragment(); title = getString(R.string.title_friends); break; case 2: fragment = new MessageFragment(); title = getString(R.string.title_messages); break; default: break; } 

printed error:

 incompatible types,required android.app.fragment but found activity.messagefragment 
+5
source share
7 answers

In your messagefragment class you need to import

 import android.app.Fragment; 

Instead

 import android.support.v4.app.Fragment; 
+14
source

You may have included the wrong class, check your import statements.

You should have this:

 import android.app.Fragment; 
+2
source

Check the import of all classes of java fragments and make sure that they all have

import android.app.Fragment;

Instead

import android.support.v4.app.Fragment;

Decides.

+1
source

Verify that both imports and snippets should be the same android.app.fragment

0
source

You need to return an object of class android.app.Fragment , but you do not return android.app.Fragment , as in case 2, which is equal to MessageFragment() . You must extend android.app.Fragment in MessageFragment .

0
source

go to tab.class which has a problem and change import android.app.Fragment; to import android.support.v4.app.Fragment;

it will start.

0
source

Your Activity should extend from AppCompatActivity , then you call your fragment using getSupportFragmentManager , which should do this.

It will look like this:

 getSupportFragmentManager().beginTransaction().replace(R.id.main_container, TabFragment.newInstance()).commit(); 
0
source

All Articles