AdMob for Android编译错误

时间:2012-06-02 00:23:14

标签: android admob

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

2 个答案:

答案 0 :(得分:2)

您的代码中错过了

setContentView(R.layout.your_layout) !!

使用

时会出现nullpointer异常
layout.addView(adView);

尝试调试代码!!

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

如果您需要包含更多视图(广告就是这种情况),那么ListActivity中的ListView仍然可以为您的活动分配布局。在这种情况下,您的布局必须包含一个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为listviews它是 android:id =“@ android:id / list”你必须牢记这一点!!

答案 1 :(得分: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

尝试这样的事情:

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

这已经过测试,并且正在运行