web-dev-qa-db-ja.com

Android-nullオブジェクト参照で仮想メソッド 'void Android.support.v4.app.Fragment.setMenuVisibility(boolean)'を呼び出そうとしました

エラーの完全なスタックトレースは次のとおりです。 http://Pastebin.com/5dQHkUw

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;

@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に似ています。)

パブリッククラス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
}

@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)
7
George
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アダプタの1つと一致することはありません。その場合、nullフラグメントが返され、例外が発生します。

14
Blackbelt