好久不更新,一直忙于直播APP的实现,直播真的是百家争鸣,百花齐放,做直播的互联网公司太多太多。。。另外,微信小程序的内测也像一颗重磅炸弹,在互联网领域激起千层浪,无奈O(∩_∩)O~
本文主要用于收集Android开发中一些也许并未注意,却会导致崩溃等问题以及一些很实用的开发小技巧的集锦,会持续更新。
(一)错误篇
1. ClickSpan导致的长按崩溃(关于TextView中使用ClickSpan后, 长按该TextView会导致崩溃)
示例代码如下
TextView content = (TextView)findViewById(R.id.tv_content);
content.setTextColor(content.getResources().getColor(R.color.util_lib_white));
String colorStr = item.getUserInfo().getNickname();
SpannableString colorString = StringUtil.colorString(colorStr + " : ",
content.getResources().getColor(R.color.util_lib_color_green_7bde4e));
colorString.setSpan(new ClickableSpan() {
@Override
public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
ds.setUnderlineText(false);
}
@Override
public void onClick(View view) {
LogUtil.e();
if (null != mChatUserClickListener &&
!TextUtils.equals(item.getUserInfo().getUuid(), UserConfig.getUid())){
mChatUserClickListener.onChatUserClick(item.getUserInfo().getUuid());
}
}
}, 0, colorString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
content.setMovementMethod(LinkMovementMethod.getInstance());
content.setText(colorString);
content.append(item.getText());
此代码, 在对TextView 进行长按操作的时候, 会导致崩溃, 崩溃的log如下:
java.lang.NullPointerException
Attempt to invoke virtual method 'int android.widget.Editor$SelectionModifierCursorController.getMinTouchOffset()' on a null object reference
android.widget.Editor.touchPositionIsInSelection(Editor.java:832)
android.widget.Editor.performLongClickMz(Editor.java:5268)
android.widget.TextView.performLongClick(TextView.java:9126)
android.view.View$CheckForLongPress.run(View.java:20357)
android.os.Handler.handleCallback(Handler.java:815)
android.os.Handler.dispatchMessage(Handler.java:104)
android.os.Looper.loop(Looper.java:194)
android.app.ActivityThread.main(ActivityThread.java:5803)
java.lang.reflect.Method.invoke(Native Method)
java.lang.reflect.Method.invoke(Method.java:372)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1009)
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:804)
之所以出现此崩溃, 是由于调用了
content.setMovementMethod(LinkMovementMethod.getInstance());
此方法内部实现如下:
public final void setMovementMethod(MovementMethod movement) {
if (mMovement != movement) {
mMovement = movement;
if (movement != null && !(mText instanceof Spannable)) {
setText(mText);
}
fixFocusableAndClickableSettings();
// SelectionModifierCursorController depends on textCanBeSelected, which depends on
// mMovement
if (mEditor != null) mEditor.prepareCursorControllers();
}
}
private void fixFocusableAndClickableSettings() {
if (mMovement != null || (mEditor != null && mEditor.mKeyListener != null)) {
setFocusable(true);
setClickable(true);
setLongClickable(true);
} else {
setFocusable(false);
setClickable(false);
setLongClickable(false);
}
}
可以看到, 会调用到setLongClickable(true);方法, 此时长按就会出现如上log所示的崩溃,而解决方案就一目了然了,有两种,都是屏蔽长按事件罢了:
(1) 调用 TextView.setLongClickable(false); 禁用长按事件
(2) 调用 TextView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
return true;
}
}); 消费掉长按事件即可;
2. Android Studio调试真机, App自动重启, 无法在控制台捕获log的问题
这是由于AS会在崩溃时自动重启当前应用, 并情况logcat导致的, 目前的解决方案大概是:
切换 控制台右上角的 Show only selected application 为 No Filters即可保留崩溃前的log【截图抽空候补】;
//todo: 添加更多...
=========华丽的分割线=========
(二)技巧篇
1. Api 23后快速开启HttpClient
在module的build.gradle文件中进行如下配置:
android{
useLibrary 'org.apache.http.legacy'
}
2. RecycleView的item点击的selector(5.0以前)或波纹效果(5.0及之后)
在item的View加上这一条属性:
android:foreground="?android:attr/selectableItemBackground"
3. 引用aar依赖包
(1) module的build.gradle中添加[与android{} 平级]:
repositories{
flatDir{
dirs "libs"
}
}
(2) 在dependecies中进行引用:
compile(name:'aarmodulename', ext:'aar')
4. 字符串[CharSequence]判等
TextUtils.equals(stringA, stringB);
看看内部实现和注释:
/**
* Returns true if a and b are equal, including if they are both null.
* <p><i>Note: In platform versions 1.1 and earlier, this method only worked well if
* both the arguments were instances of String.</i></p>
* @param a first CharSequence to check
* @param b second CharSequence to check
* @return true if a and b are equal
*/
public static boolean equals(CharSequence a, CharSequence b) {
if (a == b) return true;
int length;
if (a != null && b != null && (length = a.length()) == b.length()) {
if (a instanceof String && b instanceof String) {
return a.equals(b);
} else {
for (int i = 0; i < length; i++) {
if (a.charAt(i) != b.charAt(i)) return false;
}
return true;
}
}
ps: TextUtils是一个不错的系统工具类, 里面有不少实用的方法,建议浏览一遍;
5. String.format()的使用
String.format("%s --- userId : %s --- publishUri : %s", roomInfo.getUuid(), roomInfo.getUserId(), roomInfo.getPublishUri();
主要用来替换:
roomInfo.getUuid() + " --- userId : " + roomInfo.getUserId() + " --- " + "publishUri : " + roomInfo.getPublishUri()
类似这样的字符串拼接的, 当然,也支持除%s外的好多占位符,如%d等;
ps: 详细使用说明, 可自行Google(基本能得到想要的),应该不需要开一篇介绍用法了。
6.
// todo 添加更多...