【问题标题】:(TabHost) findViewById(...) returns NULL, Android(TabHost) findViewById(...) 返回 NULL,Android
【发布时间】:2015-02-06 07:27:46
【问题描述】:

我设置

tabHost = (TabHost) findViewBiId(android.R.id.tabhost);

结果为 NULL。

我有一个MainActivity 扩展ActionBarActivity 并实现ActionBar.TabListener,如下...

MainActivity

    public class MainActivity extends ActionBarActivity implements ActionBar.TabListener {

        SectionsPagerAdapter mSectionsPagerAdapter;
        ViewPager mViewPager;

        public static TabHost tabHost;

        private SharedPreferences mSharedPreferences;

        protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tabHost = (TabHost) findViewById(android.R.id.tabhost);

        // Set up the action bar.
        final ActionBar actionBar = getSupportActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        mSectionsPagerAdapter = new SectionsPagerAdapter(this, getSupportFragmentManager());

        // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);

        mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                actionBar.setSelectedNavigationItem(position);
            }
        });

        for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
            actionBar.addTab(
                    actionBar.newTab()
                            .setText(mSectionsPagerAdapter.getPageTitle(i))
                            .setTabListener(this));
        }
    }

    ... other methods ...
}

但是当我尝试使用 tabHost 时,我得到了 NullPointerException。检查tabHost 表明它实际上是空的。

即使是简单的tabHost.getCurrentTab() 也会使整个事情崩溃。我希望能够以编程方式切换选项卡,但它不起作用。我已经经历了几十个线程,但似乎没有任何结果。

SectionsPagerAdapter

import java.util.Locale;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.widget.TabHost;

public class SectionsPagerAdapter extends FragmentPagerAdapter {

    protected Context mContext;

    public SectionsPagerAdapter(Context context, FragmentManager fm) {
        super(fm);
        mContext = context;
    }

    @Override
    public Fragment getItem(int position) {
        switch(position){
            case 0:
                return new PunchCardFragment();
            case 1:
                return new CalendarFragment();
        }
        return null;
    }

    @Override
    public int getCount() {
        return 2;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        Locale l = Locale.getDefault();
        switch (position) {
            case 0:
                return mContext.getString(R.string.title_section1).toUpperCase(l);
            case 1:
                return mContext.getString(R.string.title_section2).toUpperCase(l);
        }
        return null;
    }
}

Gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    defaultConfig {
        applicationId "..."
        minSdkVersion 14
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:21.0.3'
}

activity_main xml

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"/>

【问题讨论】:

标签: android android-actionbar android-tabhost findviewbyid android-actionbaractivity


【解决方案1】:

您的activity_main.xml 没有声明android:id="@+id/tabhost",只是android:id="@+id/pager"ViewPager
findViewById 只为您提供setContentView(R.layout.activity_main) 中设置的视图

【讨论】:

  • 我需要更改哪些内容才能访问这些选项卡?我希望能够找到我所在的标签,并能够切换标签。
  • 使用PagerTabStrip(找到一些提示gist.github.com/pboos/4535406)或第三库ViewPagerIndicatorviewpagerindicator.com
  • 这是最新版本的 Android Studio 在创建新项目时生成的库存代码(除了将 SectionsPagerAdapter 移动到自己的类文件中)。对于自动生成的代码,为什么这是一项如此艰巨的任务。
  • 我怀疑你编辑了一些代码。带有 ViewPager 的模板项目不包含 tabhost。
  • 我添加了这一点,以尝试访问选项卡。就像我说的,我需要知道我在哪一个,并且能够以编程方式在它们之间切换。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-16
  • 1970-01-01
相关资源
最近更新 更多