createFragment( ) in SingleFragmentActivity.java Error


#1

I am getting an error on method createFragment( ) even though I imported import android.support.v4.app.Fragment; . Below is my code. I am using Android Studio 2.2.3 Any assistance is appreciated.

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;

/**

  • Created by kimcook on 12/19/16.
    */

public abstract class SingleFragmentActivity extends FragmentActivity {

protected abstract Fragment createFragment();

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_fragment);

    FragmentManager fm = getSupportFragmentManager();
    Fragment fragment = fm.findFragmentById(R.id.fragment_container);

    if (fragment == null) {
        fragment = new createFragment();
        fm.beginTransaction()
                .add(R.id.fragment_container, fragment)
                .commit();

    }
}

}


#2

I think the error is here (the text is highlighted),

Instead of fragment = new createFragment(); it should be only fragment = createFragment();
Hope that solves it!


#3

Thank you oosinoots! Removing “new” corrected the error.


#4

I am getting an error on method CrimeListFragment( ) even though I imported import android.support.v4.app.Fragment; . Below is my code. What is the problem here??

CrimeListActivity:

package com.bignerdranch.android.criminalintent;
import android.support.v4.app.Fragment;

public class CrimeListActivity extends SingleFragmentActivity {

@Override
protected Fragment createFragment() {
  > return new CrimeListFragment();
}

}

CrimeActivity:

package com.bignerdranch.android.criminalintent;

import android.support.v4.app.Fragment;

public class CrimeActivity extends SingleFragmentActivity {

@Override
protected Fragment createFragment() {
    return new CrimeFragment();
}

}


#5

Check the imports in SingleFragmentActivity to make sure that you are importing the support version of all of the fragment classes. I bet that you have an android.app import of the FragmentManager or Fragment class in that activity. Change these imports to the support version.


#6

Thanks for the reply cstewart.

I have used the same imports for all the fragment can u please help me i got stuck here.

My SingleFragmentActivity ;

package com.bignerdranch.android.criminalintent;

import android.os.Bundle;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;

public abstract class SingleFragmentActivity extends FragmentActivity {

protected abstract Fragment createFragment();
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_fragment);
    FragmentManager fm = getSupportFragmentManager();
    Fragment fragment = fm.findFragmentById(R.id.fragment_container);
    if (fragment == null) {
        fragment = createFragment();
        fm.beginTransaction()
                .add(R.id.fragment_container, fragment)
                .commit();
    }
}

}


#7

What imports do you have in your Fragment classes? Also, what’s the error message that you are getting?


#8

Hi cstewart thanks for responding.

I do have the correct support version imported.My Error got cleared

android studio 2.0 needs you to import like this on the single fragment activity that was my mistake

android.support.v4.app.FragmentManager fm = getSupportFragmentManager();
Fragment fragment = fm.findFragmentById(R.id.fragment_container);

if(fragment == null){
    fragment = createFragment();
    fm.beginTransaction().add(R.id.fragment_container, fragment).commit();
}

}


#9

Glad you got it working.

This line here:
android.support.v4.app.FragmentManager fm = getSupportFragmentManager();

Tells me that there’s something strange going on with your imports. If you put your cursor on FragmentManager in this line and use the option (or alt) + enter shortcut, IntelliJ should provide a quick fix that updates your imports so that you can refer to the Fragment Manger class in a normal way. This line will become:

FragmentManager fm = getSupportFragmentManager();


#10

yeah you are right,first i got imported the fragment manager like you said before by using the option (Alt +enter),then i read some of the errors and comments in this Chapter.After that i got clear.
Thanks for your reply cstewart


#11

I have the same error. I found some lib are not compatible together (depending on version) in my case Recyclerview 27.

I think a teacher can better explain than me…