0

我正在尝试将 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;
}
}

主要.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>
4

2 回答 2

2

setContentView(R.layout.your_layout)您的代码中遗漏了!

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

layout.addView(adView);

尝试调试您的代码!

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

如果您需要在 ListActivity 中包含更多视图(即广告的情况),那么您仍然可以为您的 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"你必须记住这一点!!

于 2012-06-02T01:37:14.520 回答
1

由于您正在使用 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

尝试这样的事情:

主.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));
}

这是经过测试的,它正在工作

于 2012-06-02T01:20:05.040 回答