前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Android大坑集锦

Android大坑集锦

作者头像
代码咖啡
发布2018-08-28 10:17:58
1.2K0
发布2018-08-28 10:17:58
举报
文章被收录于专栏:程序员叨叨叨程序员叨叨叨

一、 ImageView.setImageUri(Uri)显示不出图片

网上有这样的各种解决方案:

  1. ****将targetSdkVersion给注释掉
  2. targetSdkVersion和minSdkVersion保持一致
  3. 设置可见性:setVisibility(View.VISIBLE)

这些方法有的可以,有的不行,或许跟版本有关,或许Android本身不是很支持用setImageUri从网上获取图片吧!与其煞费苦心让这个方法有效,不如用ImageLoader来加载网上图片 吧!

二、 apk无法安装

  • The currently selected variant "debug" uses split APKs...

解决方法

在build.gradle(app)文件的defaultConfig里面添加如下代码:

代码语言:javascript
复制
ndk {
    abiFilters 'armeabi'// 'armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64', 'mips', 'mips64'
}

build.gradle(Module:app)

添加好以后,sync now即可。若依然出现这样的问题,可尝试重启Android Studio,若问题依旧,那我也不知道了-_-#,可以试着修改一下abiFilters的参数。

三、 DrawerLayout must be measured with MeasureSpec.EXACTLY

这个问题出现的原因多是: 多个可滑动的控件在同一个布局界面中嵌套使用,导致滑动冲突,因而Android错误地计算了内层的可滑动控件的尺寸。

解决方法

重写控件的onMeasure 方法

代码语言:javascript
复制
public class WrapContentRecyclerView extends RecyclerView {
    public WrapContentRecyclerView(Context context) {
        super(context);
    }
    public WrapContentRecyclerView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }
    public WrapContentRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }
    @Override
    protected void onMeasure(int widthSpec, int heightSpec) {
        widthSpec = MeasureSpec.makeMeasureSpec(
                MeasureSpec.getSize(widthSpec), MeasureSpec.EXACTLY);
        heightSpec = MeasureSpec.makeMeasureSpec(
                MeasureSpec.getSize(heightSpec), MeasureSpec.EXACTLY);
        super.onMeasure(widthSpec, heightSpec);
    }
}

四、 RecyclerView显示不了数据

RecyclerView 需要重新设置一下LayoutManger才可以显示:

代码语言:javascript
复制
// 计算RecyclerView的大小,可以显示器内容
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));

五、 java.lang.IllegalStateException: RecyclerView has no LayoutManager

这个问题的原因是: 在RecyclerView中添加其他控件,如这样:

代码语言:javascript
复制
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/my_recycler_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clickable="true"
    android:scrollbars="vertical" >

    <View android:id="@+id/randomview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center" />

</android.support.v7.widget.RecyclerView>

比如我实现RecyclerView的上拉加载功能,滑动到列表最底端,会出现“点击加载更多”,每次执行到这里的时候,就会出现RecyclerView has no LayoutManager

解决方法

代码语言:javascript
复制
// 避免出现RecyclerView has no LayoutManager的错误
mRecyclerView.setHasFixedSize(true);
// 计算RecyclerView的大小,可以显示其内容
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

六、 Android Studio的LogCat无法打印信息

关掉手机或模拟器中运行的应用,重新打开应用即可。

七、 Error install apk

**方法一: 关闭Instant Run;方法一未解决,详见方法二

方法二:由于NDK默认支持的系统框架与调试的机子不符,可在build.gralde(Module:app)文件中合适的位置添加如下代码:

代码语言:javascript
复制
android {
    defaultConfig {
        ndk {
            abiFilters 'armeabi', 'armeabi-v7a', 'x86'//, 'arm64-v8a', 'x86_64', 'mips', 'mips64'
        }
    }
}

若方法二未解决,详见方法三

方法三:拔掉手机,重插并重启Android Studio。 若方法三不行,建议你去买彩票吧!中了五百万就可以不用苦逼地写代码了。

八、 'int android.view.View.getImportantForAccessibility()' on a null...'

其详细报错信息是:

代码语言:javascript
复制
java.lang.NullPointerException: 
Attempt to invoke virtual method 'int android.view.View.getImportantForAccessibility()' 
on a null object reference

