Hinzufügen einer Ansicht innerhalb einer ScrollView programmgesteuert
Dies ist der 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>
Mein problem ist, dass wenn ich versuche, um einen Blick auf die ScrollView oder seine innere LinearLayout mein Programm stürzt ab.
Hier ist mein 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);
Wenn ich ein neues android-Projekt mit diesem code (nur mit einem relativeLayout und alles andere leer in der xml-Datei), funktioniert einwandfrei.
Ich denke, dass es einige problem mit der Methode findViewById oder meine xml-Datei.
Irgendwelche Ideen?
Dies ist mein LogCat Datei:
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
Kannst du die logcat?
Ok. Ich habe es.
Ich denke ich habe dein problem gelöst, haben Sie einen Blick auf meine Antwort
Ok. Ich habe es.
Ich denke ich habe dein problem gelöst, haben Sie einen Blick auf meine Antwort
InformationsquelleAutor Kami | 2013-07-23
Du musst angemeldet sein, um einen Kommentar abzugeben.
Haben Sie zum Aufruf setContentView bevor Sie anrufen können Sie findViewById, so wird sich der code:
Dann sollten Sie Ihre id ' s und Zeug wieder, weil der code ist richtig so (findviewbyid nach setcontentview!)
Eigentlich habe ich es erneut versucht und es funktionierte. Danke.
InformationsquelleAutor Simon
Meine Vermutung ist, dass Sie die falsche LayoutParams. Die LayoutParams einem Blick haben sollten, die Art Ihrer Eltern. In deinem Fall:
Auch, Sie sind das hinzufügen der TextView zweimal, um dein LinearLayout.
Tun das nicht. Erstellen Sie eine zweite TextView und hinzufügen, dass, wenn Sie zwei TextViews drin.
Wenn diese nicht helfen, poste dein crash-log von LogCat und ich kann helfen, Sie von dort aus.
Haben Sie versucht, die änderung der Art der LayoutParams? Und poste bitte deine logcat.
Ich habe versucht die Parameter zu ändern, aber es hat nicht funktioniert. Ich habe meine logcat. Danke.
InformationsquelleAutor Lisa Wray