其他分享
首页 > 其他分享> > Android提示"很抱歉,xxx已停止运行":Application中onCreate执行多次

Android提示"很抱歉,xxx已停止运行":Application中onCreate执行多次

作者:互联网

问题

游戏发布了vivo版本的包,发现一个问题,运行游戏后,过一会儿会弹出一个“停止运行”的弹框
在这里插入图片描述

定位问题

通过ADB调试,发现了异常的日志:

如何使用ADB调试模拟器,参加我这篇文章:https://linxinfa.blog.csdn.net/article/details/104578095

--------- beginning of crash
03-09 16:14:04.063 E/AndroidRuntime(12365): FATAL EXCEPTION: main
03-09 16:14:04.063 E/AndroidRuntime(12365): Process: com.linxinfa.mygame.vivo:xg_service_v3, PID: 12365
03-09 16:14:04.063 E/AndroidRuntime(12365): java.lang.RuntimeException: Unable to create application com.linxinfa.mygame.vivo.AppApplication: java.lang.RuntimeException: Must call initSdk first
03-09 16:14:04.063 E/AndroidRuntime(12365): 	at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4556)
03-09 16:14:04.063 E/AndroidRuntime(12365): 	at android.app.ActivityThread.access$1500(ActivityThread.java:151)
03-09 16:14:04.063 E/AndroidRuntime(12365): 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
...

从日志看,是信鸽开启了一个进程com.linxinfa.mygame.vivo:xg_service_v3,这个进程执行了AppApplicationonCreate
而在AppApplication中,执行了vivoinitSdk接口,于是这个信鸽进程就崩溃了,弹出了停止运行的提示框。

解决办法

通过processName来判断是否是信鸽的进程,如果是信鸽的进程,则不执行vivo的initSdk接口

package com.linxinfa.mygame.vivo;

import android.app.Application;
import android.util.Log;
import com.vivo.unionsdk.open.VivoUnionSDK;
import com.vivo.unionsdk.open.OrderResultEventHandler;
import com.vivo.unionsdk.open.OrderResultInfo;

import java.util.List;
import android.content.Context;
import android.app.ActivityManager;

public class AppApplication extends Application
{
	static final String tag = "linxinfa";
	static final String appId = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
	
	@Override
	public void onCreate()
	{
		super.onCreate();

        String processName = getProcessName(this);
		Log.d(tag, "AppApplication.OnCreate(): called, processName: " + processName);
		
        // TODO 根据processName判断是否为主线程,然后执行SDK初始化
		if(processName.indexOf("xg_service_v3") == -1)
		{
			//主线程中调用,信鸽子线程不调用,否则会导致sdk重复初始化而报错,表现为加载结束时提示应用程序已停止运行
			VivoUnionSDK.initSdk(this, appId, false);
			//...
		
		}
	}
    
    private String getProcessName(Context context) {
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> runningApps = am.getRunningAppProcesses();
        if (runningApps == null) {
            return null;
        }
        for (ActivityManager.RunningAppProcessInfo proInfo : runningApps) {
            if (proInfo.pid == android.os.Process.myPid()) {
                if (proInfo.processName != null) {
                    return proInfo.processName;
                }
            }
        }
        return null;
    }
}

标签:12365,processName,xxx,vivo,Application,import,Android,com,android
来源: https://blog.csdn.net/linxinfa/article/details/104755742