TabHost Layout und dynamischer Content

  • Antworten:7
  • Bentwortet
Ben Becker
  • Forum-Beiträge: 209

06.06.2011, 22:37:08 via Website

Hiho!
ich kommt mit einem TabHostLayout nicht weiter. Leider weiß ich auch gar nicht ob das geht wie ich das will. Und zwar würde ich gerne 3 Tabs haben. Auf den ersten beiden Soll eine Tabelle sein die Scrollbar ist und aus einer Datenbank befüllt wird. Auf der dritten ist eine Tabelle mit zwei Zeilen und darunter noch Infos. Geht sowas?
Ich habe mir gedacht, ich mache das TabHostLayout so:

1<?xml version="1.0" encoding="utf-8"?>
2<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
3 android:id="@android:id/tabhost"
4 android:layout_width="fill_parent"
5 android:layout_height="fill_parent">
6 <RelativeLayout
7 android:orientation="vertical"
8 android:layout_width="fill_parent"
9 android:layout_height="fill_parent"
10 android:id="@+id/rl1">
11 <LinearLayout
12 android:orientation="vertical"
13 android:layout_width="fill_parent"
14 android:layout_height="fill_parent"
15 android:id="@+id/ll1">
16 HIER DANN DIE TABELLEN REIN
17 </LinearLayout>
18 <TabWidget
19 android:id="@android:id/tabs"
20 android:layout_width="fill_parent"
21 android:layout_height="wrap_content"
22 android:layout_alignParentBottom="true"/>
23 <FrameLayout
24 android:id="@android:id/tabcontent"
25 android:layout_width="fill_parent"
26 android:layout_height="fill_parent"
27 android:padding="5dp" >
28 </FrameLayout>
29 </RelativeLayout>
30</TabHost>

Ja an die Stelle wo steht "HIER DANN DIE TABELLEN REIN" würde ich gerne die Tabellen einbinden und die Infos. Kann man das Dynamisch machen? Also über die Activity? Will nicht ewig suchen und dabei geht sowas nicht.


Ich habe das natürlich auch schon mal versucht. Einfach in dem ich versuche eine Textview zu ersetzen. Aber das Programm stürzt ab und startet erst gar nicht. Hier mal die class Datei die die textview ändern soll, dann die Tabhost Datei und das Layout:

1package de.sportwettenblogger.de.tabwidget;
2
3import android.app.Activity;
4import android.os.Bundle;
5import android.widget.TextView;
6
7public class ArtistsActivity extends Activity {
8
9 public void onCreate(Bundle savedInstanceState) {
10 super.onCreate(savedInstanceState);
11
12 TextView textview = new TextView(this);
13 textview =(TextView) findViewById(R.id.textView1);
14 textview.setText("This is the Artists tab");
15 //setContentView(textview);
16 }
17 }

1package de.sportwettenblogger.de.tabwidget;
2
3import android.app.TabActivity;
4import android.content.Intent;
5import android.content.res.Resources;
6import android.os.Bundle;
7import android.widget.TabHost;
8
9public class TabWidget extends TabActivity {
10 /** Called when the activity is first created. */
11 @Override
12 public void onCreate(Bundle savedInstanceState) {
13 super.onCreate(savedInstanceState);
14 setContentView(R.layout.main);
15
16 Resources res = getResources(); // Resource object to get Drawables
17 TabHost tabHost = getTabHost(); // The activity TabHost
18 TabHost.TabSpec spec; // Resusable TabSpec for each tab
19 Intent intent; // Reusable Intent for each tab
20
21 // Create an Intent to launch an Activity for the tab (to be reused)
22 intent = new Intent().setClass(this, ArtistsActivity.class);
23
24 // Initialize a TabSpec for each tab and add it to the TabHost
25 spec = tabHost.newTabSpec("artists").setIndicator("Artists",
26 res.getDrawable(R.drawable.ic_tab_artists))
27 .setContent(intent);
28 tabHost.addTab(spec);
29
30 // Do the same for the other tabs
31 intent = new Intent().setClass(this, AlbumsActivity.class);
32 spec = tabHost.newTabSpec("albums").setIndicator("Albums",
33 res.getDrawable(R.drawable.ic_tab_albums))
34 .setContent(intent);
35 tabHost.addTab(spec);
36
37 intent = new Intent().setClass(this, SongsActivity.class);
38 spec = tabHost.newTabSpec("songs").setIndicator("Songs",
39 res.getDrawable(R.drawable.ic_tab_songs))
40 .setContent(intent);
41 tabHost.addTab(spec);
42
43 tabHost.setCurrentTab(1);
44 }
45}

