只有在我开始在项目中使用片段后才会出现错误,因为我想提供JsON提要.在我创建Tabs,NavigationDrawer等之前没事.
MainActivity.java
公共类MainActivity扩展ActionBaractivity实现MaterialTabListener {
private Toolbar mToolbar;private NavigationDrawerFragment mDrawerFragment;private DrawerLayout mDrawerLayout;private MaterialTabHost mTabHost;private VIEwPager mVIEwPager;public static final int SERVICES_ALL = 1;public static final int SERVICES_FEATURED = 2;public static final int SERVICES_NEW = 3;@OverrIDeprotected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main_appbar); mToolbar = (Toolbar) findVIEwByID(R.ID.app_bar); setSupportActionbar(mToolbar); getSupportActionbar().setdisplayShowHomeEnabled(true); mDrawerLayout = (DrawerLayout) findVIEwByID(R.ID.drawer_layout); mDrawerFragment = (NavigationDrawerFragment) getSupportFragmentManager().findFragmentByID(R.ID.fragment_navigation_drawer); mDrawerFragment.setUp(R.ID.fragment_navigation_drawer,mDrawerLayout,mToolbar); mTabHost = (MaterialTabHost) findVIEwByID(R.ID.materialTabHost); mVIEwPager = (VIEwPager) findVIEwByID(R.ID.vIEwPager); MyPagerAdapter adapter = new MyPagerAdapter(getSupportFragmentManager()); mVIEwPager.setAdapter(adapter); mVIEwPager.setonPagechangelistener(new VIEwPager.SimpleOnPagechangelistener(){ @OverrIDe public voID onPageSelected(int position) { mTabHost.setSelectednavigationItem(position); } }); for(int i=0; i<adapter.getCount(); i++ ){ mTabHost.addTab( mTabHost.newTab(). setText(adapter.getPageTitle(i)). setTabListener(this) ); }}@OverrIDepublic voID onTabSelected(MaterialTab materialTab) { mVIEwPager.setCurrentItem(materialTab.getposition());}@OverrIDepublic voID onTabReselected(MaterialTab materialTab) {}@OverrIDepublic voID onTabUnselected(MaterialTab materialTab) {}private class MyPagerAdapter extends FragmentStatePagerAdapter{ FragmentManager fragmentManager; public MyPagerAdapter(FragmentManager fm) { super(fm); fragmentManager = fm; } @OverrIDe public Fragment getItem(int num) { Fragment fragment = null; switch (num){ case SERVICES_ALL: fragment = FragmentAll.newInstance("",""); break; case SERVICES_FEATURED: fragment = FragmentFeatured.newInstance("",""); break; case SERVICES_NEW: fragment = FragmentNew.newInstance("",""); break; } return fragment; } @OverrIDe public int getCount() { return 3; } public CharSequence getPageTitle(int position){ return getResources().getStringArray(R.array.tabs)[position]; }}@OverrIDepublic boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu_main,menu); return true;}@OverrIDepublic boolean onoptionsItemSelected(MenuItem item) { int ID = item.getItemID(); if (ID == R.ID.action_settings) { Toast.makeText(this,"hey you just hit " + item.getTitle(),Toast.LENGTH_LONG).show(); return true; } if (ID == R.ID.action_next) { startActivity(new Intent(this,DetailActivity.class)); } return super.onoptionsItemSelected(item);}
}
FragmentAll.java(其他2个片段与FragmentAll类似.)
公共类FragmentAll扩展Fragment {
private static final String ARG_ParaM1 = "param1";private static final String ARG_ParaM2 = "param2";private String mParam1;private String mParam2;public static FragmentAll newInstance(String param1,String param2) { FragmentAll fragment = new FragmentAll(); Bundle args = new Bundle(); args.putString(ARG_ParaM1,param1); args.putString(ARG_ParaM2,param2); fragment.setArguments(args); return fragment;}public FragmentAll() { // required empty public constructor}@OverrIDepublic voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (getArguments() != null) { mParam1 = getArguments().getString(ARG_ParaM1); mParam2 = getArguments().getString(ARG_ParaM2); }}@OverrIDepublic VIEw onCreateVIEw(LayoutInflater inflater,VIEwGroup container,Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_all,container,false);}}
堆栈跟踪:
androID.support.v4.app.Fragment.setMenuVisibility(boolean)' on a null object reference at androID.support.v4.app.FragmentStatePagerAdapter.instantiateItem(FragmentStatePagerAdapter.java:116) at androID.support.v4.vIEw.VIEwPager.addNewItem(VIEwPager.java:836) at androID.support.v4.vIEw.VIEwPager.populate(VIEwPager.java:986) at androID.support.v4.vIEw.VIEwPager.populate(VIEwPager.java:918) at androID.support.v4.vIEw.VIEwPager.onMeasure(VIEwPager.java:1440) at androID.vIEw.VIEw.measure(VIEw.java:17430) at androID.vIEw.VIEwGroup.measureChilDWithmargins(VIEwGroup.java:5463) at androID.Widget.linearLayout.measureChildBeforeLayout(linearLayout.java:1436) at androID.Widget.linearLayout.measureVertical(linearLayout.java:722) at androID.Widget.linearLayout.onMeasure(linearLayout.java:613) at androID.vIEw.VIEw.measure(VIEw.java:17430) at androID.vIEw.VIEwGroup.measureChilDWithmargins(VIEwGroup.java:5463) at androID.Widget.FrameLayout.onMeasure(FrameLayout.java:430) at androID.support.v7.internal.Widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:124) at androID.vIEw.VIEw.measure(VIEw.java:17430) at androID.vIEw.VIEwGroup.measureChilDWithmargins(VIEwGroup.java:5463) at androID.Widget.linearLayout.measureChildBeforeLayout(linearLayout.java:1436) at androID.Widget.linearLayout.measureVertical(linearLayout.java:722) at androID.Widget.linearLayout.onMeasure(linearLayout.java:613) at androID.vIEw.VIEw.measure(VIEw.java:17430) at androID.vIEw.VIEwGroup.measureChilDWithmargins(VIEwGroup.java:5463) at androID.Widget.FrameLayout.onMeasure(FrameLayout.java:430) at androID.vIEw.VIEw.measure(VIEw.java:17430) at androID.vIEw.VIEwGroup.measureChilDWithmargins(VIEwGroup.java:5463) at androID.Widget.linearLayout.measureChildBeforeLayout(linearLayout.java:1436) at androID.Widget.linearLayout.measureVertical(linearLayout.java:722) at androID.Widget.linearLayout.onMeasure(linearLayout.java:613) at androID.vIEw.VIEw.measure(VIEw.java:17430) at androID.vIEw.VIEwGroup.measureChilDWithmargins(VIEwGroup.java:5463) at androID.Widget.FrameLayout.onMeasure(FrameLayout.java:430) at com.androID.internal.policy.impl.PhoneWindow$DecorVIEw.onMeasure(PhoneWindow.java:2560) at androID.vIEw.VIEw.measure(VIEw.java:17430) at androID.vIEw.VIEwRootImpl.performMeasure(VIEwRootImpl.java:2001) at androID.vIEw.VIEwRootImpl.measureHIErarchy(VIEwRootImpl.java:1166) at androID.vIEw.VIEwRootImpl.performTraversals(VIEwRootImpl.java:1372) at androID.vIEw.VIEwRootImpl.doTraversal(VIEwRootImpl.java:1054) at androID.vIEw.VIEwRootImpl$TraversalRunnable.run(VIEwRootImpl.java:5779) at androID.vIEw.Choreographer$CallbackRecord.run(Choreographer.java:767) at androID.vIEw.Choreographer.doCallbacks(Choreographer.java:580) at androID.vIEw.Choreographer.doFrame(Choreographer.java:550) at androID.vIEw.Choreographer$FramedisplayEventReceiver.run(Choreographer.java:753) at androID.os.Handler.handleCallback(Handler.java:739) at androID.os.Handler.dispatchMessage(Handler.java:95) at androID.os.Looper.loop(Looper.java:135) at androID.app.ActivityThread.main(ActivityThread.java:5221) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.androID.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) at com.androID.internal.os.ZygoteInit.main(ZygoteInit.java:694)解决方法
public static final int SERVICES_ALL = 1;public static final int SERVICES_FEATURED = 2;public static final int SERVICES_NEW = 3;
应该
public static final int SERVICES_ALL = 0;public static final int SERVICES_FEATURED = 1;public static final int SERVICES_NEW = 2;
getItem是从0开始的索引,这意味着在你的代码中,你的Adapter的case 0永远不会匹配一个,在这种情况下你返回一个null片段导致异常
总结以上是内存溢出为你收集整理的Android – 尝试在空对象引用上调用虚方法’void android.support.v4.app.Fragment.setMenuVisibility(boolean)’全部内容,希望文章能够帮你解决Android – 尝试在空对象引用上调用虚方法’void android.support.v4.app.Fragment.setMenuVisibility(boolean)’所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)