很遗憾,应用程序已停止:AdMob错误?

时间:2015-06-14 11:27:32

标签: java android eclipse android-layout admob

The following classes could not be instantiated: - com.google.android.gms.ads.AdView (Open Class, Show Error Log) : AdMob

我已经按照开发人员admob网站上的文档中给出的步骤(按原样复制并粘贴)。 并在eclipse ADT中实现了他们的例子。它仍无法正常工作......应用程序意外停止。

这是 activity_main.xml

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />

<com.google.android.gms.ads.AdView
    android:id="@+id/adView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_alignParentBottom="true"
    ads:adSize="BANNER"
    ads:adUnitId="@string/banner_ad_unit_id">
</com.google.android.gms.ads.AdView>

</RelativeLayout>

- - - - - - - - MainActivity.java

package com.google.android.gms.example;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    AdView mAdView = (AdView) findViewById(R.id.adView);
    AdRequest adRequest = new AdRequest.Builder().build();
    mAdView.loadAd(adRequest);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

- 清单文件 -

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

<uses-sdk
    android:minSdkVersion="9"
    android:targetSdkVersion="17" />
<!-- 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_launcher"
    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="com.google.android.gms.example.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>
    <!--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>

- 错误日志 -

eclipse.buildId=v22.0.5-757759
java.version=1.8.0_31
java.vendor=Oracle Corporation
BootLoader constants: OS=win32, ARCH=x86, WS=win32, NL=en_US
Framework arguments:  -product com.android.ide.eclipse.adt.package.product
Command-line arguments:  -os win32 -ws win32 -arch x86 -product   com.android.ide.eclipse.adt.package.product

Error
Sun Jun 14 17:13:05 IST 2015
No command output when running: 'am start -n    com.google.android.gms.example/com.google.android.gms.example.MainActivity -a  android.intent.action.MAIN -c android.intent.category.LAUNCHER' on device  emulator-5554

com.android.ddmlib.ShellCommandUnresponsiveException
at com.android.ddmlib.AdbHelper.executeRemoteCommand(AdbHelper.java:408)
at com.android.ddmlib.Device.executeShellCommand(Device.java:435)
at    com.android.ide.eclipse.adt.internal.launch.ActivityLaunchAction.doLaunchAction(    ActivityLaunchAction.java:67)
at    com.android.ide.eclipse.adt.internal.launch.ActivityLaunchAction.doLaunchAction(    ActivityLaunchAction.java:109)
at     com.android.ide.eclipse.adt.internal.launch.AndroidLaunchController.doLaunchActi    on(AndroidLaunchController.java:1286)
at     com.android.ide.eclipse.adt.internal.launch.AndroidLaunchController.doLaunchActi    on(AndroidLaunchController.java:1298)
at     com.android.ide.eclipse.adt.internal.launch.AndroidLaunchController.launchApp(An    droidLaunchController.java:1270)
at     com.android.ide.eclipse.adt.internal.launch.AndroidLaunchController.simpleLaunch    (AndroidLaunchController.java:906)
at     com.android.ide.eclipse.adt.internal.launch.AndroidLaunchController.continueLaun    ch(AndroidLaunchController.java:748)
at     com.android.ide.eclipse.adt.internal.launch.AndroidLaunchController.launch(AndroidLaunchController.java:570)
at     com.android.ide.eclipse.adt.internal.launch.LaunchConfigDelegate.doLaunch(Launch    ConfigDelegate.java:322)
at     com.android.ide.eclipse.adt.internal.launch.LaunchConfigDelegate.launch(LaunchCo    nfigDelegate.java:238)
at       org.eclipse.debug.internal.core.LaunchConfiguration.launch(LaunchConfiguration.j      ava:855)
at     org.eclipse.debug.internal.core.LaunchConfiguration.launch(LaunchConfiguration.j    ava:704)
at     org.eclipse.debug.internal.ui.DebugUIPlugin.buildAndLaunch(DebugUIPlugin.java:10    47)
at   org.eclipse.debug.internal.ui.DebugUIPlugin$8.run(DebugUIPlugin.java:1251)
at org.eclipse.core.internal.jobs.Worker.run(Worker.java:53)

- logcat的 -