这个错误多为ListView或GridView的Adapter中,getView方法返回null的结果。

九、 符号找不到

检查是否存在或R文件导包是否正确。

十、 使用ButterKnife,报控件空指针

这种情况多半是因为ButterKnife没有配置好,如今ButterKnife的最新版本是8.4.0,其配置方法如下:

Step 1: 配置app的build.gradle

代码语言:javascript
复制
apply plugin: 'com.neenbedankt.android-apt'
apply plugin: 'com.jakewharton.butterknife'
...

dependencies {
    ...
    compile 'com.jakewharton:butterknife:8.4.0'    
    apt 'com.jakewharton:butterknife-compiler:8.4.0'
}

Step 2: 配置project的build.gradle

代码语言:javascript
复制
buildscript {
    repositories {
        ...
        mavenCentral()
    }
    dependencies {
        ...
        classpath 'com.jakewharton:butterknife-gradle-plugin:8.4.0'
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
    }
}

Step 3: 在Activity中绑定ButterKnife

代码语言:javascript
复制
ButterKnife.bind(this);

十一、 RecyclerView的item内容填充不满

Recyclerview的item设置的控件宽高,凡是MATCH_PARENTweight="1"+0dp的,通通变成了WRAP_CONTENT!

其效果如图所示:

图片盗自[http://bbs.csdn.net/topics/391937879?page=1](http://bbs.csdn.net/topics/391937879?page=1)

问题解决:

View.inflate(Context, Layout, null)

在填充内容的时候,我使用了View.inflate(Context, Layout, null)方法,如果我们将内容填充的方法改为LayoutInflater.from(Context).inflate(Layout, Parent, false)即可解决该问题。

知其然,知其所以然!我们来看看源码:

View.inflate方法源码

代码语言:javascript
复制
@param root A view group that will be the parent.  Used to properly inflate the* layout_* parameters.

root参数是用来充当我们RecyclerView的item的父容器用的,如果我们将父容器设置为了null,我们的item在设置大小上就没有了参照物,所以内容的现实效果都是WRAP_CONTENT

LayoutInflater.from(Context).inflate()方法源码

代码语言:javascript
复制
If false, root is only used to create the correct subclass of LayoutParams for the root view in the XML.

上面的话说的是,如果我们将参数设置为false,我们RecyclerView的item子项还是有父容器可以依靠的,这个参数root作为父容器用来规范item子项用的。

十二、 Android5.0环境下的CardView无法显示阴影

CardView添加margin,外边距大小与阴影大小一致。

十三、 A TaskDescription's primary color should be opaque

Android的colorPrimary不允许带有alpha值。

十四、 canvas的drawText绘制的文字第一行显示不全

drawText以文字的左下角为坐标,所以绘制的时候需要预留出一个字高的高度。

十五、 Binary XML file line # : Error inflating class

出现这种问题的原因可归结为以下几类:

  1. xml中自定义控件的包名未填写完整
  2. 自定义控件构造方法没有将三个构造函数都实现
代码语言:javascript
复制
//Simple constructor to use when creating a view from code
View(Context context)    
//Constructor that is called when inflating a view from XML
View(Context context, AttributeSet attrs)     
//Perform inflation from XML and apply a class-specific base style
View(Context context, AttributeSet attrs, int defStyle)     
  1. 中间文件没有清理干净 rebuild或clear一下工程
  2. 资源文件找不到

根据问题的不同具体解决。

十六、 LinearLayoutManager.scrollToPosition滚动无效

大家都知道,要想实现RecyclerView滚动,可以调用scrollToPosition方法,或得到它的LinearLayoutManager,然后调用scrollToPosition来实现,但是今天在同一个方法里调用scrollToPosition,发现上一行执行了,下面的行却未执行?

scrollToPosition方法执行无效

解决方案: 用LinearLayout.scrollToPositionWithOffset(position, 0)来替换scrollToPosition(position)方法。 原因分析: scrollToPosition有效执行的条件是position对应的Item不在屏幕可见范围内,而scrooToPositionWithOffset无这样的限制,所以本人黄色框框里的代码无法实现滚动的原因是target对应的item在可见范围。

大坑持续挖掘中。。。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016.09.21 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档