【问题标题】:Android AdView NoClassDefFoundErrorAndroid AdView NoClassDefFoundError
【发布时间】:2013-08-10 21:11:13
【问题描述】:

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

请帮助我,因为我必须尽快发布。

这是应用崩溃后的 Logcat:

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。我刚刚关注this link 添加admob。

<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

我已经把它放在我的清单文件中>

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

    </activity>

这在我的 Activity onCreate 方法中 >

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();
}

提前致谢

【问题讨论】:

    标签: java android eclipse exception-handling admob


    【解决方案1】:

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

    08-11 02:28:56.973: E/AndroidRuntime(27461): java.lang.RuntimeException:无法启动活动 组件信息{com.gamerspitch.easybluetooth/com.gamerspitch.easybluetooth.BlueActivity}: android.view.InflateException:二进制 XML 文件第 9 行:错误 膨胀类 com.google.ads.AdView

    您的 AdView 存在问题,导致应用崩溃。

    能否请您发布您的 .xml 布局文件以及 Activity。

    更新:

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

          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 文件:

    <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>
    

    【讨论】:

    • 我已经发布了存在 admob 的 xml 和清单文件。请看一下 。谢谢
    • 我不认为在 XML 中创建 AdView 有什么问题,但以防万一我也更喜欢在代码中创建它。特别是如果您有不同风格的应用程序,或 IAB 产品来删除广告。
    • 当然,在 .xml 中创建它并没有错,但我也更喜欢代码,因为我有点想对事情有更多的控制权:) @Abhijit:请尝试我的上述建议,看看它是否有效那么。
    • 感谢您的建议。我确实尝试过,但它只会减少我的错误数量。你现在可以看看我的问题。正如你所说,我已经在 .xml 和 .java 文件中进行了编辑。而且我的错误并没有减少。
    • 请帮我解决这个问题,因为我根本无法做到这一点。谢谢
    【解决方案2】:

    看起来是您的 adView 导致了崩溃。你是如何实施的?您是否在清单中包含了所有所需的权限?

    ClassNotFound 异常表明您的 admob 库没有连接到您的 XML。有几件事会导致这种情况,但最有可能的是它没有在清单中声明:

     <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 文件的顶部声明:

    <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 的文档 herehere

    【讨论】:

    • 我已经发布了存在 admob 的 xml 和清单文件。请看一下 。我在清单中添加了 INTERNET 和 ACCESS_NETWORK_STATE 权限。
    • 你是如何构建应用程序的?如果您使用命令行(例如 Gradle 或 ANT),您的 IDE 项目可能会配置有不同的模块/库设置,从而在 IDE 中留下有效的类,但在构建过程之后会丢失。您是将 AdMob jar 放在 libs 中还是放在自定义库文件夹中?
    • 上面的代码我已经放了。仍然显示相同的错误
    • 我正在使用 Eclipse 开发这个应用程序。 admob jar 文件存在于引用的库文件夹中。
    【解决方案3】:

    您的代码或 XML AFAICT 没有任何问题。错误清楚地表明:

    Could not find class 'com.google.ads.AdView'
    

    您部署的应用似乎不包含 Admob 库。你需要弄清楚为什么。检查您的构建工具/环境。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多