Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

This is the xml code:

<LinearLayout 
    android:id="@+id/mainLayout"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity"
    android:orientation="vertical"
    android:background="@color/black" >

    <ScrollView
        android:id="@+id/mainScrollView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/teal"
        android:layout_weight=".50">

     <LinearLayout
            android:id="@+id/linearLayout1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >

     </LinearLayout>


    </ScrollView>

    <Button 
         android:id="@+id/addButton"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:text="+"
         android:gravity="center"
         android:layout_weight=".20"
     />

     <TextView
         android:id="@+id/totalHoursLabel"
         android:text="Ore Totali"
         android:background="@color/gray"
         android:textColor="@color/red"
         android:gravity="center"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:layout_weight=".10"
     />

     <TextView
         android:id="@+id/totalHoursView"
         android:text="00:00"
         android:background="@color/white"
         android:textColor="@color/black"
         android:gravity="center"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:layout_weight=".20"
     />

</LinearLayout>

My problem is that when I try to add a view to the ScrollView or his inner LinearLayout my program crashes.

Here is my java code:

super.onCreate(savedInstanceState);

LinearLayout ll = (LinearLayout) findViewById(R.id.linearLayout1);

TextView tv = new TextView(this);
tv.setText("testView");
tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
ll.addView(tv);

setContentView(R.layout.activity_main);

If I create a new android project this code (with only a relativeLayout and everything else empty in the xml file) works fine.

I think that there is some problem with the findViewById method or my xml file.

Any ideas?

This is my LogCat file:

07-23 15:56:19.119: D/AndroidRuntime(1249): Shutting down VM
07-23 15:56:19.119: W/dalvikvm(1249): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
07-23 15:56:19.159: E/AndroidRuntime(1249): FATAL EXCEPTION: main
07-23 15:56:19.159: E/AndroidRuntime(1249): java.lang.RuntimeException: Unable to start activity ComponentInfo{workTimer.worktimer/workTimer.worktimer.MainActivity}: java.lang.NullPointerException
07-23 15:56:19.159: E/AndroidRuntime(1249):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
07-23 15:56:19.159: E/AndroidRuntime(1249):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
07-23 15:56:19.159: E/AndroidRuntime(1249):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-23 15:56:19.159: E/AndroidRuntime(1249):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
07-23 15:56:19.159: E/AndroidRuntime(1249):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-23 15:56:19.159: E/AndroidRuntime(1249):     at android.os.Looper.loop(Looper.java:137)
07-23 15:56:19.159: E/AndroidRuntime(1249):     at android.app.ActivityThread.main(ActivityThread.java:5041)
07-23 15:56:19.159: E/AndroidRuntime(1249):     at java.lang.reflect.Method.invokeNative(Native Method)
07-23 15:56:19.159: E/AndroidRuntime(1249):     at java.lang.reflect.Method.invoke(Method.java:511)
07-23 15:56:19.159: E/AndroidRuntime(1249):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-23 15:56:19.159: E/AndroidRuntime(1249):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-23 15:56:19.159: E/AndroidRuntime(1249):     at dalvik.system.NativeStart.main(Native Method)
07-23 15:56:19.159: E/AndroidRuntime(1249): Caused by: java.lang.NullPointerException
07-23 15:56:19.159: E/AndroidRuntime(1249):     at workTimer.worktimer.MainActivity.onCreate(MainActivity.java:32)
07-23 15:56:19.159: E/AndroidRuntime(1249):     at android.app.Activity.performCreate(Activity.java:5104)
07-23 15:56:19.159: E/AndroidRuntime(1249):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
07-23 15:56:19.159: E/AndroidRuntime(1249):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
07-23 15:56:19.159: E/AndroidRuntime(1249):     ... 11 more
07-23 15:56:22.649: I/Process(1249): Sending signal. PID: 1249 SIG: 9
share|improve this question
 
Can you post the logcat? –  Heinrisch Jul 23 '13 at 15:45
 
Ok. I added it. –  Kami Jul 23 '13 at 15:58
 
I think I solved your problem, have a look at my answer –  Simon Jul 23 '13 at 16:05
add comment

2 Answers

up vote 1 down vote accepted

You have to call setContentView before you can call findViewById, so your code will be:

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

LinearLayout ll = (LinearLayout) findViewById(R.id.linearLayout1);

TextView tv = new TextView(this);
tv.setText("testView");
tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
ll.addView(tv);
share|improve this answer
 
Thanks, but it did not work. Actually I had already tried to change that line. –  Kami Jul 23 '13 at 16:17
 
Then you should check your id's and stuff again because the code is right this way (findviewbyid after setcontentview!) –  Simon Jul 23 '13 at 16:20
 
Actually I tried again and it worked. Thanks. –  Kami Jul 23 '13 at 16:23
add comment

My guess is that you're using the wrong LayoutParams. The LayoutParams of a view should have the type of its parent. In your case:

tv.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

Also, you are adding the TextView twice to your LinearLayout.

ll.addView(tv);

ll.addView(tv);

Don't do this. Create a second TextView and add that if you need two TextViews in there.

If these don't help, post your crash log from LogCat and I can help you from there.

share|improve this answer
 
I edited it. I only write ll.addView(tv); once. –  Kami Jul 23 '13 at 15:52
 
Did you try changing the type of the LayoutParams? And please post your logcat. –  Lisa Wray Jul 23 '13 at 15:58
 
I tried to change the parameters, but it did not work. I added my logcat. Thanks. –  Kami Jul 23 '13 at 15:59
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.