Android一些小异常

往github中上传本地项目

adb logcat获取/过滤日志信息

7、关于 io.reactivex.exceptions.UndeliverableException

public final class RxJavaPlugins {
	...
	public static void onError(@NonNull Throwable error) {
        Consumer<? super Throwable> f = errorHandler;

        if (error == null) {
            error = new NullPointerException("onError called with null. Null values are generally not allowed in 2.x operators and sources.");
        } else {
            if (!isBug(error)) {//除了少数几个异常,其他的都包装成UndeliverableException
                error = new UndeliverableException(error);
            }
        }
		//如果 errorHandler 不为null;所以需要设置个errorHandler,才会走这里的逻辑,避免抛出异常导致crash
        if (f != null) {
            try {
                f.accept(error);
                return;//这里return掉之后,异常不会再抛出了;
            } catch (Throwable e) {
                // Exceptions.throwIfFatal(e); TODO decide
                e.printStackTrace(); // NOPMD
                uncaught(e);
            }
        }
        error.printStackTrace(); // NOPMD
        uncaught(error);//抛出异常
    }
	...
}

在初始化时设置错误处理器:

RxJavaPlugins.setErrorHandler(new Consumer<Throwable>() {
    @Override
    public void accept(Throwable throwable) throws Exception {
        //只处理掉 UndeliverableException,其他bug导致的异常还是正常抛出
        if (throwable != null && throwable instanceof UndeliverableException) {
            return;
        }
        //其他异常还是要抛出
        throw new RuntimeException(throwable);
    }
});

设置一个errorHandler,内部什么都不做,这样避免抛出异常导致crash;也可以自定义errorHandler,做一些错误提示;

6、跨进程通信中,比如在子进程的Service里做以下操作,会导致部分机型抛 android.app.RemoteServiceException 异常

Notification.Builder notification = new Notification.Builder(context);
// 随便给一个icon,反正不会显示,只是假装自己是合法的Notification而已
notification.setSmallIcon(0);
    这个setSmallIcon(0),可以给一张1px的图片来代替,避免该bug

5、popwindow在5.0以下不显示的问题

5.0之前必须设置宽高,在6.0版本以上直接使用setcontentview即可

另外一种解决办法是在xml布局中添加background图片

加上这些,避免5.0以下INPUT_TYPE时,pop不关闭的问题

setOutsideTouchable(true);
setFocusable(false);
setBackgroundDrawable(new BitmapDrawable());
popupWindow.setContentView(listView);
popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);

4、小米手机找不到该属性android.R.attr.textViewStyle

Caused by: java.lang.NullPointerException:
  at miui.util.AttributeResolver.p (AttributeResolver.java:35)
  at miui.util.AttributeResolver.resolveInt (AttributeResolver.java:145)
  at miui.util.AttributeResolver.isUsingMiuiTheme (AttributeResolver.java:179)
  at miui.os.Environment.isUsingMiui (Environment.java:108)
  at android.view.ViewConfigurationInjector.get (ViewConfigurationInjector.java:18)
  at android.view.ViewConfiguration.get (ViewConfiguration.java:369)
  at android.view.View.<init> (View.java:3837)
  at android.view.View.<init> (View.java:3953)

  at android.widget.TextView.<init> (TextView.java:700)

3、object.wait(long timeout)方法需要慎用
A线程:object.wait(1000);
B线程:阻塞2000ms

那么A线程回调的时间不是1000ms后,而是B线程释放对象之后(2000ms)

2、RecyclerView内存泄漏问题

1、5.0/5.1系统【待验证】
android.app.RemoteServiceException:
at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1448)
at android.os.Handler.dispatchMessage (Handler.java:102)
at android.os.Looper.loop (Looper.java:135)
at android.app.ActivityThread.main (ActivityThread.java:5289)
at java.lang.reflect.Method.invoke (Method.java)
at java.lang.reflect.Method.invoke (Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:694)
经过排查估计是自定义通知栏版本兼容问题setSmallIcon(int),必须要传图片资源
参考资料: StackOverFlow

猜你喜欢

转载自blog.csdn.net/u010577768/article/details/79346562