Android开发中的一些问题

1、AndroidStudio运行项目提示:解析软件包错误

把图片中箭头所指的地方不勾选

[外链图片转存失败(img-dRAahIpe-1563245029386)(https://github.com/androidzhoufeng/image/blob/master/blog_image/instant_run.png?raw=true)]

2、使用ARouter报错:there’s no route matched the path

一般是不同的module使用了相同的一级路径。

ARouter要求path必须有两级路径/app/xxx,第一级路径是Group的名称,在Arouter第一次寻找到route的时候便删除了这个一级路径的group,因为一级路径的重复,再调用另一个module的一级路径是”app”的路由时,由于之前Warehouse.groupsIndex已经删除,便导致了there’s no route matched的错误。

3、Dialog在使用ContentProvider的时候不显示

Dialog在使用ContentResiver导入通讯录的时候不显示
解决办法:开启子线程,把导入过程放入子线程中操作
原因:暂且不知

4、调整AndroidStudio内存

在AndroidStudio安装目录下的bin文件夹里找到studio64.exe.vmoptions文件:

-Xms512m
-Xmx3072m
-XX:MaxPermSize=1024m
-XX:ReservedCodeCacheSize=240m
-XX:+UseConcMarkSweepGC
-XX:SoftRefLRUPolicyMSPerMB=50
-Dsun.io.useCanonCaches=false
-Djava.net.preferIPv4Stack=true
-Djna.nosys=true
-Djna.boot.library.path=

-da

修改其中的Xms、Xmx、XX:MaxPermSize。

其中-Xms 是JVM启动的起始堆内存,堆内存是分配给对象的内存。一般默认设置是128m,建议改成512m
其中-Xmx 是 Java 虚拟机启动时的参数,用于限制最大堆内存。所以这里也需要更改。这里的更改根据电脑的实际情况进行分配。笔者这里设置了3072m。
其中-XX:MaxPermSize 是指定最大的Permanent generation大小。Permanent generation space,实际上就是方法区,存储了Class的信息以及一些其他信息。有时开发时出现的错误如Permgen Space方面的,就是指这个内存溢出了。所以一般这个可以不改,也可以加大一点。

5、ViewPager+Fragment,Fragment销毁后重新加载页面空白

两种情况:
1、重写FragmentPagerAdapter的destroyItem()方法:

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
    }

跳转到ViewPager页面时直接定位到第三个页面(或后面几个页面)时,切换到第一个页面,第一个页面空白。

2、不重写FragmentPagerAdapter的destroyItem()方法,当ViewPager页面显示过后被销毁然后再次显示时页面空白。

原因:
Fragment是绑定在Activity中的,Viewpager切换销毁Fragment时并没有走onDestroy()方法。Fragment中的全局变量也同样没有被释放回收。但是View已经被重新替换了:

    if (mAdapter == null) {
        mAdapter = new XiaoTookenAdapter(totals);
        mAdapter.setOnItemClickListener(XiaoTookenFragment.this);
        mAdapter.setLoadMoreView(new XiaoMeiLoadMoreView());
        mAdapter.bindToRecyclerView(mRecyclerView);
        mAdapter.setOnLoadMoreListener(XiaoTookenFragment.this, mRecyclerView);
    } else {
        mAdapter.notifyDataSetChanged();
    }

mAdapter不为空走else,但是mRecyclerView已经重新findViewById了。

解决:
在onDestroyView()方法中释放资源,清空数据。或者在onCreateView()初始化数据。或者将contentView也保存下来,不要每次都重新inflate一个View。

6、DataBinding 使用泛型报错

与元素类型 "variable" 相关联的 "type" 属性值不能包含 '<' 字符。

要使用 ‘<’ 的转义字符:<

7、DataBinding 使用ObservableMap进行数据绑定报错

Found data binding errors.
****/ data binding error ****msg:Cannot find the setter for attribute 'android:text' with parameter type V on android.widget.TextView.
file:E:\demo\DataBindingDome\app\src\main\res\layout\activity_main.xml
loc:46:28 - 46:41
****\ data binding error ****

layout 布局中声明ObservableMap需要添加泛型。否则使用的时候报错。

8、解决阿里云SDK出现的Stream closed

https://blog.csdn.net/weixin_33721427/article/details/87466839

9、CoordinatorLayout 嵌套Recyclerview再嵌套Recyclerview滑动出现冲突解决办法

给最里层的RecyclerView设置 nestedScrollingEnabled=“false”

CoordinatorLayout实现了NestedScrollingParent,纵向RecyclerView是CoordinatorLayout的子View,RecyclerView的滑动能通知到CoordinatorLayout,继而由CoordinatorLayout协调让CollapsingToolbarLayout发生折叠。

上面出bug的原因也能理解了,横向RecyclerView的父View是纵向RecyclerView,而RecyclerView只实现了NestedScrollingChild,无法像CoordinatorLayout一样响应。所以要关闭横向RecyclerView的嵌套滑动功能,让横向RecyclerView如同其他嵌入纵向RecyclerView的view一样,触发折叠。

10、Fragment点击事件穿透问题

通过FrameLayout布局添加Fragment,然后控制布局的显示和隐藏时会出现点击事件穿透的问题,即点击显示的Fragment下层隐藏的Fragment会响应点击事件。解决方法:在每个Fragment的根布局上添加android:clickable=“true”

11、JSON转义字符去除

依赖:“org.apache.commons:commons-text:1.7”
方法:StringEscapeUtils.unescapeJava(it)
eg:"{\"IsAnonym\": 1,\"Contents\": \u003Cp>回家姐姐聚义堂\u003C/p>,\"Title\": 哥古古惑惑}"
转换之后:"{"IsAnonym": 1,"Contents": <p>回家姐姐聚义堂</p>,"Title": 哥古古惑惑}"

12、Android5.0以下手机在请求接口的时候报 java.lang.NoClassDefFoundError 异常

我使用的是Retrofit2.0网络请求框架。
原因:我在项目中还是用了一个 com.liulishuo.okdownload:okhttp 下载框架,它里面包含了okhttp4.3.1,它仅支持Android5.0+。使用时Retrofit2中的okhttp3.10.0会被高版本覆盖,所以会报错。
解决:剔除 com.liulishuo.okdownload:okhttp 中的okhttp3包

	api (com.liulishuo.okdownload:okhttp:1.0.5){
        exclude group: 'com.squareup.okhttp3'
    }

13、Application onCreate()方法重复执行

部分第三方工具初始化时的时候会另起一个进程(如环信聊天),当app中有多个进程时,每个进程启动时都会初始化一次Application。

解决方法:获取当前进程名,与主项目包名比较判断是不是主进程。将只需要在主进程中初始化的操作放到这里进行。

/**
  * 获得当前进程的名字
  *
  * @param context
  * @return 进程号
  */

public static String getCurProcessName(Context context) {

    int pid = android.os.Process.myPid();

    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);

    for (ActivityManager.RunningAppProcessInfo appProcess : activityManager
            .getRunningAppProcesses()) {

        if (appProcess.pid == pid) {
            return appProcess.processName;
        }
    }
    return null;
}

14、ViewGroup down事件不被消费则不会产生其他事件

ViewGroup down事件没有被自己和子view消费,不会产生其他任何事件(其实是产生其他事件的时候被拦截了,不会传递到viewGroup的dispatchTouchEvent方法中)。
处理办法:可以在ViewgGroup中重写onTouchEvent()方法 return true 自己消费

@Override
public boolean onTouchEvent(MotionEvent event) {
    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            return true;
    }
    return super.onTouchEvent(event);
}

15、Activity#onResume() 方法中调用startService()报错

java.lang.IllegalStateException: Not allowed to start service Intent { cmp=com.example.servicedemo/.MyService }: app is in background uid UidRecord{66764c0 u0a86 LAST bg:+1m2s714ms idle change:idle procs:1 seq(0,0,0)}

分析:Android8.0后不允许app后台通过startService()去启动服务
文档连接:https://developer.android.google.cn/about/versions/oreo/background
但是我这里是在Activity#onResume()中启动的,按理说不会有问题,但是查看线上日志发现偶尔会出现崩溃。网上查找有人说是系统的一个bug。
处理:通过try{}cache(){}捕获这个异常,然后在cache中延迟1秒钟启动。

未完待续…
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值