无法解析newInstance

时间:2016-10-28 16:17:07

标签: android android-viewpager fragmentstatepageradapter

我正在从bignerdranch学习android开发,我正在尝试实现寻呼机适配器,但在Android Studio中遇到错误,无法解析newInstance方法。我在每个类中使用支持版本的Fragment库来创建Fragments。

public class CrimePagerActivity extends FragmentActivity {
private static final String EXTRA_CRIME_ID =
        "com.bignerdranch.android.criminalintent.crime_id";

private ViewPager mViewPager;
private List<Crime> mCrimes;

public static Intent newIntent(Context packageContext, UUID crimeId){
Intent intent = new Intent(packageContext,CrimePagerActivity.class);
intent.putExtra(EXTRA_CRIME_ID,crimeId);
return intent;}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_crime_pager);

UUID crimeId = (UUID) getIntent()
.getSerializableExtra(EXTRA_CRIME_ID);

mViewPager = (ViewPager) findViewById(R.id.activity_crime_pager_view_pager);
    mCrimes = CrimeLab.get(this).getCrimes();
    FragmentManager fragmentManager = getSupportFragmentManager();
    mViewPager.setAdapter(new FragmentStatePagerAdapter(fragmentManager) {
        @Override
        public Fragment getItem(int position) {
            Crime crime  = mCrimes.get(position);public class CrimePagerActivity extends FragmentActivity {
private static final String EXTRA_CRIME_ID =
        "com.bignerdranch.android.criminalintent.crime_id";

private ViewPager mViewPager;
private List<Crime> mCrimes;

public static Intent newIntent(Context packageContext, UUID crimeId){
    Intent intent = new Intent(packageContext,CrimePagerActivity.class);
    intent.putExtra(EXTRA_CRIME_ID,crimeId);
    return intent;}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_crime_pager);

    UUID crimeId = (UUID) getIntent()
            .getSerializableExtra(EXTRA_CRIME_ID);

    mViewPager = (ViewPager) findViewById(R.id.activity_crime_pager_view_pager);
    mCrimes = CrimeLab.get(this).getCrimes();
    FragmentManager fragmentManager = getSupportFragmentManager();
    mViewPager.setAdapter(new FragmentStatePagerAdapter(fragmentManager) {
    @Override
    public Fragment getItem(int position) {
    Crime crime  = mCrimes.get(position);
    return CrimeFragment.newInstance(crime.getId());
        }

@Override
public int getCount() {
return mCrimes.size();
        }
    });

}}
return CrimeFragment.newInstance(crime.getId());
        }
@Override
public int getCount() {
return mCrimes.size();
        }
    });

}}

1 个答案:

答案 0 :(得分:1)

您不能将参数传递给newInstance,它应该像这样

return CrimeFragment.newInstance();