Rafael T
- Forum-Beiträge: 4
03.02.2011, 18:20:41 via Website
03.02.2011 18:20:41 via Website
Hallo,
Ich habe folgendes Problem: Und zwar habe ich eine WebView, welche mir HTML Text aus einer "raw" Resource darstellen soll.
Wenn ich diese WebView nun mit Daten fülle und sie meiner Activity per setContentView(myWebView) übergebe, so wird diese auch korrekt dargestellt.
Nun brauche ich aber 2 dieser Views mit unterschiedlichem Content. Deshalb habe ich mir eine TabActivity zusammengebaut, welche über TabContentFactory eben jene beiden WebViews zugewiesen bekommt.
Mein Problem: Kein compile-error, keine Warnung aber nix ausser einem leeren TabContent.
Ausschnitte aus meinem Code:
XML
Activity:
Wäre für jede Idee dankbar
ps: Wenn mir jemand sagt, WIE man hier im Forum explizit Java und XML Code einfügt, werde ich das gerne tun. hab aber nur "[code]" gefunden
Ich habe folgendes Problem: Und zwar habe ich eine WebView, welche mir HTML Text aus einer "raw" Resource darstellen soll.
Wenn ich diese WebView nun mit Daten fülle und sie meiner Activity per setContentView(myWebView) übergebe, so wird diese auch korrekt dargestellt.
Nun brauche ich aber 2 dieser Views mit unterschiedlichem Content. Deshalb habe ich mir eine TabActivity zusammengebaut, welche über TabContentFactory eben jene beiden WebViews zugewiesen bekommt.
Mein Problem: Kein compile-error, keine Warnung aber nix ausser einem leeren TabContent.
Ausschnitte aus meinem Code:
XML
1<LinearLayout
2 xmlns:android="http://schemas.android.com/apk/res/android"
3 android:id="@+id/information_parent_layout"
4 android:layout_width="fill_parent"
5 android:layout_height="wrap_content"
6 android:orientation="vertical">
7 <include layout="@layout/header" />
8 <TabHost
9 android:id="@android:id/tabhost"
10 android:layout_width="fill_parent"
11 android:layout_height="wrap_content">
12 <LinearLayout
13 android:id="@+id/information_tab_linear"
14 android:layout_width="fill_parent"
15 android:layout_height="wrap_content">
16 <TabWidget
17 android:id="@android:id/tabs"
18 android:layout_width="fill_parent"
19 android:layout_height="wrap_content"/>
20 <FrameLayout
21 android:id="@android:id/tabcontent"
22 android:layout_width="fill_parent"
23 android:layout_height="wrap_content">
24 <ScrollView
25 android:id="@+id/information_scrollview"
26 android:layout_width="fill_parent"
27 android:layout_height="wrap_content">
28 <LinearLayout
29 android:layout_width="fill_parent"
30 android:layout_height="fill_parent"
31 android:orientation="vertical">
32 <WebView
33 android:id="@+id/information_webview"
34 android:layout_width="fill_parent"
35 android:layout_height="fill_parent"/>
36 </LinearLayout>
37 </ScrollView>
38 </FrameLayout>
39 </LinearLayout>
40 </TabHost>
41</LinearLayout>
2 xmlns:android="http://schemas.android.com/apk/res/android"
3 android:id="@+id/information_parent_layout"
4 android:layout_width="fill_parent"
5 android:layout_height="wrap_content"
6 android:orientation="vertical">
7 <include layout="@layout/header" />
8 <TabHost
9 android:id="@android:id/tabhost"
10 android:layout_width="fill_parent"
11 android:layout_height="wrap_content">
12 <LinearLayout
13 android:id="@+id/information_tab_linear"
14 android:layout_width="fill_parent"
15 android:layout_height="wrap_content">
16 <TabWidget
17 android:id="@android:id/tabs"
18 android:layout_width="fill_parent"
19 android:layout_height="wrap_content"/>
20 <FrameLayout
21 android:id="@android:id/tabcontent"
22 android:layout_width="fill_parent"
23 android:layout_height="wrap_content">
24 <ScrollView
25 android:id="@+id/information_scrollview"
26 android:layout_width="fill_parent"
27 android:layout_height="wrap_content">
28 <LinearLayout
29 android:layout_width="fill_parent"
30 android:layout_height="fill_parent"
31 android:orientation="vertical">
32 <WebView
33 android:id="@+id/information_webview"
34 android:layout_width="fill_parent"
35 android:layout_height="fill_parent"/>
36 </LinearLayout>
37 </ScrollView>
38 </FrameLayout>
39 </LinearLayout>
40 </TabHost>
41</LinearLayout>
Activity:
1public class InformationActivity extends TabActivity {
2
3 TabHost mTabHost;
4 WebView mContactView;
5 WebView mImpressumView;
6
7 public void onCreate(Bundle savedInstanceState){
8 super.onCreate(savedInstanceState);
9
10// showWebView();
11 prepareTabScreen();
12
13
14 }
15
16 private void showWebView(){
17 mContactView = new WebView(this);
18 String data = getHtmlStringFromResourceID(R.raw.contact);
19 mContactView.loadData(data, "text/html", "utf-8");
20 setContentView(mContactView);
21 }
22
23 private void prepareTabScreen(){
24
25 setContentView(R.layout.information);
26
27 mTabHost = getTabHost();
28 mTabHost.setup();
29
30 String contactIndicator = getResources().getString(R.string.tab_info_contact);
31 TabSpec spec = mTabHost.newTabSpec("contact");
32 spec.setIndicator(contactIndicator);
33 spec.setContent(new TabContentFactory() {
34 @Override
35 public View createTabContent(String tag) {
36 mContactView = (WebView)findViewById(R.id.information_webview);
37// mContactView = new WebView(InformationActivity.this);
38 LayoutParams params = new LayoutParams();
39 params.height=LayoutParams.FILL_PARENT;
40 params.width=LayoutParams.FILL_PARENT;
41 mContactView.setLayoutParams(params);
42 String data = getHtmlStringFromResourceID(R.raw.contact);
43 mContactView.loadData(data, "text/html", "utf-8");
44 return mContactView;
45 }
46 });
47 mTabHost.addTab(spec);
48
49 String impressumIndicator = getResources().getString(R.string.tab_info_imp);
50 spec = mTabHost.newTabSpec("imperessum");
51 spec.setIndicator(impressumIndicator);
52 spec.setContent(new TabContentFactory() {
53 @Override
54 public View createTabContent(String tag) {
55 mImpressumView = (WebView)findViewById(R.id.information_webview);
56 String data = getHtmlStringFromResourceID(R.raw.impressum);
57 mImpressumView.loadData(data, "text/html", "utf-8");
58 return mImpressumView;
59 }
60 });
61 mTabHost.addTab(spec);
62
63 mTabHost.setCurrentTab(0);
64 }
65
66 public String getHtmlStringFromResourceID(int ResourceID){
67 String ret = "";
68 String thisLine;
69
70 try {
71
72 InputStream html = getResources().openRawResource(ResourceID);
73 InputStreamReader inputReader = new InputStreamReader(html);
74 BufferedReader reader = new BufferedReader(inputReader);
75
76 while ((thisLine = reader.readLine()) != null) {
77 ret += thisLine;
78
79 }
80 inputReader.close();
81 reader.close();
82
83 } catch (NotFoundException e) {
84 // TODO Auto-generated catch block
85 e.printStackTrace();
86 } catch (IOException e) {
87 // TODO Auto-generated catch block
88 e.printStackTrace();
89 }
90
91 return ret;
92 }
93}
Wenn ich TabActivity zur Activity mache prepareTabScreen auskommentiere und showWebView wieder reinsetzte, geht alles einwandfrei, andersrum nicht...2
3 TabHost mTabHost;
4 WebView mContactView;
5 WebView mImpressumView;
6
7 public void onCreate(Bundle savedInstanceState){
8 super.onCreate(savedInstanceState);
9
10// showWebView();
11 prepareTabScreen();
12
13
14 }
15
16 private void showWebView(){
17 mContactView = new WebView(this);
18 String data = getHtmlStringFromResourceID(R.raw.contact);
19 mContactView.loadData(data, "text/html", "utf-8");
20 setContentView(mContactView);
21 }
22
23 private void prepareTabScreen(){
24
25 setContentView(R.layout.information);
26
27 mTabHost = getTabHost();
28 mTabHost.setup();
29
30 String contactIndicator = getResources().getString(R.string.tab_info_contact);
31 TabSpec spec = mTabHost.newTabSpec("contact");
32 spec.setIndicator(contactIndicator);
33 spec.setContent(new TabContentFactory() {
34 @Override
35 public View createTabContent(String tag) {
36 mContactView = (WebView)findViewById(R.id.information_webview);
37// mContactView = new WebView(InformationActivity.this);
38 LayoutParams params = new LayoutParams();
39 params.height=LayoutParams.FILL_PARENT;
40 params.width=LayoutParams.FILL_PARENT;
41 mContactView.setLayoutParams(params);
42 String data = getHtmlStringFromResourceID(R.raw.contact);
43 mContactView.loadData(data, "text/html", "utf-8");
44 return mContactView;
45 }
46 });
47 mTabHost.addTab(spec);
48
49 String impressumIndicator = getResources().getString(R.string.tab_info_imp);
50 spec = mTabHost.newTabSpec("imperessum");
51 spec.setIndicator(impressumIndicator);
52 spec.setContent(new TabContentFactory() {
53 @Override
54 public View createTabContent(String tag) {
55 mImpressumView = (WebView)findViewById(R.id.information_webview);
56 String data = getHtmlStringFromResourceID(R.raw.impressum);
57 mImpressumView.loadData(data, "text/html", "utf-8");
58 return mImpressumView;
59 }
60 });
61 mTabHost.addTab(spec);
62
63 mTabHost.setCurrentTab(0);
64 }
65
66 public String getHtmlStringFromResourceID(int ResourceID){
67 String ret = "";
68 String thisLine;
69
70 try {
71
72 InputStream html = getResources().openRawResource(ResourceID);
73 InputStreamReader inputReader = new InputStreamReader(html);
74 BufferedReader reader = new BufferedReader(inputReader);
75
76 while ((thisLine = reader.readLine()) != null) {
77 ret += thisLine;
78
79 }
80 inputReader.close();
81 reader.close();
82
83 } catch (NotFoundException e) {
84 // TODO Auto-generated catch block
85 e.printStackTrace();
86 } catch (IOException e) {
87 // TODO Auto-generated catch block
88 e.printStackTrace();
89 }
90
91 return ret;
92 }
93}
Wäre für jede Idee dankbar
ps: Wenn mir jemand sagt, WIE man hier im Forum explizit Java und XML Code einfügt, werde ich das gerne tun. hab aber nur "[code]" gefunden
Empfohlener redaktioneller Inhalt
Mit Deiner Zustimmung wird hier ein externer Inhalt geladen.
Mit Klick auf den oben stehenden Button erklärst Du Dich damit einverstanden, dass Dir externe Inhalte angezeigt werden dürfen. Dabei können personenbezogene Daten an Drittanbieter übermittelt werden. Mehr Infos dazu findest Du in unserer Datenschutzerklärung.