Reputation: 77
im really desperate right now, ive tried everything.
Everytime i run my app this it force closes. It shows this in the 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);
Manifest:
<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>
Yes i imported the libaries and i changed build target. I also changed lib to libs.
Please help me :(
Upvotes: 1
Views: 932
Reputation:
Since you stated that
layout.addView(adView);
throws the NullPointerException, it's most likely that
findViewById(R.layout.main);
returned null
and layout
keeps that in the lines below. And that's actually the case here. You use R.layout.main
, which is a layout reference. You need an ID though, hence findViewById() - so in this case it will never work.
So to get this working, you need two things:
First, you need to assign an id to a LinearLayout. I guess the intended one is the outer one in listplaceholder.xml, and it has no id yet. Add an android:id
xml attribute, it should look like this in the end:
<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">
Second, change the above findViewById() statement to look for the correct id
findViewById(R.id.container);
This should result in a properly assigned layout inside layout
.
Upvotes: 1