Answer the question
In order to leave comments, you need to log in
Can't start Activity. What to do?
Can't start Activity in any way.
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 Fragment getItem(int pos) {
Crime crime = mCrimes.get(pos);
return CrimeFragment.newInstance(crime.getId());
}
@Override
public int getCount() {
return mCrimes.size();
}
});
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;
}
}
mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
public void onPageScrollStateChanged(int state) {}
public void onPageScrolled(int pos, float posOffset, int posOffsetPixels) {}
public void onPageSelected(int pos) {
Crime crime = mCrimes.get( pos);
if (crime.getTitle() != null) {
setTitle(crime.getTitle());
}
}
});
}
}
By trial and error, I found out that the problem is in this section of code (if you remove it, then it starts):
mViewPager.setAdapter(new FragmentStatePagerAdapter(fm) {
@Override
public Fragment getItem(int pos) {
Crime crime = mCrimes.get(pos);
return CrimeFragment.newInstance(crime.getId());
}
@Override
public int getCount() {
return mCrimes.size();
}
});
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;
}
}
mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
public void onPageScrollStateChanged(int state) {}
public void onPageScrolled(int pos, float posOffset, int posOffsetPixels) {}
public void onPageSelected(int pos) {
Crime crime = mCrimes.get( pos);
if (crime.getTitle() != null) {
setTitle(crime.getTitle());
}
}
});
Tell me what's the problem? Everything worked before.
Code that starts the activity:
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.menu_item_new_crime:
Crime crime = new Crime();
CrimeLab.get(getActivity()).addCrime(crime);
Intent i = new Intent(getActivity(), CrimePagerActivity.class);
i.putExtra(CrimeFragment.EXTRA_CRIME_ID, crime.getId());
startActivityForResult(i, 0);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Answer the question
In order to leave comments, you need to log in
Are you by any chance studying a book by Brian Hardy and Bill Philips? Familiar code, recently parsed. Throw off the entire project, for example, on github, I’ll help you freely, otherwise you don’t have part of the classes here. Or you can see the same code from me, my activity is launched from the CrimeListFragment fragment.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question