1<?xml version="1.0" encoding="utf-8"?>
2<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
3 android:id="@android:id/tabhost"
4 android:layout_width="fill_parent"
5 android:layout_height="fill_parent">
6 <RelativeLayout
7 android:orientation="vertical"
8 android:layout_width="fill_parent"
9 android:layout_height="fill_parent"
10 android:id="@+id/rl1">
11 <LinearLayout
12 android:orientation="vertical"
13 android:layout_width="fill_parent"
14 android:layout_height="fill_parent"
15 android:id="@+id/ll1">
16 <TextView android:text="TextView" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
17 </LinearLayout>
18 <TabWidget
19 android:id="@android:id/tabs"
20 android:layout_width="fill_parent"
21 android:layout_height="wrap_content"
22 android:layout_alignParentBottom="true"/>
23 <FrameLayout
24 android:id="@android:id/tabcontent"
25 android:layout_width="fill_parent"
26 android:layout_height="fill_parent"
27 android:padding="5dp" >
28 </FrameLayout>
29 </RelativeLayout>
30</TabHost>

Warum stürzt mein Programm ab und geht das überhaupt was ich machen will?

— geändert am 06.06.2011, 23:20:10

Antworten
Markus Gu
  • Forum-Beiträge: 2.644

07.06.2011, 00:01:17 via Website

1. alles geht.

2. wie man es macht ist entscheidend

3. "warum stürtzt meine app ab" hat im entwickler forum nicht zu suchen. das gehört eher ins hellseher forum. => code posten, stacktrace posten. dann kann dir auch geholfen werden.

swordiApps Blog - Website

Antworten
Ben Becker
  • Forum-Beiträge: 209

07.06.2011, 07:50:38 via Website

Den Code habe ich ja schon gepostet. Ich versuche ja alles alleine hinzubekommen und google mir auch viel zusammen. Leider verstehe ich das mit dem Layout nicht so ganz. Ich hoffe du kannst mir dann jetzt helfen, hier ist meine Fehlermeldung. Das ich aus irgendeinem Grund einen NullPointerException habe sehe ich. Ich frag mich nur wieso. Wenn ich in der ArtistsActivity die Zeile 14 auskommentiere ist die Fehlermeldung weg. Also würde ich sagen habe ich dort einen Fehler und die Activity darf nicht auf das Layout zugreifen und findet daher das Textfeld nicht. Aber ich weiß halt nicht weiter. :(

106-07 05:48:46.737: ERROR/AndroidRuntime(286): Uncaught handler: thread main exiting due to uncaught exception
206-07 05:48:46.767: ERROR/AndroidRuntime(286): java.lang.RuntimeException: Unable to start activity ComponentInfo{de.sportwettenblogger.de.tabwidget/de.sportwettenblogger.de.tabwidget.TabWidget}: java.lang.RuntimeException: Unable to start activity ComponentInfo{de.sportwettenblogger.de.tabwidget/de.sportwettenblogger.de.tabwidget.ArtistsActivity}: java.lang.NullPointerException
306-07 05:48:46.767: ERROR/AndroidRuntime(286): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2401)
406-07 05:48:46.767: ERROR/AndroidRuntime(286): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417)
506-07 05:48:46.767: ERROR/AndroidRuntime(286): at android.app.ActivityThread.access$2100(ActivityThread.java:116)
606-07 05:48:46.767: ERROR/AndroidRuntime(286): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
706-07 05:48:46.767: ERROR/AndroidRuntime(286): at android.os.Handler.dispatchMessage(Handler.java:99)
806-07 05:48:46.767: ERROR/AndroidRuntime(286): at android.os.Looper.loop(Looper.java:123)
906-07 05:48:46.767: ERROR/AndroidRuntime(286): at android.app.ActivityThread.main(ActivityThread.java:4203)
1006-07 05:48:46.767: ERROR/AndroidRuntime(286): at java.lang.reflect.Method.invokeNative(Native Method)
1106-07 05:48:46.767: ERROR/AndroidRuntime(286): at java.lang.reflect.Method.invoke(Method.java:521)
1206-07 05:48:46.767: ERROR/AndroidRuntime(286): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
1306-07 05:48:46.767: ERROR/AndroidRuntime(286): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
1406-07 05:48:46.767: ERROR/AndroidRuntime(286): at dalvik.system.NativeStart.main(Native Method)
1506-07 05:48:46.767: ERROR/AndroidRuntime(286): Caused by: java.lang.RuntimeException: Unable to start activity ComponentInfo{de.sportwettenblogger.de.tabwidget/de.sportwettenblogger.de.tabwidget.ArtistsActivity}: java.lang.NullPointerException
1606-07 05:48:46.767: ERROR/AndroidRuntime(286): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2401)
1706-07 05:48:46.767: ERROR/AndroidRuntime(286): at android.app.ActivityThread.startActivityNow(ActivityThread.java:2242)
1806-07 05:48:46.767: ERROR/AndroidRuntime(286): at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
1906-07 05:48:46.767: ERROR/AndroidRuntime(286): at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
2006-07 05:48:46.767: ERROR/AndroidRuntime(286): at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:631)
2106-07 05:48:46.767: ERROR/AndroidRuntime(286): at android.widget.TabHost.setCurrentTab(TabHost.java:317)
2206-07 05:48:46.767: ERROR/AndroidRuntime(286): at android.widget.TabHost.addTab(TabHost.java:210)
2306-07 05:48:46.767: ERROR/AndroidRuntime(286): at de.sportwettenblogger.de.tabwidget.TabWidget.onCreate(TabWidget.java:28)
2406-07 05:48:46.767: ERROR/AndroidRuntime(286): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
2506-07 05:48:46.767: ERROR/AndroidRuntime(286): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
2606-07 05:48:46.767: ERROR/AndroidRuntime(286): ... 11 more
2706-07 05:48:46.767: ERROR/AndroidRuntime(286): Caused by: java.lang.NullPointerException
2806-07 05:48:46.767: ERROR/AndroidRuntime(286): at de.sportwettenblogger.de.tabwidget.ArtistsActivity.onCreate(ArtistsActivity.java:14)
2906-07 05:48:46.767: ERROR/AndroidRuntime(286): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
3006-07 05:48:46.767: ERROR/AndroidRuntime(286): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
3106-07 05:48:46.767: ERROR/AndroidRuntime(286): ... 20 more

