应用"不幸停止"触摸插页式广告时

时间:2015-02-05 15:31:38

标签: java android c++ admob cocos2d-x

我正在使用cocos2d-x 3.2,我希望在用户触摸屏幕时显示插页式广告。 但是当我触摸屏幕时,我得到了#34;不幸的是停止了#34;信息。 这是我的代码:

爪哇:

       @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 ++:

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);
}

我在logcat中遇到了这个错误:

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

有人可以帮忙吗?(抱歉英语:))

4 个答案:

答案 0 :(得分:0)

您必须先加载广告,然后再显示:

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();
    }
}

答案 1 :(得分:0)

由于它是SIGSEGV崩溃,显然与你的java代码无关,C ++方面出现问题。

检查HelloWorld::show(),在每一行之间打印日志以找出导致崩溃的行。这听起来很愚蠢但很有效。

顺便说一句,你必须分析整个日志,然后在崩溃时接近崩溃点。

答案 2 :(得分:0)

尝试使用getStaticMethodInfo()和CallStaticVoidMethod()。还要使showad()成为静态函数。

对我来说非常好。但是当我使用getMethodInfo()和CallVoidMethod()以及非静态java函数时,代码既不起作用也不崩溃。它根本不做任何事情。

尚未找出原因!

答案 3 :(得分:0)

我用这种方式解决了它:

    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 ++

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);
}