我正在关注在android studio中实现选项卡滑动视图的教程,它已经很好了。但后来我不得不使用标签滑动用于多个活动。例如,我有一个主要活动,在主要活动中我有3个按钮。点击一下,每个按钮都会打开另一个活动,其中包含4个标签。 主要行为>按钮1>屏幕标签1 2 3 主要活动>按钮2>带标签的屏幕4 5 6 (这些标签1 2 3 4 5 6实际上是活动)
更简单的版本可以在这里找到: http://easyway2in.blogspot.ae/2014/10/android-swipe-views-with-tabs.html
现在,我做的是: 我已经有了main_activity的fragmentpageadapter文件。我创建了fragmentpageadaptertwo用于另一个带有可跳转选项卡的活动。这就是我的fragmentpageadapter.java和fragmentpageadaptertwo.java的样子(BIG QUESTION ::我是否需要去除不同的碎片级别?):
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 in_room_dining();
case 1:
return new laundry();
case 2:
return new house_keeping();
case 3:
return new miscellaneous();
default:
break;
}
return null;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return 4;
}
}
这就是fragmentpagaadpatertwo.java的样子:
package com.appt.shreyabisht.staymax;
/**
* Created by SHREYA BISHT on 11/28/2014.
*/
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class FragmentPageAdaptertwo extends FragmentPagerAdapter {
public FragmentPageAdaptertwo (FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int arg0) {
// TODO Auto-generated method stub
switch (arg0) {
case 0:
return new gym();
case 1:
return new spa();
case 2:
return new recreational();
case 3:
return new events();
default:
break;
}
return null;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return 4;
}
}
会发生什么:当我运行我的应用程序时,它运行正常。主要活动打开。我点击按钮1,它应该带我到一个带有可跳转选项卡的屏幕,但此时应用程序崩溃了。
以下是我在logcat中看到的内容:
11-27 15:17:39.929 885-885/com.appt.shreyabisht.staymax E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.appt.shreyabisht.staymax, PID: 885
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.appt.shreyabisht.staymax/com.appt.shreyabisht.staymax.amn_menu}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
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:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.appt.shreyabisht.staymax.amn_menu.onCreate(amn_menu.java:23)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
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:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
11-27 15:17:43.339 885-885/com.appt.shreyabisht.staymax I/Process﹕ Sending signal. PID: 885 SIG: 9
11-27 15:17:44.949 1103-1103/com.appt.shreyabisht.staymax D/dalvikvm﹕ GC_FOR_ALLOC freed 37K, 9% free 3606K/3940K, paused 103ms, total 105ms
11-27 15:17:45.689 1103-1103/com.appt.shreyabisht.staymax D/﹕ HostConnection::get() New Host Connection established 0xb8d6de20, tid 1103
11-27 15:17:45.749 1103-1103/com.appt.shreyabisht.staymax W/EGL_emulation﹕ eglSurfaceAttrib not implemented
11-27 15:17:45.759 1103-1103/com.appt.shreyabisht.staymax D/OpenGLRenderer﹕ Enabling debug mode 0
(amn_menu.java:23)是 viewpager.setAdapter(FT1);
此时我的项目中有太多活动,如果我的问题不明确,我可以从我的活动中粘贴更多代码。感谢
\\\\\\\编辑\\\\\\\\\\\\\\\
amn_menu.java
package com.appt.shreyabisht.staymax;
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 amn_menu extends FragmentActivity implements ActionBar.TabListener{
ActionBar actionbar;
ViewPager viewpager;
FragmentPageAdaptertwo ft1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_amn_menu);
viewpager = (ViewPager) findViewById(R.id.pager);
ft1= new FragmentPageAdaptertwo (getSupportFragmentManager());
actionbar = getActionBar();
viewpager.setAdapter(ft1);
actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionbar.addTab(actionbar.newTab().setText("Gym").setTabListener(this));
actionbar.addTab(actionbar.newTab().setText("Spa").setTabListener(this));
actionbar.addTab(actionbar.newTab().setText("Recreational").setTabListener(this));
actionbar.addTab(actionbar.newTab().setText("Events").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
}
}
答案 0 :(得分:0)
标识符似乎有错误。
19: setContentView(R.layout.activity_amn_menu);
20: viewpager = (ViewPager) findViewById(R.id.pager); // <<-- here viewpager = null
21: ft1= new FragmentPageAdaptertwo (getSupportFragmentManager());
22: actionbar = getActionBar();
23: viewpager.setAdapter(ft1); // <--- NullPointerException HERE
24 actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
因此, R.id.pager
Layour中不存在 R.layout.activity_amn_menu
。
查看layout/activity_amn_menu.xml
R.id.pager
元素,添加或修改第20行中的名称。