Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

 @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;
    }

the error printed is:

incompatible types,required android.app.fragment but found activity.messagefragment
like image 447
user3409897 Avatar asked Sep 10 '15 07:09

user3409897


4 Answers

In your messagefragment class you need to import

import android.app.Fragment;

Instead of

import android.support.v4.app.Fragment;
like image 169
Android Team Avatar answered Nov 07 '22 13:11

Android Team


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

You should have this in there:

import android.app.Fragment;
like image 3
Oskari Mantere Avatar answered Nov 07 '22 14:11

Oskari Mantere


Your Activity must extend from AppCompatActivity, then you call your fragment using getSupportFragmentManager, that should do it.

It'll go like this:

getSupportFragmentManager().beginTransaction().replace(R.id.main_container, TabFragment.newInstance()).commit();
like image 3
Edward Avatar answered Nov 07 '22 14:11

Edward


Check imports of all the fragment java classes and make sure that they all have

import android.app.Fragment;

Instead of

import android.support.v4.app.Fragment;

It solves.

like image 1
Shobhit Sagar Avatar answered Nov 07 '22 13:11

Shobhit Sagar