06-14 08:06:11.481: E/dalvikvm(1168): Could not find class 'com.google.android.gms.ads.AdView', referenced from method com.google.android.gms.example.MainActivity.onCreate
06-14 08:06:11.481: W/dalvikvm(1168): VFY: unable to resolve check-cast 639 (Lcom/google/android/gms/ads/AdView;) in Lcom/google/android/gms/example/MainActivity;
06-14 08:06:11.491: D/dalvikvm(1168): VFY: replacing opcode 0x1f at 0x000f
06-14 08:06:11.531: D/dalvikvm(1168): DexOpt: unable to opt direct call 0x120d at 0x13 in Lcom/google/android/gms/example/MainActivity;.onCreate
06-14 08:06:12.562: D/AndroidRuntime(1168): Shutting down VM
06-14 08:06:12.562: W/dalvikvm(1168): threadid=1: thread exiting with uncaught exception (group=0x414c4700)
06-14 08:06:12.761: E/AndroidRuntime(1168): FATAL EXCEPTION: main
06-14 08:06:12.761: E/AndroidRuntime(1168): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.google.android.gms.example/com.google.android.gms.example.MainActivity}: android.view.InflateException: Binary XML file line #17: Error inflating class com.google.android.gms.ads.AdView
06-14 08:06:12.761: E/AndroidRuntime(1168):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
06-14 08:06:12.761: E/AndroidRuntime(1168):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
06-14 08:06:12.761: E/AndroidRuntime(1168):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
06-14 08:06:12.761: E/AndroidRuntime(1168):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
06-14 08:06:12.761: E/AndroidRuntime(1168):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-14 08:06:12.761: E/AndroidRuntime(1168):     at android.os.Looper.loop(Looper.java:137)
06-14 08:06:12.761: E/AndroidRuntime(1168):     at android.app.ActivityThread.main(ActivityThread.java:5103)
06-14 08:06:12.761: E/AndroidRuntime(1168):     at java.lang.reflect.Method.invokeNative(Native Method)
06-14 08:06:12.761: E/AndroidRuntime(1168):     at java.lang.reflect.Method.invoke(Method.java:525)
06-14 08:06:12.761: E/AndroidRuntime(1168):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
06-14 08:06:12.761: E/AndroidRuntime(1168):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
06-14 08:06:12.761: E/AndroidRuntime(1168):     at dalvik.system.NativeStart.main(Native Method)
06-14 08:06:12.761: E/AndroidRuntime(1168): Caused by: android.view.InflateException: Binary XML file line #17: Error inflating class com.google.android.gms.ads.AdView
06-14 08:06:12.761: E/AndroidRuntime(1168):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:707)
06-14 08:06:12.761: E/AndroidRuntime(1168):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
06-14 08:06:12.761: E/AndroidRuntime(1168):     at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
06-14 08:06:12.761: E/AndroidRuntime(1168):     at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
06-14 08:06:12.761: E/AndroidRuntime(1168):     at android.view.LayoutInflater.inflate(LayoutInflater.java:353) 
06-14 08:06:12.761: E/AndroidRuntime(1168):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:267)
06-14 08:06:12.761: E/AndroidRuntime(1168):     at android.app.Activity.setContentView(Activity.java:1895)
06-14 08:06:12.761: E/AndroidRuntime(1168):     at com.google.android.gms.example.MainActivity.onCreate(MainActivity.java:14)
06-14 08:06:12.761: E/AndroidRuntime(1168):     at android.app.Activity.performCreate(Activity.java:5133)
06-14 08:06:12.761: E/AndroidRuntime(1168):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
06-14 08:06:12.761: E/AndroidRuntime(1168):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
06-14 08:06:12.761: E/AndroidRuntime(1168):     ... 11 more
06-14 08:06:12.761: E/AndroidRuntime(1168): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.android.gms.ads.AdView" on path: DexPathList[[zip file "/data/app/com.google.android.gms.example-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.google.android.gms.example-1, /system/lib]]
06-14 08:06:12.761: E/AndroidRuntime(1168):     at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:53)
06-14 08:06:12.761: E/AndroidRuntime(1168):     at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
06-14 08:06:12.761: E/AndroidRuntime(1168):     at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
06-14 08:06:12.761: E/AndroidRuntime(1168):     at android.view.LayoutInflater.createView(LayoutInflater.java:559)
06-14 08:06:12.761: E/AndroidRuntime(1168):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
06-14 08:06:12.761: E/AndroidRuntime(1168):     ... 21 more

5 个答案:

答案 0 :(得分:3)

您的代码应该有效,没有问题。我认为,您没有将您的项目与Google提供的库集成。这篇文章可能会对你有所帮助:

https://developers.google.com/admob/android/eclipse#include_the_google_play_services_library

请彻底按照步骤操作。

答案 1 :(得分:2)

您是否已将Google Play服务lib导入为项目?

将google-play-services_lib导入为项目非常重要: Select File > Import > Android > Existing Android Code Into Workspace并单击“下一步”。 选择浏览...,输入 [android-sdk-folder]/extras/google/google_play_services/libproject/google-play-services_lib,然后单击“完成”。 (参见“示例代码”下的https://developers.google.com/maps/documentation/android/intro) 然后按照链接答案中的说明进行操作:

  
      
  • 导入“google-play-services_lib”项目的实际来源,并将其链接为&gt; Android库。

  •   
  • 通过Project执行此操作 - &gt;属性 - &gt; Android - &gt;图书馆,添加 - &gt; google-play-services_lib(您可以右键单击您的项目和   选择属性,然后选择Android)。

  •   
  • 不要通过项目的“Java Build Path”将其添加为依赖项目,这对我来说无效。

  •   

答案 2 :(得分:1)

  

从您的网站获取自己的Google Play服务   ADT-束 - 窗口x86-20130514 \ SDK \演员\谷歌。这会奏效   挑衅。

答案 3 :(得分:1)

在设备上执行shell命令时发送

ShellCommandUnresponsiveException需要太长时间才能发送输出。

该命令实际上可能没有响应,它只是花了太多时间没有向控制台输出任何东西。 所以这个问题与你的代码无关。

答案 4 :(得分:0)

修改(答案):

谢谢你的回答。我很感激。

我开始了解实际问题,Eclipse ADT没有更新,因此错误在那里,但是在更新之后,它仍然无法工作,因此我切换到Android Studio,代码现在按预期工作。< / p>

谢谢大家