javaandroidsharedpreferencesmain-activity

Shared Preferences causes app to crash. Android Studio


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...


Solution

  • 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);