2014-05-14 8 views
0

첫 번째 앱을 끝내고 몇 가지 광고를 추가하려고했습니다. Google에서 제공하는 자습서를 사용해 보았지만 정상적으로 작동합니다. 이제 응용 프로그램에서 add를 구현하려고하면 오류 메시지가 나타납니다. "IllegalStateException : 기본 UI 스레드에서 isLoaded를 호출해야합니다". 그래서 여기Android의 삽입 광고

내가 내 주요 활동에 무슨 짓을 난에서 onCreate 방법이 : 같은 활동에

public void onCreate(){ 
    // Create the interstitial. 
    interstitial = new InterstitialAd(this); 
    interstitial.setAdUnitId(MY_AD_UNIT_ID); 

    // Create ad request. 
    AdRequest adRequest = new AdRequest.Builder() 
    .addTestDevice(AdRequest.DEVICE_ID_EMULATOR) 
    .addTestDevice("xxxxxxxxxxxxxx") 
    .build(); 

    // Begin loading your interstitial. 
    interstitial.loadAd(adRequest); 
} 

을 나는이 : 그래서

public void showInterstitial() { 
    if (interstitial.isLoaded()) { 
     interstitial.show(); 
     } 
    } 

을 나는 showInterstitial 메소드를 호출 할 때 위에서 설명한 오류가 나타납니다. 저는 몇 시간 동안이 문제에 착석했습니다. 해결책을 찾을 수 없습니다.

답변

0

아래의 코드가 문제를 해결하는 데 도움이 될 것이라고 생각합니다. 기능을

interstitial.setAdListener(new AdListener() { 
    public void onAdLoaded() { 
     showInterstitial(); 
    } 
});  

을 만들 온이 사용하거나 다른 당신은 당신이 장치에서 테스트 할 수있는 다음 코드 &를 사용할 수 있습니다.

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

interstitial = new InterstitialAd(this); 
interstitial.setAdUnitId("***********"); 

AdRequest adRequest = new AdRequest.Builder().build(); 
interstitial.loadAd(adRequest); 
interstitial.setAdListener(new AdListener() { 
public void onAdLoaded() { 
    displayInterstitial(); 
} 
}); 
} 

public void displayInterstitial() { 
if (interstitial.isLoaded()) { 
interstitial.show(); 
} 
}