Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Android Android Fragments Tablet Time Side by Side

chandrashekhar singh
chandrashekhar singh
507 Points

I am getting this problem

Attempt to invoke virtual method 'int android.os.Bundle.getInt(java.lang.String)' on a null object reference at com.csing1s.myapplication.CheckBoxexFragments.<init>(CheckBoxexFragments.java:17) at com.csing1s.myapplication.IngredientFragment.<init>(IngredientFragment.java:0) at com.csing1s.myapplication.ViewPagerFragment.onCreateView(ViewPagerFragment.java:32) at android.support.v4.app.Fragment.performCreateView(Fragment.java:1974) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252) at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738) at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617)

On Line 17 of CheckBoxesFragments you call getInt() on a bundle. It looks like you may have passed in a string on the getInt() method from what I see in the log. Can you send the code?

It would be great if you could paste the code here along with the log.

4 Answers

On Line 17 of CheckBoxesFragments you call getInt() on a bundle. It looks like you may have passed in a string on the getInt() method from what I see in the log. Can you send the code?

chandrashekhar singh
chandrashekhar singh
507 Points

public abstract class CheckBoxexFragments extends Fragment { private static final String KEY_CHECKED_BOXEX ="key_checked_box"; int index = getArguments().getInt(ViewPagerFragment.KEY_RECIEPE_INDEX); String [] contents= getContents(index); private CheckBox[] mCheckBoxes= new CheckBox[contents.length]; @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // int index = getArguments().getInt(ViewPagerFragment.KEY_RECIEPE_INDEX); // boolean isIngredients = getArguments().getBoolean(ViewPagerFragment.KEY_IS_INGREDIENTS); View view = inflater.inflate(R.layout.checkboxfragments,container,false); LinearLayout linearLayout = (LinearLayout) view.findViewById(R.id.checkboxeslayoutFragment);

    String [] contents= getContents(index);
    /*
     if(isIngredients)
     {
       contents=  Recipie.ingredients[index].split("'");
     }
    else
     {
        contents= Recipie.ingredients[index].split("'");
     }
     */
    //mCheckBoxes = new CheckBox[contents.length];
    boolean[] checkedBoxex = new boolean[mCheckBoxes.length];
    if(savedInstanceState != null && savedInstanceState.getBooleanArray(KEY_CHECKED_BOXEX)!=null);
    {
        checkedBoxex = savedInstanceState.getBooleanArray(KEY_CHECKED_BOXEX);

    }
        setupCheckBoxes(contents,linearLayout,checkedBoxex);

    return view;

}

public abstract String[] getContents(int index);



private void setupCheckBoxes(String[] contents, ViewGroup container, boolean[] checkedBoxex) {
    for (String in : contents) {
        int i = 0;
        mCheckBoxes[i] = new CheckBox(getActivity());
        mCheckBoxes[i].setPadding(8, 16, 8, 16);
        mCheckBoxes[i].setTextSize(20f);
        mCheckBoxes[i].setText(in);
        container.addView(mCheckBoxes[i]);
        if (checkedBoxex[i]) {
            mCheckBoxes[i].toggle();
            i++;
        }


    }




}

@Override
public void onSaveInstanceState(Bundle outState) {
    boolean[]stateOfCheckBoxex = new boolean[mCheckBoxes.length];
    int i =0;
    for (CheckBox checkBox:mCheckBoxes)
    {
        stateOfCheckBoxex[i]=checkBox.isChecked();
        i++;
    }
    outState.putBooleanArray(KEY_CHECKED_BOXEX,stateOfCheckBoxex);
    super.onSaveInstanceState(outState);
}

}

chandrashekhar singh
chandrashekhar singh
507 Points

public class MainActivity extends AppCompatActivity implements fragment.RecipieSelected { public static final String LIST_FRAGMENT="list_fragment"; public static final String view_FRAGMENT="view_fragment";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    boolean isTablet = getResources().getBoolean(R.bool.is_tablet);
    Fragment savefragment = getSupportFragmentManager().findFragmentByTag(LIST_FRAGMENT);
    if(savefragment==null) {
        fragment fragment = new fragment();
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = manager.beginTransaction();
        fragmentTransaction.add(R.id.placeholder, fragment,LIST_FRAGMENT);
        fragmentTransaction.commit();
    }

}

@Override
public void OnListSelectedRecipie(int index) {

    ViewPagerFragment fragment = new ViewPagerFragment();
    Bundle bundle = new Bundle();
    bundle.putInt(ViewPagerFragment.KEY_RECIEPE_INDEX,index);
    fragment.setArguments(bundle);
    FragmentManager manager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = manager.beginTransaction();
    fragmentTransaction.replace(R.id.placeholder,fragment,view_FRAGMENT);
    fragmentTransaction.addToBackStack(null);
    fragmentTransaction.commit();

}

}

chandrashekhar singh
chandrashekhar singh
507 Points

public class ViewPagerFragment extends Fragment { public static final String KEY_RECIEPE_INDEX = "recipe_index"; // public static final String KEY_IS_INGREDIENTS="key_is_Ingredients"; @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { int index = getArguments().getInt(KEY_RECIEPE_INDEX); getActivity().setTitle(Recipie.names[index]); //Toast.makeText(getContext(),Recipie.names[index],Toast.LENGTH_SHORT).show();

    View view = inflater.inflate(R.layout.fragmentviewpager, container, false);
    final IngredientFragment mingredientFragment = new IngredientFragment();
    Bundle bundle = new Bundle();
    bundle.putInt(KEY_RECIEPE_INDEX,index);
 //  bundle.putBoolean(KEY_IS_INGREDIENTS,true);

    mingredientFragment.setArguments(bundle);
    final  DirectionFragments mdirectionFragments = new DirectionFragments();
    bundle = new Bundle();
    bundle.putInt(KEY_RECIEPE_INDEX,index);
//    bundle.putInt(KEY_RECIEPE_INDEX,index);bundle.putBoolean(KEY_IS_INGREDIENTS,false);
    mdirectionFragments.setArguments(bundle);
    ViewPager viewpager = (ViewPager) view.findViewById(R.id.vp);
    viewpager.setAdapter(new FragmentPagerAdapter(getFragmentManager()) {
        @Override
        public Fragment getItem(int position) {
            if(position == 0)
                return mingredientFragment;
            else {
               return mdirectionFragments;
            }

        }

        @Override
        public CharSequence getPageTitle(int position) {
            return position == 0 ? "Ingredients" : "Directions";
        }

        @Override
        public int getCount() {
            return 2;
        }

    });

    TabLayout tabLayout = (TabLayout) view.findViewById(R.id.Tablayout);
     tabLayout.setupWithViewPager(viewpager);
    return view;



}
@Override
public void onStop() {
    super.onStop();
    getActivity().setTitle(getResources().getString(R.string.app_name));
}

}

chandrashekhar singh
chandrashekhar singh
507 Points

public class IngredientFragment extends CheckBoxexFragments { @Override public String[] getContents(int index) { return Recipie.ingredients[index].split("'"); } }