按下后退按钮后的Android ViewPager异常

按下后退按钮后的Android ViewPager异常,第1张

概述我有一个ViewPager包含3个片段,工作正常.我从viewpager中的任何片段开始一个活动,并显示活动.之后当我按下后退按钮时,我的应用程序崩溃了以下例外情况: FATAL EXCEPTION: mainjava.lang.RuntimeException: Unable to resume activity {AppName.Activities/AppName.Activities.V 我有一个VIEwPager包含3个片段,工作正常.我从vIEwpager中的任何片段开始一个活动,并显示活动.之后当我按下后退按钮时,我的应用程序崩溃了以下例外情况:

FATAL EXCEPTION: mainjava.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)

以下是我的VIEwPagerActivity代码,它扩展了FragmentActivity ::

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);
总结

以上是内存溢出为你收集整理的按下后退按钮后的Android ViewPager异常全部内容,希望文章能够帮你解决按下后退按钮后的Android ViewPager异常所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1127297.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存