我遵循了本教程:#127 Android Scrollable Tabs Part 1:Android Tutorial For Beginners我创建了三个包含三个布局的片段,但是我得到了java.lang.NullPointerException。我不知道哪里出错了。
MainActivity.java:
public class MainActivity extends FragmentActivity implements TabListener {
ViewPager viewPager;
ActionBar actionBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager) findViewById(R.id.pager);
viewPager.setAdapter(new MyAdapter(getSupportFragmentManager()));
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
// TODO Auto-generated method stub
actionBar.setSelectedNavigationItem(position);
}
@Override
public void onPageScrolled(int position, float arg1, int pixels) {
// TODO Auto-generated method stub
}
@Override
public void onPageScrollStateChanged(int position) {
// TODO Auto-generated method stub
}
});
actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.Tab tab1 = actionBar.newTab();
tab1.setText("Tab 1");
tab1.setTabListener(this);
ActionBar.Tab tab2 = actionBar.newTab();
tab2.setText("Tab 2");
tab2.setTabListener(this);
ActionBar.Tab tab3 = actionBar.newTab();
tab3.setText("Tab 3");
tab3.setTabListener(this);
actionBar.addTab(tab1);
actionBar.addTab(tab2);
actionBar.addTab(tab3);
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
}
class MyAdapter extends FragmentPagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}
@Override
public Fragment getItem(int position) {
// TODO Auto-generated method stub
Fragment fragment = null;
switch(position)
{
case 0:
fragment = new FragmentA();
break;
case 1:
fragment = new FragmentB();
break;
case 2:
fragment = new FragmentC();
break;
}
return fragment;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return 3;
}
}
以下是一切,ActionBars,ViewPager和Adapter。有人能告诉我哪里出错了吗?
logcat的:
05-05 16:38:22.670: E/AndroidRuntime(13118): FATAL EXCEPTION: main
05-05 16:38:22.670: E/AndroidRuntime(13118): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.kiko.swipetabs/com.kiko.swipetabs.MainActivity}: java.lang.NullPointerException
05-05 16:38:22.670: E/AndroidRuntime(13118): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
05-05 16:38:22.670: E/AndroidRuntime(13118): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2099)
05-05 16:38:22.670: E/AndroidRuntime(13118): at android.app.ActivityThread.access$600(ActivityThread.java:138)
05-05 16:38:22.670: E/AndroidRuntime(13118): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
05-05 16:38:22.670: E/AndroidRuntime(13118): at android.os.Handler.dispatchMessage(Handler.java:99)
05-05 16:38:22.670: E/AndroidRuntime(13118): at android.os.Looper.loop(Looper.java:137)
05-05 16:38:22.670: E/AndroidRuntime(13118): at android.app.ActivityThread.main(ActivityThread.java:4929)
05-05 16:38:22.670: E/AndroidRuntime(13118): at java.lang.reflect.Method.invokeNative(Native Method)
05-05 16:38:22.670: E/AndroidRuntime(13118): at java.lang.reflect.Method.invoke(Method.java:511)
05-05 16:38:22.670: E/AndroidRuntime(13118): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:798)
05-05 16:38:22.670: E/AndroidRuntime(13118): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:565)
05-05 16:38:22.670: E/AndroidRuntime(13118): at dalvik.system.NativeStart.main(Native Method)
05-05 16:38:22.670: E/AndroidRuntime(13118): Caused by: java.lang.NullPointerException
05-05 16:38:22.670: E/AndroidRuntime(13118): at com.kiko.swipetabs.MainActivity.onCreate(MainActivity.java:44)
05-05 16:38:22.670: E/AndroidRuntime(13118): at android.app.Activity.performCreate(Activity.java:5254)
05-05 16:38:22.670: E/AndroidRuntime(13118): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1082)
05-05 16:38:22.670: E/AndroidRuntime(13118): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2038)
05-05 16:38:22.670: E/AndroidRuntime(13118): ... 11 more
实际上视频中的人在GutHub上发布了代码。这是代码:
public class MainActivity extends FragmentActivity {
ViewPager viewPager=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager= (ViewPager) findViewById(R.id.pager);
FragmentManager fragmentManager=getSupportFragmentManager();
viewPager.setAdapter(new MyAdapter(fragmentManager));
}
}
class MyAdapter extends FragmentStatePagerAdapter
{
public MyAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int i) {
Fragment fragment=null;
// Log.d("VIVZ", "get Item is called "+i);
if(i==0)
{
fragment=new FragmentA();
}
if(i==1)
{
fragment=new FragmentB();
}
if(i==2)
{
fragment=new FragmentC();
}
return fragment;
}
@Override
public int getCount() {
// Log.d("VIVZ", "get Count is called");
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
if(position==0)
{
return "Tab 1";
}
if(position==1)
{
return "Tab 2";
}
if(position==2)
{
return "Tab 3";
}
return null;
}
}
此代码有效,但此处标题上没有ActionBar,只有片段正在滑动。
答案 0 :(得分:3)
您的Android SDK版本(<uses-sdk android:minSdkVersion="11" />
)必须高于11。
您的主题必须有一个像
这样的操作栏android:theme="Theme.AppCompat"
请勿在您的活动中使用此代码:
requestWindowFeature(Window.FEATURE_NO_TITLE);
而不是import android.support.v4.app.ActionBar
在您的活动中使用import android.support.v7.app.ActionBar
。
将actionBar=getActionBar();
更改为actionbar=getSupportActionBar();
。
答案 1 :(得分:0)
getActionBar()
似乎返回null。如果主题指定Activity不应该有Action Bar,则会发生这种情况。
尝试拨打
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
在setContentView()
之前。