我有一个viewpager,它包含3个片段,运行良好。我从viewpager中的任何片段启动一个活动并显示该活动。之后,当我按下后退按钮时,我的应用程序将崩溃,出现以下异常:
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to resume activity {AppName.Activities/AppName.Activities.ViewPagerActivity}: java.lang.IndexOutOfBoundsException: Invalid index 2, size is 0
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2120)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:957)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IndexOutOfBoundsException: Invalid index 2, size is 0
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:257)
at java.util.ArrayList.set(ArrayList.java:484)
at android.support.v4.app.FragmentStatePagerAdapter.destroyItem(FragmentStatePagerAdapter.java:97)
at android.support.v4.view.ViewPager.populate(ViewPager.java:415)
at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:271)
at android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:244)
at AppName.Activities.ViewPagerActivity.setUpView(ViewPagerActivity.java:36)
at AppName.Activities.ViewPagerActivity.onStart(ViewPagerActivity.java:28)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1129)
at android.app.Activity.performStart(Activity.java:3791)
at android.app.Activity.performRestart(Activity.java:3821)
at android.app.Activity.performResume(Activity.java:3826)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2110)
以下是扩展FragmentActivity的ViewPageActivity的代码:
public class ViewPagerActivity extends FragmentActivity
{
private ViewPager mViewPager;
private ViewPagerAdapter adapter;
boolean flag = false;
@Override
protected void onCreate(Bundle arg0)
{
super.onCreate(arg0);
setContentView(R.layout.view_pager);
}
@Override
protected void onStart()
{
super.onStart();
setUpView();
}
private void setUpView()
{
mViewPager = (ViewPager) findViewById(R.id.viewPager);
adapter = new ViewPagerAdapter(getApplicationContext(),getSupportFragmentManager());
mViewPager.setAdapter(adapter);
mViewPager.setCurrentItem(0);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.settings:
startActivity(new Intent(this, AppSettingsActivity.class));
return true;
case R.id.addSituationMenu:
Intent i = new Intent(this, MainLayout.class);
i.putExtra("parentActivity", "SplashScreenLayout");
startActivity(i);
return true;
case R.id.historyActivity:
startActivity(new Intent(this, HistoryActivity.class));
return true;
case R.id.chartActivity:
startActivity(new Intent(this, ViewPagerActivity.class));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
行mviewpager.setcurrentitem(0);导致崩溃。
这是viewPageRadapter代码:
public class ViewPagerAdapter extends FragmentStatePagerAdapter
{
private final Context context;
ArrayList<ArrayList<Object>> data;
int totalMoodEntries = 0;
static Fragment f = null;
public ViewPagerAdapter(Context mcontext, FragmentManager fm)
{
super(fm);
context = mcontext;
}
@Override
public Fragment getItem(int position)
{
switch (position)
{
case 0:
{
f = new ChartFragment(context, totalMoodEntries, data);
break;
}
case 1:
{
f = new ViewRecordsFragment(context, data);
break;
}
case 2:
{
f = new LearnMoreFragment(context);
break;
}
}
return f;
}
@Override
public int getCount()
{
return 3;
}
}
最佳答案
您可以尝试在onCreate()
方法中移动以下行:
mViewPager = (ViewPager) findViewById(R.id.viewPager);
adapter = new ViewPagerAdapter(getApplicationContext(),getSupportFragmentManager());
mViewPager.setAdapter(adapter);