Android开发遇到的异常整理

2019-12-27  本文已影响0人  KyneMaster

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做处理

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内外数据不一致,从而报错。
解决

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手势缩放/翻页时的错误。
解决
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;
    }
}
上一篇下一篇

猜你喜欢

热点阅读