【问题标题】:AdMob for Android Compile ErrorAdMob for Android 编译错误
【发布时间】:2012-06-02 19:39:49
【问题描述】:

我正在尝试将 AdMob 广告添加到我的应用中。我按照这里的说明进行操作:https://developers.google.com/mobile-ads-sdk/docs/android/fundamentals,当我尝试运行它时,我得到了这些错误:

 06-01 19:16:23.337: E/AndroidRuntime(30602): FATAL EXCEPTION: main
 06-01 19:16:23.337: E/AndroidRuntime(30602): java.lang.RuntimeException: Unable to    start activity ComponentInfo{com.ComedyZone/com.ComedyZone.MainActivity}: java.lang.NullPointerException
 06-01 19:16:23.337: E/AndroidRuntime(30602):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1696)
 06-01 19:16:23.337: E/AndroidRuntime(30602):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1716)
 06-01 19:16:23.337: E/AndroidRuntime(30602):   at android.app.ActivityThread.access$1500(ActivityThread.java:124)
 06-01 19:16:23.337: E/AndroidRuntime(30602):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:968)
 06-01 19:16:23.337: E/AndroidRuntime(30602):   at android.os.Handler.dispatchMessage(Handler.java:99)
 06-01 19:16:23.337: E/AndroidRuntime(30602):   at android.os.Looper.loop(Looper.java:130)
 06-01 19:16:23.337: E/AndroidRuntime(30602):   at android.app.ActivityThread.main(ActivityThread.java:3806)
 06-01 19:16:23.337: E/AndroidRuntime(30602):   at java.lang.reflect.Method.invokeNative(Native Method)
 06-01 19:16:23.337: E/AndroidRuntime(30602):   at java.lang.reflect.Method.invoke(Method.java:507)
 06-01 19:16:23.337: E/AndroidRuntime(30602):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
 06-01 19:16:23.337: E/AndroidRuntime(30602):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
 06-01 19:16:23.337: E/AndroidRuntime(30602):   at dalvik.system.NativeStart.main(Native Method)
 06-01 19:16:23.337: E/AndroidRuntime(30602): Caused by: java.lang.NullPointerException
 06-01 19:16:23.337: E/AndroidRuntime(30602):   at com.ComedyZone.MainActivity.onCreate(MainActivity.java:37)
 06-01 19:16:23.337: E/AndroidRuntime(30602):   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
 06-01 19:16:23.337: E/AndroidRuntime(30602):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1660)
 06-01 19:16:23.337: E/AndroidRuntime(30602):   ... 11 more

MainActivity.java:

package com.ComedyZone;

import android.content.Intent;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.actionbarsherlock.app.SherlockListActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.google.ads.*;


public class MainActivity extends SherlockListActivity {
public static int THEME = R.style.Theme_Sherlock;
private AdView adView;

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

    String[] countries = getResources().getStringArray(R.array.jokes_array);
    setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, countries));

    ListView lv = getListView();
    lv.setTextFilterEnabled(true);

 // Create the adView
    adView = new AdView(this, AdSize.BANNER, "a14fc778668df3b");

    // Lookup your LinearLayout assuming it’s been given
    // the attribute android:id="@+id/mainLayout"
    LinearLayout layout = (LinearLayout)findViewById(R.id.mainLayout);

    // Add the adView to it
    layout.addView(adView);

    // Initiate a generic request to load it with an ad
    adView.loadAd(new AdRequest());
  }

  @Override
  public void onDestroy() {
    if (adView != null) {
      adView.destroy();
    }
    super.onDestroy();
  }


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    menu.add("Options")
    .setIcon(R.drawable.ic_options)
    .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    Intent intent = new Intent(this, Preferences.class);
    startActivity(intent);
    return true;
}
}

Main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:id="@+id/mainLayout"
android:layout_height="fill_parent"
android:orientation="vertical">

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="16sp" >
</TextView>

<ListView
    android:id="@+id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
</ListView>

</LinearLayout>

清单:

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

    <uses-sdk android:minSdkVersion="7" />

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

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/Theme.Sherlock.Light">
        <activity android:name="com.ComedyZone.Preferences" />
        <activity
            android:name=".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>
        <activity android:name="com.google.ads.AdActivity"
              android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>
    </application>
</manifest>

【问题讨论】:

  • 发布您的代码,您的 MainActivity 第 37 行出现空指针异常
  • 你已经得到了一些很好的答案,但是在未来,在发布之前稍微调试一下。尽管我们感谢您的彻底,don't throw a block of code at your readers and expect results.
  • 我花了一整天的时间试图弄清楚这一点,但我不知道从哪里开始。

