美文网首页
Android开发遇到的异常整理

Android开发遇到的异常整理

作者: KyneMaster | 来源:发表于2019-12-27 10:44 被阅读0次

title: Android开发遇到的异常整理
date: 2019-12-26 11:32:07
tags:
bolg: wwww.gitkyne.com


前言

记录在开发过程中遇到的不好找到问题的崩溃异常。

目录

目录结构

IllegalArgumentException

java.lang.IllegalArgumentException: You cannot start a load for a destroyed activity
    at com.bumptech.glide.manager.RequestManagerRetriever.assertNotDestroyed(RequestManagerRetriever.java:134)
    at com.bumptech.glide.manager.RequestManagerRetriever.get(RequestManagerRetriever.java:102)
    at com.bumptech.glide.Glide.with(Glide.java:653)
场景
当你的Activity重新创建并且Glide中的context是旧的时候,最容易出现。
换言之,context已经不在,但是异步处理刚回调到主线程更新UI,便会报异常
解决
问题发生在当前组件的上下文对象(Context)已经不存在,即围绕context做处理
  • 方案一
    使用控件的context,比如adapter中
    viewholder.imag.getContext()
    
  • 方案二
    getApplicationContext() 替代,
    然后在onDestroy() 中取消异步请求,避免多余的资源消耗
    Glide.with(getApplicationContext()).pauseRequests();
    
  • 方案三
    加载前做判断
    if (ContextUtils.isActivityRunning(xxActivity.this)) {}
    
     /**
     * 判断正在运行的Activity
     * @param context
     * @return  boolean
     */
    public static boolean isActivityRunning(Context context) {
        if (context == null) {
            return false;
        }
        if (context instanceof Activity) {
            return !((Activity) context).isFinishing();
        } else if (context instanceof ContextThemeWrapper) {
            Context baseContext = ((ContextThemeWrapper) context).getBaseContext();
            if (baseContext != null) {
                if (baseContext instanceof Activity) {
                    return !((Activity) baseContext).isFinishing();
                }
            }
        }
        return false;
    }
    

IllegalStateException

java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
    at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:2080)
    at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:2106)
    at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:683)
    at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:637)
    ...
场景
该异常发生FragmentManager的commit方法是在Activity的onSaveInstanceState()之后调用时发生。(在commit之前进行了异步请求)
原因是onSaveInstanceState()是在onDestroy()前调用来保存数据的,之后便不能再commit,故出错。
解决
commit()替换为commitAllowingStateLoss()

IndexOutOfBoundsException(Inconsistency detected)

java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionViewHolder{2064e5c6 position=2 id=-1, oldPos=2, pLpos:-1 scrap [attachedScrap] tmpDetached no parent}
    at android.support.v7.widget.RecyclerView Recycler.validateViewHolderForOffsetPosition(RecyclerView.java:4505)
    at android.support.v7.widget.RecyclerView Recycler.getViewForPosition(RecyclerView.java:4636)
    at android.support.v7.widget.RecyclerView Recycler.getViewForPosition(RecyclerView.java:4617)
    at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1994)
    at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1390)
    at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1353)
    at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:574)
    at android.support.v7.widget.RecyclerView.dispatchLayoutStep1(RecyclerView.java:2979)
    ...
场景
该问题算是RecyclerView的错误。对adapter传入前的List先移除一条数据,然后又添加数据,之后再notify,就会报错。notify前,adapter内部的数据还是list移除数据前的List,内外数据不一致导致。
即adapter内外数据不一致,从而报错。
解决
  • 方案一
    自己写一个继承LinearLayoutManager的包装类,在onLayoutChildren()方法里try-catch捕获该异常。
    @Override
    public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
        try {
            super.onLayoutChildren(recycler, state);
        } catch (IndexOutOfBoundsException e) {
            e.printStackTrace();
        }
    }
    
  • 方案二
    在进行数据移除和数据增加时,务必要保证RecyclerView的Adapter中的数据集和移除/添加等操作后的数据集保持一致!
    notifyItemRangeRemoved()
    notifyItemRangeInserted()
    notifyItemRangeChanged()
    使用这些方法之前先处理List数据,保证数据一致

    每一次对外部数据集做改动时,都需要紧接着主动对外部数据集和内部数据集做一次同步操作

  • 方案三
    下滑的同时到adapter更新数据完毕,让RecyclerView暂时禁止滑动。

PS:

不要在列表滑动的时候去更新你的数据源

IllegalArgumentException

java.lang.IllegalArgumentException: pointerIndex out of range pointerIndex=-1 pointerCount=1
    at android.view.MotionEvent.nativeGetAxisValue(MotionEvent.java)
    at android.view.MotionEvent.getX(MotionEvent.java:2216)
    at android.support.v4.view.ViewPager.onInterceptTouchEvent(ViewPager.java:2072)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2582)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3041)
    ....
场景
原因不详,ViewPager手势缩放/翻页时的错误。
解决
  • 简单有效的方法,try-catch异常发生的地方。即继承ViewPager重写onTouchEvent()onInterceptTouchEvent() 两个方法
public class ViewPagerFixed extends android.support.v4.view.ViewPager {
 
    public ViewPagerFixed(Context context) {
        super(context);
    }
 
    public ViewPagerFixed(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
 
    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        try {
            return super.onTouchEvent(ev);
        } catch (IllegalArgumentException ex) {
            ex.printStackTrace();
        }
        return false;
    }
 
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        try {
            return super.onInterceptTouchEvent(ev);
        } catch (IllegalArgumentException ex) {
            ex.printStackTrace();
        }
        return false;
    }
}

相关文章

网友评论

      本文标题:Android开发遇到的异常整理

      本文链接:https://www.haomeiwen.com/subject/rrnroctx.html