1
public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener, page1_user_notes.communicate{

    private SectionsPagerAdapter mSectionsPagerAdapter;
    private ViewPager mViewPager;

    //checkbox array list
    public ArrayList<CheckBox> checkBoxArrayList = new ArrayList<>();
    //user titleText Array List
    public ArrayList<String> titleTextArrayList = new ArrayList<>();
    //user notesArrayList
    public ArrayList<String> notesArrayList = new ArrayList<>();

    DateFormat df = new SimpleDateFormat("EEE, MMM d, ''yy");
    String date = df.format(Calendar.getInstance().getTime());
   // PopupMenu popup
    LinearLayout ll;
    LinearLayout.LayoutParams lp;
    PopupMenu popup;
    CheckBox checkbox;
    private int checkboxPopupCheck;

    String checkTitle;
    String checkNotes;

    //We will use this to store our shared preferences.
    public static final String SAVE = "MyPrefs";
    private EditText mTitle;
    private EditText mNotes;
    private String mTitleString = "titles";
    private String mNotesString = "notes";
    String t;
    String n;

    @RequiresApi(api = Build.VERSION_CODES.GINGERBREAD)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // recovering the instance state
        if (savedInstanceState != null) {

        }
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

        /***-----------Added after----------*/
        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
        // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) findViewById(R.id.container);
        mViewPager.setAdapter(mSectionsPagerAdapter);

//Shared preferences
        mTitle = (EditText)findViewById(R.id.editText);
        mNotes = (EditText)findViewById(R.id.editText2);
        t = mTitle.getText().toString();
        n = mNotes.getText().toString();
        SharedPreferences settings = getSharedPreferences(SAVE, Context.MODE_PRIVATE);

        SharedPreferences.Editor editor = settings.edit();
        editor.putString(mTitleString,t);
        editor.putString(mNotesString,n);
        editor.apply();
        Toast.makeText(MainActivity.this, "onCreate called", Toast.LENGTH_LONG).show();

    }

Here is my Main Activity class.

I have looked at

https://www.tutorialspoint.com/android/android_shared_preferences.htm

Public shared preferences causes app to crash

https://developer.android.com/reference/android/content/SharedPreferences.html

Log cat error

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference

 at com.myApps.Notes.MainActivity.onCreate(MainActivity.java:103)

Line 103 is

t = mTitle.getText().toString();

So its a null pointer exception. To me that means the EditText hasn't been initialized yet.

Not sure what to try from here. Feel like I'm just throwing code around now...

pewpew
  • 700
  • 2
  • 9
  • 32
  • 1
    It would seem that the `` with ID `editText` is not in the `activity_main` layout, either directly, or in an ``d layout. `findViewById(R.id.editText)` will return null in that case. – Mike M. Oct 08 '17 at 01:16
  • Right its not in the same layout. Its part of a fragment. So when I tried to use this in code in the onCreate() method in the fragment. The same thing happens. – pewpew Oct 08 '17 at 01:19
  • A `Fragment`'s `View`s won't be available yet in its `onCreate()` method, since that runs before the `onCreateView()` method. – Mike M. Oct 08 '17 at 01:20
  • Ok. maybe I will try to make a method in the fragment. onCreateView() ...https://developer.android.com/guide/components/fragments.html – pewpew Oct 08 '17 at 01:23
  • Actually that is what I tried. already. But ill give it a shot again. – pewpew Oct 08 '17 at 01:25
  • You should already have an `onCreateView()` override in your `Fragment`, otherwise that will eventually cause a crash, too. – Mike M. Oct 08 '17 at 01:25
  • Yes I have that in the fragment. The fragment is working fine. – pewpew Oct 08 '17 at 01:28
  • Whenever I try to initialize the `String t` the app crashes – pewpew Oct 08 '17 at 01:37
  • Ah--- so in the fragment you have to have `mTitle = (EditText)rootView.findViewById(R.id.editText);` I thought it was `getActivity().findViewById` – pewpew Oct 08 '17 at 01:44

1 Answers1

0

In your code:

mTitle = (EditText)findViewById(R.id.editText);
mNotes = (EditText)findViewById(R.id.editText2);
t = mTitle.getText().toString();
n = mNotes.getText().toString();

The method findViewById returns the view only if a view with provided id exists in the activity, not in the fragments. Otherwise it will return null.

If the views are inside the fragment, you should get those views from inside the onCreateView method of your fragment.

mNotes = (EditText)rootView.findViewById(R.id.editText2);
Nabin Bhandari
  • 15,949
  • 6
  • 45
  • 59