首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android AdView NoClassDefFoundError

Android AdView NoClassDefFoundError
EN

Stack Overflow用户
提问于 2013-08-10 21:11:13
回答 3查看 351关注 0票数 0

我是Android开发的初学者。我刚刚发布了一个应用程序,但发现了一些故障,所以我想通过创建一个新的Android项目来纠正这些问题,这个项目的包名与发布的应用程序相同。但是一旦我完成了更新应用程序,这个应用程序就不会在手机上运行(调试)。他说:“不幸的是thisApp已经停止了。”这一次我也试图加入adMob。

请帮帮我,因为我必须尽快出版这篇文章。

这是应用程序崩溃后的日志:

代码语言:javascript
复制
08-11 18:14:31.063: E/dalvikvm(15877): Could not find class 'com.google.ads.AdView', referenced from method com.gamerspitch.easybluetooth.BlueActivity.initAdView
08-11 18:14:31.254: E/AndroidRuntime(15877): FATAL EXCEPTION: main
08-11 18:14:31.254: E/AndroidRuntime(15877): java.lang.NoClassDefFoundError: com.google.ads.AdView
08-11 18:14:31.254: E/AndroidRuntime(15877):    at com.gamerspitch.easybluetooth.BlueActivity.initAdView(BlueActivity.java:107)
08-11 18:14:31.254: E/AndroidRuntime(15877):    at com.gamerspitch.easybluetooth.BlueActivity.onCreate(BlueActivity.java:40)
08-11 18:14:31.254: E/AndroidRuntime(15877):    at android.app.Activity.performCreate(Activity.java:5133)
08-11 18:14:31.254: E/AndroidRuntime(15877):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
08-11 18:14:31.254: E/AndroidRuntime(15877):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
08-11 18:14:31.254: E/AndroidRuntime(15877):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
08-11 18:14:31.254: E/AndroidRuntime(15877):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
08-11 18:14:31.254: E/AndroidRuntime(15877):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
08-11 18:14:31.254: E/AndroidRuntime(15877):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-11 18:14:31.254: E/AndroidRuntime(15877):    at android.os.Looper.loop(Looper.java:137)
08-11 18:14:31.254: E/AndroidRuntime(15877):    at android.app.ActivityThread.main(ActivityThread.java:5103)
08-11 18:14:31.254: E/AndroidRuntime(15877):    at java.lang.reflect.Method.invokeNative(Native Method)
08-11 18:14:31.254: E/AndroidRuntime(15877):    at java.lang.reflect.Method.invoke(Method.java:525)
08-11 18:14:31.254: E/AndroidRuntime(15877):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
08-11 18:14:31.254: E/AndroidRuntime(15877):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-11 18:14:31.254: E/AndroidRuntime(15877):    at dalvik.system.NativeStart.main(Native Method)

这里是我的admob位置的XML。我只是跟着此链接添加admob。

代码语言:javascript
复制
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/easyb"
tools:context=".BlueActivity" >

<LinearLayout
    android:id="@+id/adviewPlaceholder"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:orientation="vertical" >
</LinearLayout>

//Other elements

我把这个放进了我的清单文件>

代码语言:javascript
复制
<activity android:name="com.google.ads.AdActivity" 
        android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" >

    </activity>

这在我的Activity方法中>

代码语言:javascript
复制
private AdView ad;


@Override
protected void onCreate(Bundle savedInstanceState) {
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_blue);

    initAdView();
//Other elements
protected void initAdView() {

    ad = new AdView(this, AdSize.SMART_BANNER, "a15204b9eb97566");

    LinearLayout ll = (LinearLayout)findViewById(R.id.adviewPlaceholder);

    ll.addView(ad);

    ad.loadAd(new AdRequest());
}

protected void destroyAdView() {
    if(ad != null) ad.destroy();
}

@Override
protected void onDestroy() {    
    // destroy the ad when the activity is destroyed
    destroyAdView();
    super.onDestroy();
}

提前谢谢

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-08-10 21:14:37

根据错误消息中的这一行:

08-11 02:28:56.973: E/AndroidRuntime(27461):java.lang.RuntimeException:无法启动活动android.view.InflateException:二进制android.view.InflateException文件行#9:错误膨胀类com.google.ads.AdView

您的AdView有一个问题,导致应用程序崩溃。

请您发布您的.xml布局文件以及活动。

更新:

为了让这件事更清楚一点。我从不在AdView中定义.xml。我只需在布局.xml文件中创建一个没有子文件的布局,并通过代码将AdView添加到其中。看起来是这样的:

代码语言:javascript
复制
      private AdView ad;

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

          setContentView(R.layout.yourlayout);

          initAdView();

          // other code...
      }

       protected void initAdView() {

            ad = new AdView(this, AdSize.SMART_BANNER, "MY_AD_UNIT_ID");

            LinearLayout ll = (LinearLayout) findViewById(R.id.adviewPlaceholder);

            ll.addView(ad);

            ad.loadAd(new AdRequest());
        }

        protected void destroyAdView() {
            if(ad != null) ad.destroy();
        }

        @Override
        protected void onDestroy() {    
            // destroy the ad when the activity is destroyed
            destroyAdView();
            super.onDestroy();
        }

以及布局yourlayout.xml文件:

代码语言:javascript
复制
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent >

    <!-- lots of other layout stuff here -->


    <!-- make the adview be on the bottom of the screen -->
    <LinearLayout
        android:id="@+id/adviewPlaceholder"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:orientation="vertical" >
    </LinearLayout>
 </RelativeLayout>
票数 2
EN

Stack Overflow用户

发布于 2013-08-10 21:23:00

看来是你的adView造成了坠机。你是如何实现它的?您是否包括了清单中所需的所有权限?

ClassNotFound异常表示您的admob库没有连接到您的XML。造成这种情况的原因有几种,但最有可能的是清单中没有声明它:

代码语言:javascript
复制
 <application android:icon="@drawable/icon" android:label="@string/app_name"
           android:debuggable="true">
  <activity android:label="@string/app_name" android:name="yourActivity">
       .....
  </activity>
  <activity android:name="com.google.ads.AdActivity"   <----- this line
            android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>
 </application>

或者它没有在XML文件的顶部声明:

代码语言:javascript
复制
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"   <----- this line
          android:orientation="vertical"
          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="MY_AD_UNIT_ID"
                     ads:adSize="BANNER"
                     ads:testDevices="TEST_EMULATOR, TEST_DEVICE_ID"
                     ads:loadAdOnCreate="true"/>
    </LinearLayout>

试试看,如果它们做不到,请查看Google的文档这里这里

票数 2
EN

Stack Overflow用户

发布于 2013-08-11 22:14:02

您的代码或XML AFAICT没有任何问题。这一错误明确指出:

代码语言:javascript
复制
Could not find class 'com.google.ads.AdView'

您部署的应用程序似乎不包含Ad暴徒库。你得找出原因。检查您的构建工具/环境。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18166379

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档