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