编辑:这是我所遵循的教程,以及它对应的YouTube视频:
http://easyway2in.blogspot.ae/2014/10/android-swipe-views-with-tabs.html
https://www.youtube.com/watch?v=Xu2yoIqTsj4
这是我在Android应用程序中使用选项卡的第一次体验,我似乎遇到了一个我似乎无法修复的错误。
这是错误:
10-29 21:46:50.928 27935-27935/tabs.ibettergetagoodgradeforthisorillbepissed.sciencefair.beta.mmmeds.com.mmmedstabbeta E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: tabs.ibettergetagoodgradeforthisorillbepissed.sciencefair.beta.mmmeds.com.mmmedstabbeta, PID: 27935
java.lang.NullPointerException
at android.support.v4.app.BackStackRecord.doAddOp(BackStackRecord.java:394)
at android.support.v4.app.BackStackRecord.add(BackStackRecord.java:389)
at android.support.v4.app.FragmentPagerAdapter.instantiateItem(FragmentPagerAdapter.java:99)
at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:832)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1048)
at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:550)
at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:509)
at android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:490)
at tabs.ibettergetagoodgradeforthisorillbepissed.sciencefair.beta.mmmeds.com.mmmedstabbeta.MainActivity.onTabSelected(MainActivity.java:61)
at com.android.internal.app.ActionBarImpl.selectTab(ActionBarImpl.java:577)
at com.android.internal.app.ActionBarImpl$TabImpl.select(ActionBarImpl.java:1105)
at com.android.internal.widget.ScrollingTabContainerView$TabClickListener.onClick(ScrollingTabContainerView.java:547)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5034)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1270)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1086)
at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
at dalvik.system.NativeStart.main(Native Method)
这是我的主要课程
package tabs.ibettergetagoodgradeforthisorillbepissed.sciencefair.beta.mmmeds.com.mmmedstabbeta;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
public class MainActivity extends FragmentActivity implements ActionBar.TabListener{
ActionBar actionbar;
ViewPager viewpager;
FragmentPageAdapter ft;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewpager = (ViewPager) findViewById(R.id.pager);
ft = new FragmentPageAdapter(getSupportFragmentManager());
actionbar = getActionBar();
viewpager.setAdapter(ft);
actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionbar.addTab(actionbar.newTab().setText("Add Medication").setTabListener(this));
actionbar.addTab(actionbar.newTab().setText("View Medication").setTabListener(this));
viewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
actionbar.setSelectedNavigationItem(arg0);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
});
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
viewpager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
}
我的主要活动XML:
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
我的FragmentPageAdapter类:
package tabs.ibettergetagoodgradeforthisorillbepissed.sciencefair.beta.mmmeds.com.mmmedstabbeta;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class FragmentPageAdapter extends FragmentPagerAdapter {
public FragmentPageAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int arg0) {
// TODO Auto-generated method stub
switch (arg0) {
case 0:
return new AddMedFragment();
case 1:
return new ViewMedFragment();
default:
break;
}
return null;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return 3;
}
}
以及其中一个显示片段的示例(它们现在完全相同):
package tabs.ibettergetagoodgradeforthisorillbepissed.sciencefair.beta.mmmeds.com.mmmedstabbeta;
/**
* Created by WindowsSucks on 10/29/2014.
*/
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class AddMedFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
return inflater.inflate(R.layout.add_med_layout, container,false);
}
}
以及相应的XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="42dp"
android:text="Welcome to Add Med page"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
非常感谢你们
答案 0 :(得分:0)
我能够通过更改FragmentPageAdapter类的getCount()方法来解决这个问题。它设置为返回3(如上所示),但只应设置为返回2,因为这是我当前设置的选项卡数。
代码:
package tabs.ibettergetagoodgradeforthisorillbepissed.sciencefair.beta.mmmeds.com.mmmedstabbeta;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class FragmentPageAdapter extends FragmentPagerAdapter {
public FragmentPageAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int arg0) {
// TODO Auto-generated method stub
switch (arg0) {
case 0:
return new AddMedFragment();
case 1:
return new ViewMedFragment();
default:
break;
}
return null;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return 2;
}
}