处理 空指针异常

CommonAppContext.getTopActivity()==null
主要处理了 ToastUtil,OpenAdManager
This commit is contained in:
hch 2023-10-26 14:50:17 +08:00
parent b191dfb1be
commit adcafc7e16
4 changed files with 55 additions and 44 deletions

View File

@ -56,9 +56,7 @@ public class CommonAppContext extends MultiDexApplication {
//设置新加坡融云服务器 有时候国内收不到推送是因为这个 //设置新加坡融云服务器 有时候国内收不到推送是因为这个
RongIMClient.setServerInfo("navsg01.cn.ronghub.com", null); RongIMClient.setServerInfo("navsg01.cn.ronghub.com", null);
//谷歌推送 //谷歌推送
PushConfig gconfig = new PushConfig.Builder() PushConfig gconfig = new PushConfig.Builder().enableFCM(true).build();
.enableFCM(true)
.build();
RongPushClient.setPushConfig(gconfig); RongPushClient.setPushConfig(gconfig);
//初始化友盟统计 //初始化友盟统计
// UMConfigure.init(this, UMConfigure.DEVICE_TYPE_PHONE, null); // UMConfigure.init(this, UMConfigure.DEVICE_TYPE_PHONE, null);
@ -86,6 +84,9 @@ public class CommonAppContext extends MultiDexApplication {
} }
public static Activity getTopActivity() { public static Activity getTopActivity() {
if (activityWeakReference == null) {
return null;
}
return activityWeakReference.get(); return activityWeakReference.get();
} }

View File

@ -262,7 +262,7 @@ public class CommonHttpUtil {
} }
}).build().show(); }).build().show();
} else { } else {
ToastUtil.show(CommonAppContext.getTopActivity().getString(R.string.net_error) + "code:" + code + " msg:" + msg); ToastUtil.show(context.getString(R.string.net_error) + "code:" + code + " msg:" + msg);
} }
} }

View File

@ -1,5 +1,6 @@
package com.yunbao.common.manager; package com.yunbao.common.manager;
import android.content.Context;
import android.os.Handler; import android.os.Handler;
import android.os.Looper; import android.os.Looper;
import android.util.Log; import android.util.Log;
@ -52,11 +53,14 @@ public class OpenAdManager {
if (list != null && list.isEmpty()) { if (list != null && list.isEmpty()) {
return; return;
} }
if(CommonAppContext.getTopActivity()==null){ Context context = CommonAppContext.getTopActivity();
if (context == null) {
context = CommonAppContext.sInstance;
}
if (context == null) {
return; return;
} }
LiveNetManager.get(CommonAppContext.getTopActivity()) LiveNetManager.get(context).activityPopup(new HttpCallback<List<OpenAdModel>>() {
.activityPopup(new HttpCallback<List<OpenAdModel>>() {
@Override @Override
public void onSuccess(List<OpenAdModel> data) { public void onSuccess(List<OpenAdModel> data) {
if (data == null || data.isEmpty()) { if (data == null || data.isEmpty()) {
@ -163,15 +167,11 @@ public class OpenAdManager {
} }
showMap.put(model.getId(), true); showMap.put(model.getId(), true);
if (model.getModel() == OpenAdModel.MODEL_BOTTOM) { if (model.getModel() == OpenAdModel.MODEL_BOTTOM) {
new OpenAdBottomDialogPopup(CommonAppContext.getTopActivity(), model) new OpenAdBottomDialogPopup(CommonAppContext.getTopActivity(), model).setListener((bean, position) -> {
.setListener((bean, position) -> { }).showDialog();
})
.showDialog();
} else { } else {
new OpenAdCenterDialogPopup(CommonAppContext.getTopActivity(), model) new OpenAdCenterDialogPopup(CommonAppContext.getTopActivity(), model).setListener((bean, position) -> {
.setListener((bean, position) -> { }).showDialog();
})
.showDialog();
} }
} }
} }

View File

@ -64,7 +64,15 @@ public class ToastUtil {
* @param delayMillis 显示时间 毫秒 * @param delayMillis 显示时间 毫秒
*/ */
public static void showStyle(String content, long delayMillis) { public static void showStyle(String content, long delayMillis) {
Dialog dialog = new Dialog(CommonAppContext.getTopActivity(), R.style.dialog); Context context = CommonAppContext.getTopActivity();
if (context == null) {
context = CommonAppContext.sInstance;
}
if (context == null) {
return;
}
Dialog dialog = new Dialog(context, R.style.dialog);
dialog.setContentView(R.layout.dialog_toast); dialog.setContentView(R.layout.dialog_toast);
dialog.setCancelable(false); dialog.setCancelable(false);
dialog.setCanceledOnTouchOutside(false); dialog.setCanceledOnTouchOutside(false);
@ -79,14 +87,16 @@ public class ToastUtil {
dialog.show(); dialog.show();
new Handler(Looper.getMainLooper()).postDelayed(dialog::dismiss, delayMillis); new Handler(Looper.getMainLooper()).postDelayed(dialog::dismiss, delayMillis);
} }
public static void showDebug(String s){
if(BuildConfig.DEBUG){ public static void showDebug(String s) {
show("开发模式:"+s); if (BuildConfig.DEBUG) {
show("开发模式:" + s);
} }
} }
public static void showDebug(int s){
if(BuildConfig.DEBUG){ public static void showDebug(int s) {
show("开发模式:"+s); if (BuildConfig.DEBUG) {
show("开发模式:" + s);
} }
} }
@ -94,10 +104,10 @@ public class ToastUtil {
if (TextUtils.isEmpty(s)) { if (TextUtils.isEmpty(s)) {
return; return;
} }
Log.i("Toast", "Toast show: "+s); Log.i("Toast", "Toast show: " + s);
Log.i("ts","ll"); Log.i("ts", "ll");
Toast.makeText(CommonAppContext.sInstance,s,Toast.LENGTH_SHORT).show(); Toast.makeText(CommonAppContext.sInstance, s, Toast.LENGTH_SHORT).show();
Log.i("ts","22"); Log.i("ts", "22");
// long curTime = System.currentTimeMillis(); // long curTime = System.currentTimeMillis();
// if (curTime - sLastTime > 2000) { // if (curTime - sLastTime > 2000) {