Android TabHost mit Fragments

  • Antworten:1
Audioline
  • Forum-Beiträge: 18

14.01.2016, 19:54:05 via Website

Hi,

ich bin grade dabei eine einfache App mit 3 Tabs zu schreiben. Diese sollen dann erstmal jeweils die gleichen Fragmente anzeigen. Soweit funktioniert das auch, nur habe ich das Problem, das die ActivityMain.xml nicht mehr gerendert werden kann, aufgrund des folgenden Fehlers:
Rendering Problems Exception raised during rendering: No tab known for tag null (Details)
(java.lang.IllegalStateException: No tab known for tag null)

Ich sehe also keine neuen Elemente auf dem Layout.
Soviel ich bis jetzt weiß, hat das wohl was mit der OnCreateView Methode zu tun. Hier wird wohl der TabHost zu spät initialisiert. Ich finde allerdings keine Lösung, die noch aktuell ist und klappt. Da ich mich hiermit auch noch nicht auskenne, brauche ich nun Hilfe von extern.

Kann mir eine sagen, was ich abändern muss? Und wieso gibt es hier nur Probleme beim rendern?

MainActivity

package com.example.XXX.XXX;

import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TabHost;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;

public class MainActivity extends FragmentActivity {
private FragmentTabHost mTabHost;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
    mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
    mTabHost.setup(this, getSupportFragmentManager(), android.R.id.tabcontent);

    mTabHost.addTab(
            mTabHost.newTabSpec("tab1").setIndicator("Tab 1", null),
            SecondActivity.class, null);
    mTabHost.addTab(
            mTabHost.newTabSpec("tab2").setIndicator("Tab 2", null),
            SecondActivity.class, null);
    mTabHost.addTab(
            mTabHost.newTabSpec("tab3").setIndicator("Tab 3", null),
            SecondActivity.class, null);
}

}

Activity_main.xml

<android.support.v4.app.FragmentTabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >

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

    <TabWidget
        android:id="@android:id/tabs"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"/>

    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />
</LinearLayout>

SecondActivity

package com.example.XXX.XXX;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTabHost;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class SecondActivity extends Fragment {
private FragmentTabHost mTabHost;
@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_layout, container, false);
    return rootView;
    // Create FragmentTabHost
    /*View v = inflater.inflate(R.layout.fragment_layout, container, false);
    TextView tv = (TextView) v.findViewById(R.id.text);
    // tv.setText(this.getTag() + " Content");
    return v;*/
}

}

Fragment_layout.xml

<?xml version="1.0" encoding="utf-8"?>

xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
android:background="#eaecee">

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="@string/deine_Angebote"
        android:id="@+id/textView"
        android:gravity="top|center_vertical"
        android:layout_gravity="right" />

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageButton"
        android:layout_gravity="right"
        android:src="@android:drawable/sym_def_app_icon" />

</LinearLayout>

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

    <ScrollView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/scrollView" >

    </ScrollView>
</LinearLayout>

Antworten
Pascal P.
  • Admin
  • Forum-Beiträge: 11.286

14.01.2016, 20:07:32 via App

Den TabHost stört es, das du ihm Null werte übergibst. Nimm dafür Views oder halt das Erwartete:
http://www.java2s.com/Code/Android/UI/AddTabtoTabHost.htm

Zudem ist das TabHost seit Android L und Material Design nicht mehr der beste.
Lieber ein ViewPager mit TabView. Dann kannst du auch die google design lib nutzen.

LG Pascal //It's not a bug, it's a feature. :) ;)

Antworten