当我尝试将AdMob广告放入Android应用中时,我收到了错误消息。我已经使用Eclipse + ADT完成了它,但是当我尝试使用Android Studio时,它无法正常工作。我想我对如何在Android Studio中配置项目了解不足。我将在下面提供一些细节:
堆栈跟踪错误:
10-17 23:57:05.910 2067-2067/com.nlpoomanual E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.nlpoomanual, PID: 2067
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nlpoomanual/br.com.nlpoomanual.activity.AjudeNosActivity}: android.view.InflateException: Binary XML file line #19: Error inflating class com.google.android.gms.ads.Adview
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: android.view.InflateException: Binary XML file line #19: Error inflating class com.google.android.gms.ads.Adview
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:757)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:255)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)
at br.com.nlpoomanual.activity.AjudeNosActivity.onCreate(AjudeNosActivity.java:25)
at android.app.Activity.performCreate(Activity.java:5937)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.android.gms.ads.Adview" on path: DexPathList[[zip file "/data/app/com.nlpoomanual-1/base.apk"],nativeLibraryDirectories=[/vendor/lib64, /system/lib64]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
at android.view.LayoutInflater.createView(LayoutInflater.java:571)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:255)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)
at br.com.nlpoomanual.activity.AjudeNosActivity.onCreate(AjudeNosActivity.java:25)
at android.app.Activity.performCreate(Activity.java:5937)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Suppressed: java.lang.ClassNotFoundException: com.google.android.gms.ads.Adview
at java.lang.Class.classForName(Native Method)
at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
... 25 more
Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available
AjudeNosActivity.java
package br.com.nlpoomanual.activity;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.AdapterView;
import android.widget.RadioButton;
import android.view.View;
import android.widget.RadioGroup;
import br.com.nlpoomanual.R;
import br.com.nlpoomanual.bean.Sugestao;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
public class AjudeNosActivity extends ActionBarActivity {
Sugestao sugestao = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ajude_nos);
AdView ajudeNosAdAview = (AdView) findViewById (R.id.ajudeNosAdView);
AdRequest adRequest = new AdRequest.Builder().addTestDevice(AdRequest.DEVICE_ID_EMULATOR).build();
ajudeNosAdAview.loadAd (adRequest);
}
/* Other methods that I believe that are unecessary to show...
*/
}
RES /布局/ ajude_nos.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ScrollView
android:id="@+id/scroll"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/linear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<com.google.android.gms.ads.Adview
android:id="@+id/ajudeNosAdView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"
ads:adSize="BANNER"
ads:adUnitId="ca-app-pub-7700319823577941/5947568710"
/>
<TextView
android:id="@+id/desc_ajude_nos"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/texto_comum"
android:text="@string/desc_ajude_nos"
android:layout_margin="15dp"
/>
<RadioGroup
android:id="@+id/rg_ajude_nos"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@color/pink"
>
<RadioButton
android:id="@+id/rb_sugestao_produto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/rb_sugestao_produto"
android:onClick="onRadioButtonClicked"
android:checked="true"
android:background="@color/background_white"
android:layout_marginBottom="7dp"
android:layout_marginTop="7dp"
/>
<RadioButton
android:id="@+id/rb_sugestao_componente"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/rb_sugestao_componente"
android:onClick="onRadioButtonClicked"
android:background="@color/background_white"
android:layout_marginBottom="7dp"
/>
<RadioButton
android:id="@+id/rb_sugestao_termo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/rb_sugestao_termo"
android:onClick="onRadioButtonClicked"
android:background="@color/background_white"
android:layout_marginBottom="7dp"
/>
<RadioButton
android:id="@+id/rb_comunique_erro"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/rb_comunique_erro"
android:onClick="onRadioButtonClicked"
android:background="@color/background_white"
android:layout_marginBottom="7dp"
/>
<RadioButton
android:id="@+id/rb_outros"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/rb_outros"
android:onClick="onRadioButtonClicked"
android:background="@color/background_white"
android:layout_marginBottom="7dp"
/>
</RadioGroup>
<Button
android:id="@+id/bt_proximo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/bt_proximo"
android:layout_gravity="center"
android:layout_margin="20dp"
android:onClick="onButtonClicked"
android:background="@color/pink"
android:textColor="@color/background_white"
/>
</LinearLayout>
</ScrollView>
</LinearLayout>
应用程序/的build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
defaultConfig {
applicationId "com.nlpoomanual"
minSdkVersion 11
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.0.1'
compile 'com.google.android.gms:play-services-ads:8.1.0'
compile files('libs/google-play-services.jar')
}
的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="br.com.nlpoomanual" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="23"
>
</uses-sdk>
<application
android:allowBackup="true"
android:icon="@drawable/ic_afrogirl"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".activity.TelaInicial"
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=".activity.SobreActivity"
android:label="@string/sobre_titulo" >
</activity>
<activity
android:name=".activity.AjudeNosActivity"
android:label="@string/title_activity_ajude_nos" >
</activity>
<activity
android:name=".activity.DicionarioDeTermos"
android:label="@string/title_activity_dicionario_de_termos" >
</activity>
<activity android:name=".activity.DescricaoTermoActivity"
android:label="@string/dicionario"
>
</activity>
<activity android:name=".activity.BuscaComponentesActivity"
android:label="@string/busca_componentes">
</activity>
<activity android:name=".activity.TipoProdutoActivity"
android:label="@string/produtos_liberados"
>
</activity>
<activity android:name=".activity.BuscaProdutosActivity"
android:label="@string/produtos_liberados"
>
</activity>
<activity android:name=".activity.DescricaoProdutoActivity"
android:label="@string/produtos_liberados"
>
</activity>
<activity android:name=".activity.MenuPrincipalActivity"
android:label="@string/app_name">
</activity>
<activity
android:name=".activity.FotoProdutoActivity"
android:label="@string/produtos_liberados"
>
</activity>
<activity android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:theme="@android:style/Theme.Translucent" />
</application>
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
我猜这些文件应该有助于理解问题。 提前谢谢......
答案 0 :(得分:0)
我通过将应用程序文件从Google复制到BannerExample项目来解决错误。为了让我的应用程序显示广告,我特意更改了以下文件。
的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="br.com.nlpoomanual" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="23"
>
</uses-sdk>
<!-- Include required permissions for Google Mobile Ads to run-->
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_afrogirl"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<!--This meta-data tag is required to use Google Play Services.-->
<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version"/>
<activity
android:name=".activity.TelaInicial"
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.example.bannerexample.MyActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".activity.SobreActivity"
android:label="@string/sobre_titulo" >
</activity>
<activity
android:name=".activity.AjudeNosActivity"
android:label="@string/title_activity_ajude_nos" >
</activity>
<activity
android:name=".activity.DicionarioDeTermos"
android:label="@string/title_activity_dicionario_de_termos" >
</activity>
<activity android:name=".activity.DescricaoTermoActivity"
android:label="@string/dicionario"
>
</activity>
<activity android:name=".activity.BuscaComponentesActivity"
android:label="@string/busca_componentes">
</activity>
<activity android:name=".activity.TipoProdutoActivity"
android:label="@string/produtos_liberados"
>
</activity>
<activity android:name=".activity.BuscaProdutosActivity"
android:label="@string/produtos_liberados"
>
</activity>
<activity android:name=".activity.DescricaoProdutoActivity"
android:label="@string/produtos_liberados"
>
</activity>
<activity android:name=".activity.MenuPrincipalActivity"
android:label="@string/app_name">
</activity>
<activity
android:name=".activity.FotoProdutoActivity"
android:label="@string/produtos_liberados"
>
</activity>
<!--Include the AdActivity configChanges and theme. -->
<activity android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:theme="@android:style/Theme.Translucent" />
</application>
</manifest>
ajude_nos.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".activity.AjudeNosActivity"
tools:ignore="MergeRootFrame"
>
<ScrollView
android:id="@+id/scroll"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/linear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<com.google.android.gms.ads.AdView
android:id="@+id/ajudeNosAdView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
ads:adSize="BANNER"
ads:adUnitId="ca-app-pub-7700319823577941/5947568710" />
<TextView
android:id="@+id/desc_ajude_nos"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/texto_comum"
android:text="@string/desc_ajude_nos"
android:layout_margin="15dp"
/>
<RadioGroup
android:id="@+id/rg_ajude_nos"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@color/pink"
>
<RadioButton
android:id="@+id/rb_sugestao_produto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/rb_sugestao_produto"
android:onClick="onRadioButtonClicked"
android:checked="true"
android:background="@color/background_white"
android:layout_marginBottom="7dp"
android:layout_marginTop="7dp"
/>
<RadioButton
android:id="@+id/rb_sugestao_componente"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/rb_sugestao_componente"
android:onClick="onRadioButtonClicked"
android:background="@color/background_white"
android:layout_marginBottom="7dp"
/>
<RadioButton
android:id="@+id/rb_sugestao_termo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/rb_sugestao_termo"
android:onClick="onRadioButtonClicked"
android:background="@color/background_white"
android:layout_marginBottom="7dp"
/>
<RadioButton
android:id="@+id/rb_comunique_erro"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/rb_comunique_erro"
android:onClick="onRadioButtonClicked"
android:background="@color/background_white"
android:layout_marginBottom="7dp"
/>
<RadioButton
android:id="@+id/rb_outros"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/rb_outros"
android:onClick="onRadioButtonClicked"
android:background="@color/background_white"
android:layout_marginBottom="7dp"
/>
</RadioGroup>
<Button
android:id="@+id/bt_proximo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/bt_proximo"
android:layout_gravity="center"
android:layout_margin="20dp"
android:onClick="onButtonClicked"
android:background="@color/pink"
android:textColor="@color/background_white"
/>
</LinearLayout>
</ScrollView>
</LinearLayout>
的build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
defaultConfig {
applicationId "com.google.android.gms.example.bannerexample"
minSdkVersion 9
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.0.1'
compile 'com.google.android.gms:play-services-ads:8.1.0'
}