Problem with Listing 9.10


#1

I created the SingleFragmentActivity abstract class but I’m getting an error about “createFragment cannot be resolved to a type” Here is my code:

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 manager = getSupportFragmentManager();
	Fragment fragment = manager.findFragmentById(R.id.fragmentContainer);
	if (fragment == null)
	{
		fragment = new createFragment();
		manager.beginTransaction()
			.add(R.id.fragmentContainer, fragment)
			.commit();
	}
}

}


#2

You are calling a method (createFragment()) and you are putting the output of her at your variable.
You are not creating something new so just remove that from there, and you will be fine. :slight_smile:

fragment = createFragment();

#3

Thanks, that was it.


#4

I get an error with R.id.fragmentContainer, shouldnt it be R.id.fragment_container ?

I searched the generated R and I dont find any fragmentContainer. Am I missing something? I dont think so.


#5

fragmentContainer was defined in activity_crime.xml. See Listing 9.7.


#6

My bad…sorry.

I had used fragment_container in the xml. I should have looked.


#7

I am also getting the fragment cannot be resolved error and yet here is my activity_crime:

<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/[b]fragmentContainer[/b]" android:layout_width="match_parent" android:layout_height="match_parent" />

This is my SingleFragmentActivity.java:

[code]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;
import android.R;

import criminalintent.android.bignerdranch.com.criminalintent.CrimeFragment;

public abstract class SingleFragmentActivity extends FragmentActivity {
protected abstract Fragment createFragment();
/** Called when the activity is first created. */

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_crime);
    FragmentManager manager = getSupportFragmentManager();
    Fragment fragment = manager.findFragmentById(R.id.fragmentContainer);

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

}[/code]

I have fragmentContainer in activity_crime and yet it still cannot find it…


#8

Hi,

I have the same problem as the one originally posted here, with the exception that removing the ‘new’ doesn’t resolve the problem. It still says it cannot resolve the symbol “createFragment”

Here is the exact code I have now for SingleFragmentActivity.java:

package wheaslip.com.crimeactivity;

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

/**

  • Created by User on 10/01/2015.
    */
    public abstract class SingleFragmentActivity extends FragmentActivity {
    protected abstract Fragment createFragment();

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

     FragmentManager fm = getFragmentManager();
     Fragment fragment = fm.findFragmentById(R.id.fragmentContainer);
    
     if (fragment == null) {
         fragment = [color=#FF0000]createFragment[/color]();
         // Fragment transactions are used to add, remove, attach, detach or
         // replace fragments in the fragment list.
         // The container tells the fm where the fragment should be in the view and helps id it.
         fm.beginTransaction().add(R.id.fragmentContainer, fragment).commit();
     }
    

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

     //noinspection SimplifiableIfStatement
     if (id == R.id.action_settings) {
         return true;
     }
    
     return super.onOptionsItemSelected(item);
    

    }
    }


#9

I got it fixed on my own. In my case the code was fine; I shut down and reinitialized Android Studio and the problem went away.