目录
汇总开发中遇到的没有能快速解决的问题,留下记录以供参考。
View相关
SurfaceView
- canvas绘制时会有拖尾现象
解决方案:添加canvas.drawColor(Color.TRANSPARENT,PorterDuff.Mode.CLEAR); 用以清除画布
- SurfaceView背景全黑
解决方案:初始化时添加
this.setZOrderOnTop(true);
getHolder().setFormat(PixelFormat.TRANSLUCENT);
需要注意setZOrderOnTop会让surfaceview显示在最顶层。
TextView
- 使用Databinding设置android:background时TextView的文字会被截断,类似设定了padding的效果,原因未知
解决方案:增加使用selector设置背景
- 使用android:ellipsize="marquee"属性,若TextView是代码动态设置的text,跑马灯不会进行
解决方案:在设置完text之后把xml中属性重新设置一遍:
textView.setEllipsize(TextUtils.TruncateAt.MARQUEE);
textView.setSingleLine(true);
textView.setSelected(true);
textView.setFocusable(true);
textView.setFocusableInTouchMode(true);
VideoView
- 部分机型播放mp4文件失败(如三星S8)
解决方案:根据不同机型文件需要转码:
转码
ffmpeg -i 1.mov -r 25 -ac 1 -profile:v baseline -s 526x936 out.mp4
查询文件信息
ffprobe -v quiet -print_format json -show_format -show_streams #{android.amr}
自定义View
- onLayout()时获取的子View宽高均为0
解决方案:在onMeasure中添加measureChildren(widthMeasureSpec,heightMeasureSpec);
Fragment相关
- 报错原因为父Fragment的mView为空,而mView在onCreateView中返回,所以DialogFragment中的相关初始化不能在onCreateDialog中,需在onCreateView中执行。
错误信息:Fragment does not have a view
- FragmentPagerAdapter在Fragment中创建对象时要调用getChildFragmentManager(),在Activity中创建时要调用getSupportFragmentManager()
错误信息:FragmentPagerAdapter中的Fragment可创建但不显示
Broadcast相关
- 隐式注册的广播在Android 8.0以上不能再使用,推荐使用动态注册。或者为Intent添加setComponent或setPackage。
Beginning with Android 8.0 (API level 26), the system imposes additional restrictions on manifest-declared receivers. If your app targets API level 26 or higher, you cannot use the manifest to declare a receiver for most implicit broadcasts (broadcasts that do not target your app specifically).
Layout相关
ConstraintLayout
- 使用TextView并限制singleLine和宽度后,文字并不会被限制宽度并显示省略号。
解决方案:添加app:layout_constraintedWidth="true"属性
其他
- Bundle所存数据超过1MB导致:targetSdkVersion在23及以下时为警告,否则直接crash。使用Toolargetool可以很快定位到问题。我的问题是在Fragment中调用onSaveInstanceState存储数据,当使用ViewPager切换时数据存储过多。
错误信息:java.lang.RuntimeException: android.os.TransactionTooLargeException: data parcel size xxxxxx bytes
Toolargetool最新版本使用androidx,如果自身项目用的不是androidx会报错,建议使用历史版本
这篇博客汇总了Android开发中遇到的各种问题,包括View相关(SurfaceView、TextView、VideoView和自定义View)的问题及其解决方案,Fragment的注意事项,Broadcast的适配Android 8.0以上系统的方法,以及ConstraintLayout的使用技巧。还提到了Bundle大小限制可能导致的运行时异常以及如何处理。

460

被折叠的 条评论
为什么被折叠?



