2014-11-23 44 views
1

主要活動的Java文件:了java.lang.RuntimeException:無法啓動活動ComponentInfo {} android.view.InflateException:二進制XML文件行#8:

public class MainActivity extends AndroidApplication { 
    private InterstitialAd interstitial; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration(); 
     cfg.useGL20 = false; 

     initialize(new Library(), cfg); 

     setContentView(R.layout.activity_main); 

     // Prepare the Interstitial Ad 
     interstitial = new InterstitialAd(MainActivity.this); 
     // Insert the Ad Unit ID 
     interstitial.setAdUnitId("ca-app-pub-123456789/123456789"); 

     //Locate the Banner Ad in activity_main.xml 
     AdView adView = (AdView) this.findViewById(R.id.adView); 

     // Request for Ads 
     AdRequest adRequest = new AdRequest.Builder() 

     // Add a test device to show Test Ads 
     .addTestDevice(AdRequest.DEVICE_ID_EMULATOR) 
     .addTestDevice("CC5F2C72DF2B356BBF0DA198") 
       .build(); 

     // Load ads into Banner Ads 
     adView.loadAd(adRequest); 

     // Load ads into Interstitial Ads 
     interstitial.loadAd(adRequest); 

     // Prepare an Interstitial Ad Listener 
     interstitial.setAdListener(new AdListener() { 
      public void onAdLoaded() { 
       // Call displayInterstitial() function 
       displayInterstitial(); 
      } 
     }); 

    } 
    public void displayInterstitial() { 
     // If Ads are loaded, show Interstitial else show nothing. 
     if (interstitial.isLoaded()) { 
      interstitial.show(); 
     } 
    } 
} 

Actvity 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/res-auto" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <com.google.android.gms.ads.AdView 
     android:id="@+id/adView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     ads:adSize="BANNER" 
     ads:adUnitId="ca-app-pub-123456789/123456789" /> 

</LinearLayout> 

Mainfest XML

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

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

    <application 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" > 
     <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version"/> 
     <activity 
      android:name=".MainActivity" 
      android:label="@string/app_name" 
      android:screenOrientation="landscape" 
      android:configChanges="keyboard|keyboardHidden|orientation|screenSize"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version"/> 
    <activity android:name="com.google.android.gms.ads.AdActivity" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/> 

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

錯誤:

11-22 11:23:55.144: E/AndroidRuntime(955): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xxx.Library/com.neodots.Library.MainActivity}: android.view.InflateException: Binary XML file line #8: Error inflating class com.google.android.gms.ads.AdView 
11-22 11:23:55.144: E/AndroidRuntime(955): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211) 
11-22 11:23:55.144: E/AndroidRuntime(955): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261) 
11-22 11:23:55.144: E/AndroidRuntime(955): at android.app.ActivityThread.access$600(ActivityThread.java:141) 
11-22 11:23:55.144: E/AndroidRuntime(955): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256) 
11-22 11:23:55.144: E/AndroidRuntime(955): at android.os.Handler.dispatchMessage(Handler.java:99) 
11-22 11:23:55.144: E/AndroidRuntime(955): at android.os.Looper.loop(Looper.java:137) 

我是新手android編程,使用一些教程我寫了這些代碼。但現在我不能運行我的應用程序,由於一堆錯誤。所以任何人都可以幫助我解決,謝謝你。 .................................................. .........

+0

由於設備太舊而無法編譯API,因此出現類似的錯誤。你有沒有修改你的'build.gradle'文件? – Johnny 2014-11-23 18:07:24

+0

模擬器不舊。實際上,我正在使用libgdx,我在我的項目中找到了build.gradle。您可以幫助如何設置.. – 2014-11-24 03:20:02

+0

您是否在項目中引用了Google Play服務庫? – 2014-11-24 11:09:18

回答

0

看起來你沒有在你的APK中包含GooglePlayServices庫。由於您使用的是非標準的構建方式,因此您需要參考他們的文檔,瞭解如何確保GPS庫包含在您的APK中。

+0

我按照以下步驟https://developer.android.com/google/play-services/setup.html – 2014-11-25 02:54:10

+0

對不起我的壞。當我說「我遵循一些教程」並使用libgdx時,我認爲您正在使用一些libgdx構建機制。該教程的重要標題是「將Google Play服務添加到您的項目」。確保你爲你的環境做了正確的選擇。你在使用Android Studio,Eclipse還有其他的東西嗎? – William 2014-11-25 20:16:27

相關問題