HorizontalScrollView Item hinzufügen

C

Crosaider

Fortgeschrittenes Mitglied
11
Hallo @ all

Ich schreibe gerade an einem App, wo ich Itemes einer HorizontalScrollView dynamisch hinzufügen möchte. Mein Layout von der HorizontalScrollView sieht so aus:

HTML:
<HorizontalScrollView
    android:id="@+id/horizontalScrollView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/imageView1"
    android:scrollbars="none" >
    
    <LinearLayout
        android:id="@+id/ScrollViewLayout"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:gravity="fill_horizontal"
        android:orientation="horizontal">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical" >

                <TextView
                    android:id="@+id/TextView02"
                    android:layout_width="105dp"
                    android:layout_height="wrap_content"
                    android:text="@string/note"
                    android:textAppearance="?android:attr/textAppearanceMedium" />

                <TextView
                    android:id="@+id/TextView01"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="2"
                    android:textAppearance="?android:attr/textAppearanceMedium" />
            </LinearLayout>
        </LinearLayout>

        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/trennwand_vertikal" />
    </LinearLayout>
</HorizontalScrollView>
Nun habe ich so versucht ein Item der Liste hinzuzufügen:

Code:
LinearLayout sv = (LinearLayout) findViewById(R.id.ScrollViewLayout);
        
TextView tv = new TextView(this);
tv.setText("Test");
sv.addView(tv);
Bei sv.addView(tv); tritt allerdings ein Fehler auf und das App stürzt ab. Woran kann das liegen? Was habe ich falsch gemacht?

Viele Grüße
Crosaider
 
Hättest du vielleicht den Text ausm logcat? Hilft oftmals sehr

Edit:
Ich kenn das so das man es so macht:

Private textview txtmeintext;

In der oncreate

Txtmeintext = (textview) findviewbyid(id);
Txtmeintext.settext("Test");

Sent from my iPhone using Tapatalk
 
Zuletzt bearbeitet:
Hier der Log:
Code:
09-01 11:02:23.830: D/AndroidRuntime(370): Shutting down VM
09-01 11:02:23.830: W/dalvikvm(370): threadid=1: thread exiting with uncaught exception (group=0x40015560)
09-01 11:02:23.839: E/AndroidRuntime(370): FATAL EXCEPTION: main
09-01 11:02:23.839: E/AndroidRuntime(370): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bko.lehrerorganizer/com.bko.lehrerorganizer.Schueler}: java.lang.NullPointerException
09-01 11:02:23.839: E/AndroidRuntime(370):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
09-01 11:02:23.839: E/AndroidRuntime(370):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
09-01 11:02:23.839: E/AndroidRuntime(370):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
09-01 11:02:23.839: E/AndroidRuntime(370):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
09-01 11:02:23.839: E/AndroidRuntime(370):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-01 11:02:23.839: E/AndroidRuntime(370):     at android.os.Looper.loop(Looper.java:123)
09-01 11:02:23.839: E/AndroidRuntime(370):     at android.app.ActivityThread.main(ActivityThread.java:3683)
09-01 11:02:23.839: E/AndroidRuntime(370):     at java.lang.reflect.Method.invokeNative(Native Method)
09-01 11:02:23.839: E/AndroidRuntime(370):     at java.lang.reflect.Method.invoke(Method.java:507)
09-01 11:02:23.839: E/AndroidRuntime(370):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-01 11:02:23.839: E/AndroidRuntime(370):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-01 11:02:23.839: E/AndroidRuntime(370):     at dalvik.system.NativeStart.main(Native Method)
09-01 11:02:23.839: E/AndroidRuntime(370): Caused by: java.lang.NullPointerException
09-01 11:02:23.839: E/AndroidRuntime(370):     at com.bko.lehrerorganizer.Schueler.onCreate(Schueler.java:44)
09-01 11:02:23.839: E/AndroidRuntime(370):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-01 11:02:23.839: E/AndroidRuntime(370):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
09-01 11:02:23.839: E/AndroidRuntime(370):     ... 11 more
09-01 11:02:25.999: I/Process(370): Sending signal. PID: 370 SIG: 9

Bei deiner Methode musst die TextView aber schon bestehen. Ich möchte sie ja dynamisch erzeugen und später mehrere hinzufügen. Wenn ich das neach deiner Methode mache würde ich ja immer nur den Text von diesem einen bestimmten Label ändern. Ich möchte schon dynamisch erzeugen und dann später jeder TextView einen eigene Text hinzufügen.
 
Dann musst du dir dein eigenen Adapter schreiben


Sent from my iPhone using Tapatalk
 
Falls du immer noch das Problem hast, dann versuch mal diesen Codeschnipsel hier:

Code:
LinearLayout MainLL= (LinearLayout) findViewById(R.id.myLayoutId);  
  for(int i=0; i<5; i++){ 
 TextView text = new TextView(this); 
   text.setText("The Value of i is :"+i); // <-- does it really compile without the + sign? 
   text.setTextSize(12);   
   text.setGravity(Gravity.LEFT); 
   text.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); 
MainLL.addView(text); 
}
 

Ähnliche Themen

J
Antworten
5
Aufrufe
929
swa00
swa00
E
Antworten
2
Aufrufe
777
ekaya999
E
W
Antworten
5
Aufrufe
1.132
washpuda
W
Zurück
Oben Unten