簡體   English   中英

AdMob強制關閉(Android)

[英]AdMob Force Close ( Android )

我現在真的很絕望,我已經嘗試了一切。

每當我運行我的應用程序時,它就會強制關閉。 它在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。

請幫助我:(

因為你說過

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內部正確分配layout

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM