2015-02-05 100 views
0

我正在使用cocos2d-x 3.2,并且我想在用户触摸屏幕时显示插页式广告。 但是当我触摸屏幕时,我收到了“不幸停止”的消息。 这里是我的代码:应用程序“不幸停止”触摸插页式广告时

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

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) 

谁能帮(对不起英语:))

回答

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

我的应用程序仍然崩溃。 – Arashx12 2015-02-06 08:32:56

0

因为它是一个SIGSEGV崩溃,显然有什么可以做你的Java代码,问题出现在C++的一面。

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

顺便说一句,你必须分析整个日志临近碰撞点,而不是坠毁时的路线。

0

尝试使用getStaticMethodInfo()和CallStaticVoidMethod()来代替。也让showad()成为一个静态函数。

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

还没有弄清楚原因呢!

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); 
} 
相关问题