AdMob Force Close(Android)

时间:2012-04-02 19:40:19

标签: java android eclipse sdk admob

我现在非常绝望,我尝试了一切。

每次我运行我的应用程序,它强行关闭。它在logcat中显示了这一点。

04-02 21:26:33.079: E/AndroidRuntime(18013): FATAL EXCEPTION: main
04-02 21:26:33.079: E/AndroidRuntime(18013): java.lang.RuntimeException: Unable to            start activity ComponentInfo{com.pxr.tutorial.xmltest/com.pxr.tutorial.xmltest.Main}:    java.lang.NullPointerException
04-02 21:26:33.079: E/AndroidRuntime(18013):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
04-02 21:26:33.079: E/AndroidRuntime(18013):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
04-02 21:26:33.079: E/AndroidRuntime(18013):    at android.app.ActivityThread.access$600(ActivityThread.java:123)
04-02 21:26:33.079: E/AndroidRuntime(18013):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
04-02 21:26:33.079: E/AndroidRuntime(18013):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-02 21:26:33.079: E/AndroidRuntime(18013):    at android.os.Looper.loop(Looper.java:137)
04-02 21:26:33.079: E/AndroidRuntime(18013):    at android.app.ActivityThread.main(ActivityThread.java:4424)
04-02 21:26:33.079: E/AndroidRuntime(18013):    at java.lang.reflect.Method.invokeNative(Native Method)
04-02 21:26:33.079: E/AndroidRuntime(18013):    at java.lang.reflect.Method.invoke(Method.java:511)
04-02 21:26:33.079: E/AndroidRuntime(18013):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
04-02 21:26:33.079: E/AndroidRuntime(18013):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
04-02 21:26:33.079: E/AndroidRuntime(18013):    at dalvik.system.NativeStart.main(Native Method)
04-02 21:26:33.079: E/AndroidRuntime(18013): Caused by: java.lang.NullPointerException
04-02 21:26:33.079: E/AndroidRuntime(18013):    at com.pxr.tutorial.xmltest.Main.onCreate(Main.java:41)
04-02 21:26:33.079: E/AndroidRuntime(18013):    at android.app.Activity.performCreate(Activity.java:4465)
04-02 21:26:33.079: E/AndroidRuntime(18013):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
04-02 21:26:33.079: E/AndroidRuntime(18013):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
04-02 21:26:33.079: E/AndroidRuntime(18013):    ... 11 more

Main.java

public class Main extends ListActivity {
private AdView adView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.listplaceholder);

    LinearLayout layout = (LinearLayout)findViewById(R.layout.main);
    adView = new AdView(this, AdSize.BANNER, "here i put my id with the quotes");
    layout.addView(adView);
    AdRequest adRequest = new AdRequest();
    adRequest.setTesting(true);
    adView.loadAd(adRequest);

清单:

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

main.xml中:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"

listplaceholder.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">    

    <ListView
        android:id="@id/android:list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:drawSelectorOnTop="false" />

    <TextView
        android:id="@id/android:empty"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="No data"/>

</LinearLayout>

是的,我导入了库,我更改了构建目标。我还将lib改为libs。

请帮助我 :(

1 个答案:

答案 0 :(得分:1)

既然你说过

layout.addView(adView);

抛出NullPointerException,它很可能就是

findViewById(R.layout.main);

返回nulllayout将其保留在下面的行中。这就是实际情况。您使用R.layout.main,这是一个布局参考。你需要一个ID,因此findViewById() - 所以在这种情况下它永远不会工作。

为了让这个工作,你需要两件事:

首先,您需要为LinearLayout分配一个id。我猜想它的目标是listplaceholder.xml中的外部,它还没有id。添加android:id xml属性,它最终应该如下所示:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/container">

其次,更改上面的findViewById()语句以查找正确的id

findViewById(R.id.container);

这应该会导致在layout内正确分配布局。