2013-11-09 55 views
0

我正在使用android應用程序,並且正在嘗試將google addmob添加到我的應用程序。在我的代碼中沒有錯誤,但應用程序似乎在運行時被強制關閉。我找不到這個錯誤。請幫我, 我使用的代碼 -Android addmob應用程序強制關閉

adView = new AdView(this, AdSize.BANNER, "my add id"); 
RelativeLayout layout = (RelativeLayout)findViewById(R.id.adView);  
layout.addView(adView); 
AdRequest request = new AdRequest(); 
request.setTesting(false); 
adView.loadAd(request); 

請幫助我,因爲我是新來的Android開發

的logcat:

11-09 17:26:43.508: E/AndroidRuntime(8982): at android.app.Activity.setContentView(Activity.java:1862) 
11-09 17:26:43.508: E/AndroidRuntime(8982): Caused by: java.lang.ClassNotFoundException: com.google.ads.AdView 
+0

任何日誌錯誤? –

+0

沒有錯誤發生,但它強制運行時關閉:( – ptm

+0

可能是你沒有網絡權限的清單。 –

回答

0
// try this 
1. download latest google-play-services,jar 
2. add jar to project build path 

**main.xml** 
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    android:id="@+id/lnrMain"> 

</LinearLayout> 

public class MyActivity extends FragmentActivity { 

    LinearLayout lnrMain; 

    @Override 
    protected void onCreate(Bundle savedInstanceState){ 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     lnrMain = (LinearLayout) findViewById(R.id.lnrMain); 

     runOnUiThread(new Runnable() { 
      @Override 
      public void run() { 
       AdView adView = new AdView(MyActivity.this); 
       adView.setAdUnitId("0445b7141d9d4e1b"); 
       adView.setAdSize(AdSize.BANNER); 
       AdRequest.Builder builder = new AdRequest.Builder(); 
       builder.addTestDevice(AdRequest.DEVICE_ID_EMULATOR); 
       adView.loadAd(builder.build()); 
       lnrMain.addView(adView); 
      } 
     }); 

    } 
} 

**AndroidManifest.xml** 
<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.Demo" 
    android:versionCode="1" 
    android:versionName="1.0"> 

    <uses-sdk android:minSdkVersion="8" 
     android:targetSdkVersion="17"/> 
    <uses-permission android:name="android.permission.INTERNET" /> 

    <application 
     android:label="@string/app_name" 
     android:icon="@drawable/ic_launcher"> 
     <activity 
      android:name="MyActivity" 
      android:label="@string/app_name"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 

     <activity 
      android:name="com.google.android.gms.ads.AdActivity" 
      android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" /> 

     <meta-data 
      android:name="ADMOB_ALLOW_LOCATION_FOR_ADS" 
      android:value="true" /> 
     <meta-data 
      android:name="com.google.android.gms.version" 
      android:value="4030500" /> 
    </application> 
</manifest> 
+0

adView.setAdUnitId(「5655767」 ); \t \t adView.setAdSize(AdSize.SMART_BANNER); \t \t layout.addView(adView); \t \t AdRequest.Builder builder = new AdRequest.Builder(); \t \t builder.addTestDevice(AdRequest.DEVICE_ID_EMULATOR); – ptm

+0

這些代碼包含錯誤 – ptm

0

昨天我有我的第一個應用集成與admob和我所做的只是我將一個.jar庫添加到Java Build Path中。當清單文件,我添加這些線以上收盤應用標籤:

<activity 
      android:name="com.google.ads.AdActivity" 
      android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" /> 

這些權限:

<uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 

這是我的Manifest.xml看起來像:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="PACKAGENAME" 
    android:versionCode="1" 
    android:versionName="1.1" > 

    <uses-sdk 
     android:minSdkVersion="8" 
     android:targetSdkVersion="18" /> 

    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
    <uses-permission android:name="android.permission.CAMERA" /> 

    <uses-feature android:name="android.hardware.camera" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name=".MainActivity" 
      android:label="@string/app_name" 
      android:screenOrientation="portrait" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity 
      android:name="com.google.ads.AdActivity" 
      android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" /> 
    </application> 

</manifest> 

當我在main_acitivty.xml中添加了以下代碼行,我實際上想看到我的廣告橫幅:

<com.google.ads.AdView 
      xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads" 
      android:id="@+id/adView" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      ads:adSize="BANNER" 
      ads:adUnitId="YOU-ID" 
      ads:loadAdOnCreate="true" > 
     </com.google.ads.AdView> 

我其實沒有在我的.java文件中編寫任何代碼。因爲您可以選擇通過XML或以編程方式實施AdMob。

所有這些步驟都提供了這裏 - >AdMob Banners

+0

我使用了給出的代碼,但它也顯示了相同的錯誤 – ptm

+0

嘗試轉到您的項目屬性,當轉到「Java Build Path」時。在下一個窗口中,轉到「庫」,並在「添加外部JAR」時。最後,在同一個屏幕上,點擊「訂購和導出」標籤並選中「GoogleAdMobsAdsSDK」。你做過了嗎? 或者試試這個解決方案 - >> http://android-er.blogspot.com/2012/06/javalangnoclassdeffounderror.html –

0
here is my code am used 

    ****MainActivity.java` 




    package com.example.smacon; 
    import com.google.ads.*; 
    import android.app.Activity; 
    import android.content.Context; 
    import android.os.Bundle; 
    import android.view.Surface; 
    import android.webkit.WebView; 
    import android.widget.RelativeLayout; 
    import android.widget.RelativeLayout.LayoutParams; 


    public class MainActivity extends Activity { 
    private AdView adView; 
private WebView webView; 

public void onCreate(Bundle savedInstanceState) { 
    final Context context = this; 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    AdRequest adRequest = new AdRequest(); 

     //test mode on EMULATOR 
     adRequest.addTestDevice(AdRequest.TEST_EMULATOR); 

     //test mode on DEVICE (this example code must be replaced with your device uniquq ID) 
     adRequest.addTestDevice("4G74FC73D62D42B62A7F7DA61EF5F776"); 

     adView = (AdView)findViewById(R.id.adView);  

     // Initiate a request to load an ad in test mode. 
     // You can keep this even when you release your app on the market, because 
     // only emulators and your test device will get test ads. The user will receive real ads. 
     adView.loadAd(adRequest); 

    webView = (WebView) findViewById(R.id.webView1); 
    webView.getSettings().setJavaScriptEnabled(true); 
    webView.loadUrl("my url"); 
    webView.setWebViewClient(new MyWebViewClient()); 


} 


    @Override 
    public void onDestroy() { 
    if (adView != null) { 
    adView.destroy(); 
    } 
    super.onDestroy(); 
    } 

    } 

    ***** MyWebViewClient.java 



    package com.example.smacon; 

    import android.graphics.Bitmap; 
    import android.view.View; 
    import android.webkit.WebView; 
    import android.webkit.WebViewClient; 


    public class MyWebViewClient extends WebViewClient { 
// show the web page in webview but not in web browser 
    public boolean shouldOverrideUrlLoading(WebView view, String url) { 
    view.loadUrl(url); 
    return true; 
    } 

    @Override 
    public void onPageFinished(WebView view, String url) { 
    super.onPageFinished(view, url); 
    // webProg.setVisibility(View.GONE); 
    } 

    @Override 
    public void onPageStarted(WebView view, String url, Bitmap favicon) { 
    super.onPageStarted(view, url, favicon); 
    } 

    @Override 
    public void onLoadResource(WebView view, String url) { 
    super.onLoadResource(view, url); 
    } 

    } 

    **** manifest 




    <?xml version="1.0" encoding="utf-8"?> 
    <manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.smacon" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
    android:minSdkVersion="8" 
    android:targetSdkVersion="17" /> 

    <application 
    android:allowBackup="true" 
    android:debuggable="true" 
    android:icon="@drawable/logo" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <activity 
     android:name="com.example.smacon.MainActivity" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 

    <activity 
     android:name="com.google.ads.AdActivity" 
       android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode 
     |screenSize|smallestScreenSize" > 
    </activity> 

    </application> 
    <uses-permission android:name="android.permission.INTERNET"/> 
    </manifest> 



    ****activity_main.xml 



    <?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 


    <WebView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/webView1" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    /> 
    <com.google.ads.AdView android:id="@+id/adView" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        ads:adUnitId="7565756565" 
        ads:adSize="BANNER" 
        ads:loadAdOnCreate="true"/> 

    </LinearLayout> 



    no errors in this code. But it is forced closed . :(