Pergunta

Atualmente, estou tentando o Android 1.5 SDK e eu vi um par de exemplos sobre TabHost. O que estou tentando fazer é usar um botão diferente em cada guia para fazer suas tarefas.

O que eu tentei foi usando onClickListiner () e onClick (). Eu acho que isso é o que todos os desenvolvedores usar, mas eu continuo recebendo uma exceção nula na LogCat cada vez que o botão é pressionado. Também tenho cada layout XML para que eu ligue para o Tab como: tab.add (... setContent (R.id.firstTabLayout))

firstTabLayout = layout for Button and TextView.

Qual seria a melhor maneira de fazer um trabalho de botão / TextView adequadamente sob a TabHost?

Foi útil?

Solução

Eu não sou inteiramente certo onde seu problema é, mas isso é como eu configurar minhas atividades com guias antes

layout.xml

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

<FrameLayout android:id="@android:id/tabcontent"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <LinearLayout android:id="@+id/tab1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <TextView android:id="@android:id/text"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />

    </LinearLayout>
    <LinearLayout android:id="@+id/tab2"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <TextView android:id="@android:id/text"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />

    </LinearLayout>
    <LinearLayout android:id="@+id/tab3"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <TextView android:id="@android:id/text"
            android:layout_width="fill_parent"
                android:layout_height="wrap_content" />

    </LinearLayout>
</FrameLayout>

Tab.java

public class InitialActivity extends TabActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout);

        TabHost th = getTabHost();
        th.addTab(th.newTabSpec("tab_1").setIndicator("Tab1").setContent(R.id.tab1));
        th.addTab(th.newTabSpec("tab_2").setIndicator("Tab2").setContent(R.id.tab2));
        th.addTab(th.newTabSpec("tab_3").setIndicator("Tab3").setContent(R.id.tab3));
    }
}

Em seguida, você pode apenas usar findViewById () em todas as vistas dentro das guias, ou se não são compartilhados nomes entre eles, você pode fazer findViewById(R.id.tab1).findViewById(R.id.text)

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top