(TabHost)findViewById(...)返回NULL,Android的
我设置(TabHost)findViewById(...)返回NULL,Android的
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;
}
}
摇篮
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"/>
您activity_main.xml
不声明android:id="@+id/tabhost"
,只是android:id="@+id/pager"
与ViewPager
。 findViewById
只给你,在setContentView(R.layout.activity_main)
我需要更改哪些内容才能访问标签?我希望能够找到我正在使用哪个标签,并且能够切换标签页。 – Birrel 2015-02-06 07:46:05
使用'PagerTabStrip'(发现一些提示https://gist.github.com/pboos/4535406)或第三个库'ViewPagerIndicator' http://viewpagerindicator.com/ – ytRino 2015-02-06 07:52:04
这是由最新版本Android Studio创建新项目时(除将SectionsPagerAdapter移动到其自己的类文件外)。为什么这是一个如此艰难的任务,对于自动生成的代码。 – Birrel 2015-02-06 08:00:31
[http://*.com/questions/18703962/actionbaractivity-with-tabhost](http://*.com/questions/18703962/actionbaractivity-with-设置意见tabhost) – 2015-02-06 07:28:55
[http://www.androidbegin.com/tutorial/implementing-fragment-tabs-in-android/](http://www.androidbegin.com/tutorial/implementing-fragment-tabs-in-android/ ) – 2015-02-06 07:29:50
发布您的activity_main.xml。 – 2015-02-06 07:31:38