处理 空指针异常

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);
//谷歌推送
PushConfig gconfig = new PushConfig.Builder()
.enableFCM(true)
.build();
PushConfig gconfig = new PushConfig.Builder().enableFCM(true).build();
RongPushClient.setPushConfig(gconfig);
//初始化友盟统计
// UMConfigure.init(this, UMConfigure.DEVICE_TYPE_PHONE, null);
@ -86,6 +84,9 @@ public class CommonAppContext extends MultiDexApplication {
}
public static Activity getTopActivity() {
if (activityWeakReference == null) {
return null;
}
return activityWeakReference.get();
}

View File

@ -262,7 +262,7 @@ public class CommonHttpUtil {
}
}).build().show();
} 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;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
@ -52,29 +53,32 @@ public class OpenAdManager {
if (list != null && list.isEmpty()) {
return;
}
if(CommonAppContext.getTopActivity()==null){
Context context = CommonAppContext.getTopActivity();
if (context == null) {
context = CommonAppContext.sInstance;
}
if (context == null) {
return;
}
LiveNetManager.get(CommonAppContext.getTopActivity())
.activityPopup(new HttpCallback<List<OpenAdModel>>() {
@Override
public void onSuccess(List<OpenAdModel> data) {
if (data == null || data.isEmpty()) {
Log.i(TAG, "onSuccess: 没有数据");
list = new ArrayList<>();
return;
}
list = data;
if (isShow) {
show(TYPE_HOME, false);
}
}
LiveNetManager.get(context).activityPopup(new HttpCallback<List<OpenAdModel>>() {
@Override
public void onSuccess(List<OpenAdModel> data) {
if (data == null || data.isEmpty()) {
Log.i(TAG, "onSuccess: 没有数据");
list = new ArrayList<>();
return;
}
list = data;
if (isShow) {
show(TYPE_HOME, false);
}
}
@Override
public void onError(String error) {
System.err.println("弹框列表:" + error);
}
});
@Override
public void onError(String error) {
System.err.println("弹框列表:" + error);
}
});
}
@ -163,15 +167,11 @@ public class OpenAdManager {
}
showMap.put(model.getId(), true);
if (model.getModel() == OpenAdModel.MODEL_BOTTOM) {
new OpenAdBottomDialogPopup(CommonAppContext.getTopActivity(), model)
.setListener((bean, position) -> {
})
.showDialog();
new OpenAdBottomDialogPopup(CommonAppContext.getTopActivity(), model).setListener((bean, position) -> {
}).showDialog();
} else {
new OpenAdCenterDialogPopup(CommonAppContext.getTopActivity(), model)
.setListener((bean, position) -> {
})
.showDialog();
new OpenAdCenterDialogPopup(CommonAppContext.getTopActivity(), model).setListener((bean, position) -> {
}).showDialog();
}
}
}

View File

@ -60,11 +60,19 @@ public class ToastUtil {
/**
* 显示黑底白字的提示
*
* @param content 内容
* @param content 内容
* @param 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.setCancelable(false);
dialog.setCanceledOnTouchOutside(false);
@ -79,14 +87,16 @@ public class ToastUtil {
dialog.show();
new Handler(Looper.getMainLooper()).postDelayed(dialog::dismiss, delayMillis);
}
public static void showDebug(String s){
if(BuildConfig.DEBUG){
show("开发模式:"+s);
public static void showDebug(String s) {
if (BuildConfig.DEBUG) {
show("开发模式:" + s);
}
}
public static void showDebug(int s){
if(BuildConfig.DEBUG){
show("开发模式:"+s);
public static void showDebug(int s) {
if (BuildConfig.DEBUG) {
show("开发模式:" + s);
}
}
@ -94,10 +104,10 @@ public class ToastUtil {
if (TextUtils.isEmpty(s)) {
return;
}
Log.i("Toast", "Toast show: "+s);
Log.i("ts","ll");
Toast.makeText(CommonAppContext.sInstance,s,Toast.LENGTH_SHORT).show();
Log.i("ts","22");
Log.i("Toast", "Toast show: " + s);
Log.i("ts", "ll");
Toast.makeText(CommonAppContext.sInstance, s, Toast.LENGTH_SHORT).show();
Log.i("ts", "22");
// long curTime = System.currentTimeMillis();
// if (curTime - sLastTime > 2000) {