0

I have a FragmentActivity which I am trying to restore the Sharedpreferences on in the onCreateView method.

My code is:

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        Bundle args = getArguments();
        int position = args.getInt(ARG_OBJECT);
        int tabLayout = 0;
        switch (position) {
            case 0:
                tabLayout = R.layout.layout_one;
                break;
            case 1:
                tabLayout = R.layout.layout_two;
                break;
        }

        View rootView = inflater.inflate(tabLayout, container, false);

        //Get restore state of all checkboxes
        SharedPreferences prefs = getActivity().getSharedPreferences(PREFS_NAME, 0);
        String topack00value = prefs.getString("topack00", "");

        CheckedTextView topack00 = (CheckedTextView) rootView.findViewById(R.id.topack_00);

        switch (topack00value) {
            case "yes":
                topack00.setTextColor(getResources().getColor(R.color.cyan700));
                topack00.setPaintFlags(topack00.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
                break;
            case "no":
                topack00.setTextColor(getResources().getColor(R.color.black));
                topack00.setPaintFlags(topack00.getPaintFlags() & (~Paint.STRIKE_THRU_TEXT_FLAG));
                break;
            default:
                topack00.setTextColor(getResources().getColor(R.color.black));
                topack00.setPaintFlags(topack00.getPaintFlags() & (~Paint.STRIKE_THRU_TEXT_FLAG));
                break;
        }

        return rootView;

I constantly get the attempt to invoke null object resource error. The error is pointing at the topack00 checktextView but I already have it linked to the 'rootView' View.

I've had a look at a few similar cases but mine seems to be okay..Or am I completely missing something. Similar issues: Attempt to invoke virtual method

Been stuck on this for literally 5 hours. :|

Community
  • 1
  • 1
karlstackoverflow
  • 3,298
  • 6
  • 30
  • 41
  • can you please post your logcat? – blueware Jul 12 '15 at 08:11
  • Btw, did you try using `getIntent().getExtras()` instead of `getArguments()` ? As its an activity not a fragment, Also, please check your `args` data, are you passing null values? – blueware Jul 12 '15 at 08:25
  • possible duplicate of [NullPointerException error on context when calling sharedPref from fragment](http://stackoverflow.com/questions/30953358/nullpointerexception-error-on-context-when-calling-sharedpref-from-fragment) – Nilay Dani Jul 12 '15 at 08:32
  • Thanks blueware. Actually the onCreateView sits inside a class TabFragment which extends Fragment. – karlstackoverflow Jul 12 '15 at 11:06

1 Answers1

0

Thanks for the suggestions guys. I figured out a theory (which worked btw) within minutes of leaving the computer. Couldn't test it though until I got home. The issue was to do with the switch case launching two different layouts. The ID of the CheckedTextView only existed in the first layout for tab one but not the other which caused it to be null. Thanks.

karlstackoverflow
  • 3,298
  • 6
  • 30
  • 41