标签: android admob


【解决方案1】:

您的代码中缺少setContentView(R.layout.your_layout)!!

当你使用时你会得到一个空指针异常

layout.addView(adView);

尝试调试你的代码!!

如果您只想显示一个 ListView ListActivity 默认包含一个 ListView,则 ListActivity 不需要您通过 setContentView() 方法为其分配布局。

如果您需要在 ListActivity 中包含更多视图(即广告的情况)然后是 ListView,您仍然可以为您的 Activity 分配布局。在这种情况下,您的布局必须包含一个 ListView,其 android:id 属性设置为 @android:id/list

我想你的 SherlockListActivity 是从 ListActivity 扩展而来的,这就是我上面所说的你应该注意的!

<ListView
  android:id="@android:id/list"
  android:layout_width="match_parent"
  android:layout_height="wrap_content" >
</ListView>

android:id="@+id/list" 更改为android:id="@android:id/list"

列表活动在 android sdk 中为列表视图使用默认 ID,它是 android:id="@android:id/list" 你必须牢记这一点!!

【讨论】:

  • 我已经这样做了,但我收到错误消息:E/AndroidRuntime(7480): Caused by: java.lang.RuntimeException: Your content must have a ListView its id attribute is 'android.R.id .list'
  • 是的,我提到您的列表 ID 必须是 @android:id/list 而不是您选择的 ID
  • 检查我的 main.xml 文件。我已将其设置为列表
  • 检查我的更新,你应该使用的 id 是我给你的,而不是你选择的随机 id,我的朋友 listactivity 是如何工作的
  • 好的,它运行了,但是列表项和广告都没有显示。
【解决方案2】:

由于您使用的是 ListActivity 而您没有使用 setContentView(R.layout.main),因此您的 layout 将是 null,您将收到 NullPointerException。

更多关注https://developers.google.com/mobile-ads-sdk/docs/android/fundamentals中的示例

一种解决方案是通过 XML 来实现。

除了在 Java 中创建 AdView,还可以设置 一个完全在 XML 中。这样做很简单:

https://developers.google.com/mobile-ads-sdk/docs/android/banner_xml

看不到广告的问题与其所需的最小尺寸有关。 AdMob 视图至少应为 320 x 50,否则,您将永远不会在应用程序中看到广告。

https://developers.google.com/mobile-ads-sdk/docs/android/intermediate?hl=nl#bannersizes

试试这样的:

Main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:orientation="vertical">
       <LinearLayout android:layout_width="fill_parent"
                  android:id="@+id/home_layout"
                  android:orientation="vertical"
                  android:layout_height="wrap_content">
         <ListView 
            android:id="@id/android:list" 
            android:layout_height="wrap_content"
            android:layout_width="wrap_content" />
     </LinearLayout>

    <LinearLayout android:layout_width="fill_parent"
                      android:id="@+id/ad_layout"
                      android:layout_height="wrap_content"
                      android:gravity="bottom"
                      android:layout_alignParentBottom="true"
                      android:layout_alignBottom="@+id/home_layout">

     <com.google.ads.AdView android:id="@+id/adView"
                             android:layout_width="fill_parent"
                             android:layout_height="wrap_content"
                             ads:adUnitId="youaddunitid"
                             ads:adSize="BANNER"
                             android:gravity="bottom"
                             ads:testDevices="TEST_EMULATOR, yourphoneid"
                             ads:loadAdOnCreate="true"/>
    </LinearLayout>
</RelativeLayout>

MainActivity.java:

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);        
        setContentView(R.layout.main);
        String[] countries = getResources().getStringArray(R.array.jokes_array);
        ListView lv = getListView();

        lv.setTextFilterEnabled(true);
        lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, countries));
}

这已经过测试并且可以正常工作

【讨论】:

  • 我替换了:layout.addView(adView);与: lv.addFooterView(adView);我的应用现在可以运行,但广告没有显示。
  • 你在模拟器中测试吗?
  • 设置测试模式AdManager.setTestDevices( new String[] { AdManager.TEST_EMULATOR, // Android emulator "E83D20734F72FB3108F104ABC0FFC738", // your Phone id } );
  • 我设置了测试模式,在我的手机和模拟器上都测试过了,结果不行
  • 当我尝试通过 xml 执行此操作时出现 nullpointerexception 错误
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-08-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多