Android无法更改标签图标

时间:2015-02-04 12:59:41

标签: android android-tabhost android-fragmentactivity android-tabs

在我的Android应用程序中,我正在使用FragmentTabHost。我在tabhost中添加了3个选项卡,现在我的问题是,当选择每个选项卡时,我无法动态更改选项卡图标。

我的要求是,如果我选择一个标签,则应更改其他标签图标,我在尝试更改标签图标时获得了null pointer exception。这是我得到空指针异常的行。 onTabChanged method

下的以下代码
ImageView v1 = (ImageView) mTabHost.getTabWidget().getChildAt(0)
                    .findViewById(android.R.id.icon); 

v1 is return null (checked by if condition)

    public class MainActivity extends FragmentActivity implements
            OnTabChangeListener {

        private static final String TAB_1_TAG = "FindTab";
        private static final String TAB_2_TAG = "AddPopUpTab";
        private static final String TAB_3_TAG = "MyAccoutTab";

        private FragmentTabHost mTabHost;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            InitView();
        }

        private void InitView() {
            mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
            mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);

            mTabHost.addTab(
                    setIndicator1(MainActivity.this,
                            mTabHost.newTabSpec(TAB_1_TAG), R.drawable.tab1),
                    Tab1Container.class, null);
            mTabHost.addTab(
                    setIndicator1(MainActivity.this,
                            mTabHost.newTabSpec(TAB_2_TAG), R.drawable.tab2),
                    Tab2Container.class, null);
            mTabHost.addTab(
                    setIndicator1(MainActivity.this,
                            mTabHost.newTabSpec(TAB_3_TAG), R.drawable.tab3),
                    Tab3Container.class, null);

            mTabHost.getTabWidget().setDividerDrawable(null);
            mTabHost.setOnTabChangedListener(MainActivity.this);

        }
    private TabSpec setIndicator1(Context ctx, TabSpec spec, int genresIcon) {
        View v = LayoutInflater.from(ctx).inflate(R.layout.tab_item, null);
        ImageView img = (ImageView) v.findViewById(R.id.img_tabtxt);
        img.setBackgroundResource(genresIcon);
        return spec.setIndicator(v);
    }

    @Override
    public void onTabChanged(String arg0) {
        Log.e("tab change string", arg0);
                    ImageView v1 = (ImageView) mTabHost.getTabWidget().getChildAt(0)
                    .findViewById(android.R.id.icon);
            if (v1 == null) {
                Log.e("v1", "null");  // Line 
            } else {
                Log.e("v1", " Not null");
            }

            if (getResources().getDrawable(R.drawable.tab3) == null) {
                Log.e("resource", "null");
            } else {
                Log.e("resource", "not null");
            }

            v1.setImageDrawable(getResources().getDrawable(R.drawable.tab3));

            ImageView v2 = (ImageView) mTabHost.getTabWidget().getChildAt(1)
                    .findViewById(android.R.id.icon);
            v2.setImageDrawable(getResources().getDrawable(R.drawable.tab1));

            ImageView v3 = (ImageView) mTabHost.getTabWidget().getChildAt(2)
                    .findViewById(android.R.id.icon);
            v3.setImageDrawable(getResources().getDrawable(R.drawable.tab2));
    }

    }

Layout for tab item:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <ImageView
            android:id="@+id/img_tabtxt"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="5dp"
            android:layout_centerHorizontal="true" />

</RelativeLayout>

0 个答案:

没有答案