2012-04-02 73 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行? – 2012-04-02 19:43:21

+0

layout.addView(adView); – Niell 2012-04-02 19:47:45

+0

我将listplaceholder.xml文件添加到我的文章 – Niell 2012-04-02 19:52:40

回答

1

既然你说

layout.addView(adView); 

抛出NullPointerException异常,这是最有可能的

findViewById(R.layout.main); 

返回nulllayout保持在该线下面是这种情况,你使用R.layout.main,这是一个布局参考。你需要一个ID,因此findViewById() - 所以在这种情况下,它永远不会工作。

所以得到这个工作,你需要两样东西:

首先,你需要一个id分配到的LinearLayout。我猜想目标之一是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

+0

我不知道在listplaceholder.xml中添加android:id =“@ + id/container”的位置已经改变了findViewById。感谢您的帮助 – Niell 2012-04-02 20:00:15

+0

@Niell将它编辑为完整的标签。希望现在更清楚。 :) – 2012-04-02 20:01:39

+0

Yay谢谢,没有强制关闭,我没有看到广告tho,但我听到它需要一段时间首次推出=)非常感谢 – Niell 2012-04-02 20:06:33