java – Swipe Tab – NullPointerException – Android
•
Android
I followed this tutorial: #127 Android scrollable tabs Part 1: Android tutorial for beginners. I created three clips with three layouts, but I got java.lang.nullpointerexception. I don't know what went wrong
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;
}
}
Here's everything, actionbars, viewpager and adapter. Can someone tell me what went wrong?
Of 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
In fact, the person in the video released the code on guthub. This is the code:
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;
}
}
This code is valid, but there is no actionbar here, only fragments are sliding
resolvent:
Getactionbar() seems to return null. This happens if the topic specifies that the activity should not have an action bar
Try calling
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
Before setcontentview()
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码