Android - 尝试在空对象引用上调用虚拟方法 'void android.support.v4.app.Fragment.setMenuVisibility(boolean)'
Android - Attempt to invoke virtual method 'void android.support.v4.app.Fragment.setMenuVisibility(boolean)' on a null object reference
这里是错误的完整堆栈跟踪:http://pastebin.com/5dQHkUw3
这个错误是在我开始在项目中使用片段后才开始出现的,因为我想提供 JSON 个提要。之前我创建 Tabs,NavigationDrawer 等的时候没问题
MainActivity.java
public class 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;
@Override
protected 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)
);
}
}
@Override
public void onTabSelected(MaterialTab materialTab) {
mViewPager.setCurrentItem(materialTab.getPosition());
}
@Override
public void onTabReselected(MaterialTab materialTab) {
}
@Override
public 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];
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public 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类似。)
public class FragmentAll 扩展片段 {
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
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public 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 的索引,这意味着在您的代码中,适配器的 case 0
永远不会匹配一个,在这种情况下,您将返回一个导致异常的空片段
这里是错误的完整堆栈跟踪:http://pastebin.com/5dQHkUw3
这个错误是在我开始在项目中使用片段后才开始出现的,因为我想提供 JSON 个提要。之前我创建 Tabs,NavigationDrawer 等的时候没问题
MainActivity.java
public class 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;
@Override
protected 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)
);
}
}
@Override
public void onTabSelected(MaterialTab materialTab) {
mViewPager.setCurrentItem(materialTab.getPosition());
}
@Override
public void onTabReselected(MaterialTab materialTab) {
}
@Override
public 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];
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public 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类似。)
public class FragmentAll 扩展片段 {
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
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public 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 的索引,这意味着在您的代码中,适配器的 case 0
永远不会匹配一个,在这种情况下,您将返回一个导致异常的空片段