bugly的報錯
java.lang.NullPointerException
Attempt to invoke virtual method 'int android.widget.Editor
on a null object reference android.widget.Editor.touchPositionIsInSelection(Editor.java:955)
android.widget.Editor.touchPositionIsInSelection(Editor.java:901)
android.widget.Editor.performLongClick(Editor.java:1002)
android.widget.TextView.performLongClick(TextView.java:9261)
android.view.View$CheckForLongPress.run(View.java:21151)
android.os.Handler.handleCallback(Handler.java:742)
android.os.Handler.dispatchMessage(Handler.java:95)
android.os.Looper.loop(Looper.java:154)
android.app.ActivityThread.main(ActivityThread.java:5523)
java.lang.reflect.Method.invoke(Native Method)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629)
主要出現(xiàn)在小米的機(jī)型
問題原因:
view.setMovementMethod(LinkMovementMethod.getInstance());
一般用于 點(diǎn)擊 textview中的 可跳轉(zhuǎn)鏈接的時候使用
但是長按這個View的時候 就會報這個異常篡帕。
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(FOCUSABLE);
setClickable(true);
setLongClickable(true);
} else {
setFocusable(FOCUSABLE_AUTO);
setClickable(false);
setLongClickable(false);
}
}
可以看到,
會調(diào)用到setLongClickable(true);方法,
此時長按就會出現(xiàn)如上log所示的崩潰,
而解決方案就一目了然了,
有兩種,都是屏蔽長按事件罷了:
調(diào)用 TextView.setLongClickable(false); 禁用長按事件
android:longClickable="false"
消費(fèi)掉長按事件即可;
TextView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
return true;
}
});
不過我看到Android-26的sdk已經(jīng)修復(fù)了這個問題了广料,有加空判斷
android-26 sdk 已處理