문제

나는 현재 Android 1.5 SDK를 시도하고 있으며 Tabhost에서 몇 가지 예를 보았습니다. 내가하려는 것은 각 탭에서 다른 버튼을 사용하여 작업을 수행하는 것입니다.

내가 시도한 것은 onclickListiner ()와 onclick ()를 사용하는 것이 었습니다. 나는 이것이 모든 개발자가 사용하는 것이라고 생각하지만 버튼을 누를 때마다 Logcat에서 널 예외를 계속 얻습니다. 또한 각 XML 레이아웃이 있으므로 탭을 tab.add (... setContent (r.id.firsttablayout)로 호출합니다.

firstTabLayout = layout for Button and TextView.

Tabhost에서 버튼/텍스트 뷰를 제대로 작동시키는 가장 좋은 방법은 무엇입니까?

도움이 되었습니까?

해결책

나는 당신의 문제가 어디에 있는지 확실하지 않지만 이것이 내가 전에 내 탭 활동을 설정하는 방법입니다.

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));
    }
}

그런 다음 탭 내의 뷰에서 FindViewByid ()를 사용할 수 있거나 그 사이에 공유 이름이 있으면 수행 할 수 있습니다. findViewById(R.id.tab1).findViewById(R.id.text)

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top