简体   繁体   English

触摸插页式广告时,应用程序“不幸停止”

[英]app “unfortunately stopped” when touching interstitial ads

I'm using cocos2d-x 3.2 and I want to show interstitial ad when user touches the screen. 我正在使用cocos2d-x 3.2,并且希望在用户触摸屏幕时展示插页式广告。 but when I touch the screen I got "Unfortunately Stopped" message. 但是当我触摸屏幕时,收到“不幸停止”消息。 here is my code: 这是我的代码:

Java: Java的:

       @Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);

final String ADMOB_ID = "ca-app-pub-0000000000000000/0000000000";
interstitial = new InterstitialAd(this);
interstitial.setAdUnitId(ADMOB_ID);

AdRequest.Builder builder = new AdRequest.Builder();
AdRequest adRequest = new AdRequest.Builder().addTestDevice("3A34952B128D8DC19CCB75CA752ED31B").build();

interstitial.loadAd(adRequest);
}

public void showad() { 
    interstitial.setAdListener(new AdListener() {
        @Override
        public void onAdLoaded() {
            interstitial.show();
        }
    });
    }
;

C++: C ++:

bool HelloWorld::onTouchBegan(cocos2d::Touch *touch , cocos2d::Event *event){

    show();
}

void HelloWorld::show(){
       cocos2d::JniMethodInfo methodInfo;

        if (! cocos2d::JniHelper::getMethodInfo(methodInfo, "org/cocos2dx/cpp/AppActivity", "showad", "()V")) {
            return;
        }

        methodInfo.env->CallVoidMethod(methodInfo.classID, methodInfo.methodID);
        methodInfo.env->DeleteLocalRef(methodInfo.classID);
}

and i've got this error in logcat: 我在logcat中遇到此错误:

A/libc(6628): Fatal signal 11 (SIGSEGV) at 0x00020004 (code=1), thread 6643 (Thread-553)

can someone help?(sorry for english :) ) 有人可以帮忙吗(对不起英语:))

You have to first load the ad, and then show it: 您必须先加载广告,然后展示它:

private boolean adIsLoaded = false;

protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);

    final String ADMOB_ID = "ca-app-pub-0000000000000000/0000000000";
    interstitial = new InterstitialAd(this);
    interstitial.setAdUnitId(ADMOB_ID);

    AdRequest.Builder builder = new AdRequest.Builder();
    AdRequest adRequest = new AdRequest.Builder().addTestDevice("3A34952B128D8DC19CCB75CA752ED31B").build();
    interstitial.setAdListener(new AdListener() {
        @Override
        public void onAdLoaded() {
            adIsLoaded=true;
        }
    });

    interstitial.loadAd(adRequest);

}

public void showad() { 
    if (adIsLoaded)
            interstitial.show();
    }
}

Since it's a SIGSEGV crash, clearly there's nothing to do with your java code, problem appeared on C++ side. 由于这是SIGSEGV崩溃,因此显然与Java代码无关,问题出现在C ++方面。

Check HelloWorld::show() , print log between each line to find out which line caused the crash. 检查HelloWorld::show() ,在每行之间打印日志,以找出导致崩溃的行。 It sounds stupid but effective. 听起来很愚蠢,但却很有效。

BTW, you have to analyze whole log nears the crash point instead of the line when it crashed. 顺便说一句,您必须分析接近崩溃点的整个日志,而不是分析崩溃时的直线。

Try using getStaticMethodInfo() and CallStaticVoidMethod() instead. 尝试改用getStaticMethodInfo()和CallStaticVoidMethod()。 Also make showad() a static function. 同时将showad()设为静态函数。

It works pretty good for me. 对我来说效果很好。 But when I use getMethodInfo() and CallVoidMethod() along with non-static java function, neither the code works nor does it crash. 但是,当我将getMethodInfo()和CallVoidMethod()以及非静态Java函数一起使用时,代码均无效,也不会崩溃。 It simply does not do anything. 它根本不做任何事情。

Haven't figured out the reason yet! 还没有找到原因!

I fixed it in this way : 我以这种方式修复它:

    private static AppActivity _appActiviy;
       @Override
    protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);

    final String ADMOB_ID = "ca-app-pub-0000000000000000/0000000000";
    interstitial = new InterstitialAd(this);
    interstitial.setAdUnitId(ADMOB_ID);

    AdRequest.Builder builder = new AdRequest.Builder();
    AdRequest adRequest = new AdRequest.Builder().addTestDevice("3A34952B128D8DC19CCB75CA752ED31B").build();

    interstitial.loadAd(adRequest);
    }

    public static void showAd(){
    _appActiviy.runOnUiThread(new Runnable()
    {

    @Override
     public void run(){ 
         if (_appActiviy.interstitial.isLoaded())
            {_appActiviy.interstitial.show();}

};
});}

c++ C ++

bool HelloWorld::onTouchBegan(cocos2d::Touch *touch , cocos2d::Event *event){

    show();
}

void HelloWorld::show(){
       cocos2d::JniMethodInfo methodInfo;

        if (! cocos2d::JniHelper::getStaticMethodInfo(methodInfo, "org/cocos2dx/cpp/AppActivity", "showAd", "()V")) {
            return;
        }

        methodInfo.env->CallStaticVoidMethod(methodInfo.classID, methodInfo.methodID);
        methodInfo.env->DeleteLocalRef(methodInfo.classID);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM