美文网首页Android
处理 TransactionTooLargeException

处理 TransactionTooLargeException

作者: 李云龙_ | 来源:发表于2019-04-18 16:51 被阅读0次

使用 bugly 可以看到 TransactionTooLargeException 异常

异常原因

activity 传输数据时超过 binder 的限制,抛出 TransactionTooLargeException,比如使用 intent 跳转时,intent.putExtra() 的东西太多

解决方案

intent 传递数据时不要传递大量数据,可以只传一个 id ,然后到下一个界面根据 id 联网获取数据,网上可能推荐使用如下方式来回避,但是不推荐!!

public class DataHolder {

    //    private HashMap<String, WeakReference> dataList = new HashMap<>();
    private HashMap<String, Object> dataList = new HashMap<>();

    private static volatile DataHolder instance;

    public static DataHolder getInstance() {
        if (instance == null) {
            synchronized (DataHolder.class) {
                if (instance == null) {
                    instance = new DataHolder();
                }
            }
        }
        return instance;
    }

    public <T> void setData(String key, T o) {
//        WeakReference<T> value = new WeakReference<T>(o);
//        dataList.put(key, value);
        dataList.put(key, o);
    }

    public <T> T getData(String key) {
//        WeakReference<T> reference = dataList.get(key);
//        if (reference != null) {
//            T o = reference.get();
//            dataList.remove(key);
//            return o;
//        }
//        return null;

        Object o = dataList.get(key);
        dataList.remove(key);
        return (T) o;
    }
}

这是把 WeakReference 注释了后的,因为使用 WeakReference 到下一个 activity 可能拿不到数据(已经被系统回收),导致直接崩溃,但是即使注释了有时候仍然拿不到数据,因为 DataHolder instance 是个静态全局变量,有不可预知的风险,所以每次乖乖的通过 intent 传递数据

其他

如果哪位大佬有更好的方法可以告诉我,,

相关文章

网友评论

    本文标题:处理 TransactionTooLargeException

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