From 1028e395552798a5bfb5316caf401af2694889ad Mon Sep 17 00:00:00 2001 From: zlzw <583819556@qq.com> Date: Thu, 25 Aug 2022 15:52:20 +0800 Subject: [PATCH] =?UTF-8?q?add:=E6=96=B0=E5=A2=9E=E5=85=A8=E5=B1=80?= =?UTF-8?q?=E5=BC=82=E5=B8=B8=E6=8D=95=E8=8E=B7=E7=B1=BB=EF=BC=8C=E6=9A=82?= =?UTF-8?q?=E6=9C=AA=E5=90=AF=E7=94=A8=EF=BC=8C=E6=9C=89=E8=B0=83=E8=AF=95?= =?UTF-8?q?=E9=9C=80=E8=A6=81=E6=97=B6=E5=8F=AF=E4=BB=A5=E5=9C=A8AppContex?= =?UTF-8?q?t=E8=B0=83=E7=94=A8=20fix:=E4=BF=AE=E5=A4=8D=E5=91=A8=E6=A6=9C?= =?UTF-8?q?=E3=80=81=E8=A7=82=E4=BC=97=E6=9D=A5=E5=9B=9E=E5=88=87=E6=8D=A2?= =?UTF-8?q?=E5=9C=A8=E7=BD=91=E7=BB=9C=E6=85=A2=E7=9A=84=E6=83=85=E5=86=B5?= =?UTF-8?q?=E4=B8=8B=E4=BC=9A=E9=97=AA=E9=80=80=E7=9A=84=E9=97=AE=E9=A2=98?= =?UTF-8?q?=20fix:=E4=BC=98=E5=8C=96=E8=A7=82=E4=BC=97=E5=88=97=E8=A1=A8?= =?UTF-8?q?=E5=88=87=E6=8D=A2=E5=86=85=E5=AE=B9=E5=9C=A8=E7=BD=91=E7=BB=9C?= =?UTF-8?q?=E6=85=A2=E7=9A=84=E6=83=85=E5=86=B5=E4=B8=8B=E6=9C=89=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E6=AE=8B=E7=95=99=E7=9A=84=E9=97=AE=E9=A2=98=20update?= =?UTF-8?q?:=E6=A0=B9=E6=8D=AE=E9=9C=80=E6=B1=82=E6=81=A2=E5=A4=8D?= =?UTF-8?q?=E5=9C=A8=E7=BA=BF=E5=AE=88=E6=8A=A4=E5=88=97=E8=A1=A8=E5=88=B0?= =?UTF-8?q?=E8=A7=82=E4=BC=97=E5=88=97=E8=A1=A8=E4=B8=AD=20update:?= =?UTF-8?q?=E6=A0=B9=E6=8D=AE=E9=9C=80=E6=B1=82=E7=82=B9=E5=87=BB=E5=AE=88?= =?UTF-8?q?=E6=8A=A4icon=E7=9B=B4=E6=8E=A5=E5=88=B0=E5=AE=88=E6=8A=A4?= =?UTF-8?q?=E5=88=97=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/shayu/phonelive/AppContext.java | 19 +++ .../com/shayu/phonelive/NeverCrashUtils.java | 110 ++++++++++++++++++ .../dialog/LiveUserMoreDialogFragment.java | 6 + 3 files changed, 135 insertions(+) create mode 100644 app/src/main/java/com/shayu/phonelive/NeverCrashUtils.java diff --git a/app/src/main/java/com/shayu/phonelive/AppContext.java b/app/src/main/java/com/shayu/phonelive/AppContext.java index d08d84b24..9e8a813e1 100644 --- a/app/src/main/java/com/shayu/phonelive/AppContext.java +++ b/app/src/main/java/com/shayu/phonelive/AppContext.java @@ -101,6 +101,8 @@ public class AppContext extends CommonAppContext { @Override public void onCreate() { super.onCreate(); + //注册全局异常捕获 + //registerError(); sInstance = this; L.setDeBug(BuildConfig.DEBUG); AppEventsLogger.activateApp(this); @@ -238,4 +240,21 @@ public class AppContext extends CommonAppContext { RongcloudIMManager.removeRongcloudIMOnReceiveMessageListener(); super.onTerminate(); } + + /** + * 注册全局异常捕获,有需要时可以在onCreate调用 + */ + private void registerError(){ + NeverCrashUtils.getInstance() + .setDebugMode(BuildConfig.DEBUG) + .setMainCrashHandler((t, e) -> { + Log.e("ApplicationError", "主线程异常");//此处log只是展示,当debug为true时,主类内部log会打印异常信息 + e.printStackTrace(); + }) + .setUncaughtCrashHandler((t, e) -> { + Log.e("ApplicationError", "子线程异常");//此处log只是展示,当debug为true时,主类内部log会打印异常信息 + e.printStackTrace(); + }) + .register(this); + } } diff --git a/app/src/main/java/com/shayu/phonelive/NeverCrashUtils.java b/app/src/main/java/com/shayu/phonelive/NeverCrashUtils.java new file mode 100644 index 000000000..bf9b557c4 --- /dev/null +++ b/app/src/main/java/com/shayu/phonelive/NeverCrashUtils.java @@ -0,0 +1,110 @@ +package com.shayu.phonelive; +import android.app.Application; +import android.os.Handler; +import android.os.Looper; +import android.util.Log; + +/** + * @ClassName NeverCrashUtils + * @Description 全局捕获异常 + */ +public class NeverCrashUtils { + + private final static String TAG = NeverCrashUtils.class.getSimpleName(); + private final static NeverCrashUtils INSTANCE = new NeverCrashUtils(); + + private boolean debugMode; + private MainCrashHandler mainCrashHandler; + private UncaughtCrashHandler uncaughtCrashHandler; + + private NeverCrashUtils() { + } + + public static NeverCrashUtils getInstance() { + return INSTANCE; + } + + private synchronized MainCrashHandler getMainCrashHandler() { + if (null == mainCrashHandler) { + mainCrashHandler = (t, e) -> { + }; + } + return mainCrashHandler; + } + + /** + * 主线程发生异常时的回调,可用于打印日志文件 + *
+ * 注意跨线程操作的可能 + */ + public NeverCrashUtils setMainCrashHandler(MainCrashHandler mainCrashHandler) { + mainCrashHandler = mainCrashHandler; + return this; + } + + private synchronized UncaughtCrashHandler getUncaughtCrashHandler() { + if (null == uncaughtCrashHandler) { + uncaughtCrashHandler = (t, e) -> { + }; + } + return uncaughtCrashHandler; + } + + /** + * 子线程发生异常时的回调,可用于打印日志文件 + *
+ * 注意跨线程操作的可能 + */ + public NeverCrashUtils setUncaughtCrashHandler(UncaughtCrashHandler uncaughtCrashHandler) { + this.uncaughtCrashHandler = uncaughtCrashHandler; + return this; + } + + private boolean isDebugMode() { + return debugMode; + } + + /** + * debug模式,会打印log日志,且toast提醒发生异常,反之则都没有 + */ + public NeverCrashUtils setDebugMode(boolean debugMode) { + this.debugMode = debugMode; + return this; + } + + /** + * 完成监听异常的注册 + * @param application application + */ + public void register(Application application) { + //主线程异常拦截 + new Handler(Looper.getMainLooper()).post(() -> { + while (true) { + try { + Looper.loop(); + } catch (Throwable e) { + if (isDebugMode()) { + Log.e(TAG, "未捕获的主线程异常行为", e); + } + getMainCrashHandler().mainException(Looper.getMainLooper().getThread(), e); + } + } + }); + + //子线程异常拦截 + Thread.setDefaultUncaughtExceptionHandler((t, e) -> { + if (isDebugMode()) { + Log.e(TAG, "未捕获的子线程异常行为", e); + } + getUncaughtCrashHandler().uncaughtException(t, e); + }); + } + + public interface MainCrashHandler { + void mainException(Thread t, Throwable e); + } + + public interface UncaughtCrashHandler { + void uncaughtException(Thread t, Throwable e); + } +} \ No newline at end of file diff --git a/live/src/main/java/com/yunbao/live/dialog/LiveUserMoreDialogFragment.java b/live/src/main/java/com/yunbao/live/dialog/LiveUserMoreDialogFragment.java index eea5cf065..a565bc090 100644 --- a/live/src/main/java/com/yunbao/live/dialog/LiveUserMoreDialogFragment.java +++ b/live/src/main/java/com/yunbao/live/dialog/LiveUserMoreDialogFragment.java @@ -332,8 +332,11 @@ public class LiveUserMoreDialogFragment extends AbsDialogFragment implements Vie } void Up() { + userMoreInfoAdapter.clearData(); bottom_msg.setVisibility(View.VISIBLE); title.setVisibility(View.GONE); + LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) mRefreshView.getLayoutParams(); + params.bottomMargin=DpUtil.dp2px(65); if (Tips.equals("1")) { tags.setText("開通貴族,尊享超多特權!"); btn.setBackgroundResource(R.mipmap.btn_openvip); @@ -361,6 +364,7 @@ public class LiveUserMoreDialogFragment extends AbsDialogFragment implements Vie type = "fans"; no_more.setImageResource(R.mipmap.bixin); }else if(Tips.equals("4")){ + params.bottomMargin=DpUtil.dp2px(0); userMoreInfoAdapter.type = "4"; bottom_msg.setVisibility(View.GONE); type="dayRank"; @@ -368,6 +372,7 @@ public class LiveUserMoreDialogFragment extends AbsDialogFragment implements Vie gz_view.setVisibility(View.GONE); no_more.setImageResource(R.drawable.img_rank_empty); }else if (Tips.equals("5")){ + params.bottomMargin=DpUtil.dp2px(0); userMoreInfoAdapter.type = "5"; bottom_msg.setVisibility(View.GONE); type="weekRank"; @@ -375,6 +380,7 @@ public class LiveUserMoreDialogFragment extends AbsDialogFragment implements Vie no_more.setImageResource(R.drawable.img_rank_empty); setTextColor(weekRank,audience_btn,guard_btn,fans_btn,gz_view,dayRank); } + mRefreshView.setLayoutParams(params); } /**