Listing 11.4


#1

In CrimePagerActivity.java i get "change return type of getItem() to fragment.

		@Override
		public Fragment getItem(int pos) {
			Crime crime = mCrimes.get(pos);
			return CrimeFragment.newInstance(crime.getId());
		}

If I do that I get other errors. It changes the code to:
public android.support.v4.app.Fragment getItem(int pos) {
Crime crime = mCrimes.get(pos);
return CrimeFragment.newInstance(crime.getId());
}

then i get a type mismatch, cannot convert from CrimeFragment to Fragment.

Where’s my problem?

Thanks


#2

Just in case, here’s the entire code:

import java.util.ArrayList;

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

public class CrimePagerActivity extends FragmentActivity {
private ViewPager mViewPager;
private ArrayList mCrimes;

@Override 
public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	mViewPager = new ViewPager(this);
	mViewPager.setId(R.id.viewPager);
	setContentView(mViewPager);
	
	mCrimes = CrimeLab.get(this).getCrimes();
	
	FragmentManager fm = getSupportFragmentManager();
	mViewPager.setAdapter(new FragmentStatePagerAdapter(fm) {
		@Override
		public int getCount() {
			return mCrimes.size();
		}
		
		@Override
		public Fragment getItem(int pos) {
			Crime crime = mCrimes.get(pos);
			return CrimeFragment.newInstance(crime.getId());
		}
	});
}

}


#3

Does your definition of CrimeFragment extend Fragment?


#4

I just checked, yes it does… plus I confirmed newInstance was there. Up through all of Chap 10 it’s been fine.

public static CrimeFragment newInstance(UUID crimeId) { // use to pass bundled args
Bundle args = new Bundle();
args.putSerializable(EXTRA_CRIME_ID, crimeId);

	CrimeFragment fragment = new CrimeFragment();
	fragment.setArguments(args);
	
	return fragment;
}

#5

I could really use some help here. I cant continue as I’m stuck since my code wont “compile”.

I’ve been using android fragments, since I didnt feel the need to go to early versions of android, however when implementing 11.4 I found i had to use the support.v4 lib.

Does that mean I need to change all of my code to use android.v4 wherever I see fragments? I there an “easy” way to do that? I sure dont wish to recode the entire thing.

Thanks for any help.


#6

The changes should be minor. Look at p. 147 “For the More Curious…”


#7

Yes, thanks.
In order to be safe what I did was delete all of the imports from each module and then made sure to select the support.v4 option when Eclipse made a suggestion. Also making changes such as getSupportxxxxx in place of getxxxxx.

Thanks for the help!