Antworten
Gelöschter Account
  • Forum-Beiträge: 5.136

07.06.2011, 08:22:36 via Website

Der Fehler wird Dir mit Zeilennummer im Stacktrace genannt:

06-07 05:48:46.767: ERROR/AndroidRuntime(286): at de.sportwettenblogger.de.tabwidget.ArtistsActivity.onCreate(ArtistsActivity.java:14)

Dort verweist Du auf eine nicht vorhandene Ressource wenn ich das richtig lese:

13 textview =(TextView) findViewById(R.id.textView1);
14 textview.setText("This is the Artists tab");

In Zeile 13 weist Du textview die nicht existierende Ressource R.id.textView1 zu und in Zeile 14 willst Du dem etwas zuweisen.

Ergebnis : NullPointer Exeption ...

Mit anderen Worten .. Du willst einer nicht existierenden Textview einen Text zuweisen.

Versuch einfach mal nach dem Tippen von "R.id" in Zeile 13, in Eclipse "[strg] + [Leertaste]" zu drücken .. dann sollte Eclipse Dir die vorhandenen id's auflisten.

— geändert am 07.06.2011, 08:38:23

lg Voss

Antworten
Ben Becker
  • Forum-Beiträge: 209

07.06.2011, 08:32:37 via Website

Ja das meine ich ja auch schon erkannt haben.

16 <TextView android:text="TextView" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
17 </LinearLayout>

Hier in Zeile 16 im Layout meine ich aber die ID drin zuhaben. Kann es sein, dass ich durch die Activity nicht auf das Layout von der ersten Activityzugreifen kann?

Antworten
Ben Becker
  • Forum-Beiträge: 209

07.06.2011, 14:16:05 via Website

Jörg V.


Versuch einfach mal nach dem Tippen von "R.id" in Zeile 13, in Eclipse "[strg] + [Leertaste]" zu drücken .. dann sollte Eclipse Dir die vorhandenen id's auflisten.

Das habe ich versucht und dann bietet er mir die textView1 an. So schwer kann das doch nicht sein. :(

Antworten
San Blarnoi
  • Forum-Beiträge: 2.545

07.06.2011, 19:04:44 via Website

Dann darf ich auf die Schnelle auch mal einen Versuch wagen:
vor zeile 13 fehlt ein setContentView(), ohne das ein findView... natürlich nix findet.

Antworten
Ben Becker
  • Forum-Beiträge: 209

07.06.2011, 19:15:35 via Website

Das geht ach net. Habe es jetzt anders gemacht.

1setContent(new TabHost.TabContentFactory() {
2
3 public View createTabContent(String tag) {
4
5 textview =(TextView) findViewById(R.id.textView1);
6 textview.setText("This is the Artists tab");
7 return textview;
8 }
9 });

Sieht zwar net so toll aus wenn alles in der TabActivity ist aber keine Ahnung wie es sonst geht.

Antworten