2012-04-02 10 views
1

今は本当に絶望的です。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に変更しました。

私を助けてください:(

+0

メインクラスファイルの41行目はどれですか? –

+0

layout.addView(adView); – Niell

+0

私の投稿にlistplaceholder.xmlファイルを追加しました – Niell

答えて

1

をあなたは

layout.addView(adView); 

は、NullPointerExceptionがスロー、それは

findViewById(R.layout.main); 

nullを返され、layoutがラインであること保つことが最も可能性がありますことを述べたので、それは実際にはここのケースです。あなたはを使用しています210、これはレイアウト基準である。 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(R.id.container); 

正しいIDを探すためにfindViewById上記()ステートメントを変更しますlayoutの中に割り当てられたレイアウト。

+0

どこにアンドロイドを追加するべきかわからない:listplaceholder.xmlのid = "@ + id/container"は既にfindViewByIdを変更しました。ヘルプありがとう – Niell

+0

@Niell完全なタグに編集しました。今はっきりしていることを願っています。 :) –

+0

よろしくお願いします、強制しないでください、私は広告を見ていませんが、最初の打ち上げでは少し時間がかかると聞きました=)ありがとうございました – Niell