Listing 11.7 I always get null as crimeId


#1

UUID crimeId = (UUID)getIntent().getSerializableExtra(CrimeFragment.EXTRA_CRIME_ID);
for(int i=0; i< mCrimes.size(); i++) {
if(mCrimes.get(i).getId().equals(crimeId)){
mViewPager.setCurrentItem(i);
break;
}
}

When I clicked one item, I always get null as “crimeId” , So I always end up first crime item, intead of the one I clicked.

I reviewed code several times ,did not find anything suspicious. Need help. thanks!

This is my full code of CrimePagerActivity.java

package com.bignerdranch.android.criminalintent;

import java.util.ArrayList;
import java.util.UUID;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentActivity;
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());
		}
	});
	
	UUID crimeId = (UUID)getIntent().getSerializableExtra(CrimeFragment.EXTRA_CRIME_ID);
	for(int i=0; i< mCrimes.size(); i++) {
		 if(mCrimes.get(i).getId().equals(crimeId)){
			 mViewPager.setCurrentItem(i);
			 break;
		 }
	}
	
}

}


#2

problem found and solved.
I forgot to change function :astonished:nListItemClick

Thanks for this wonderful book.