共享首选项会导致应用崩溃。 Android Studio

时间:2017-10-08 01:08:30

标签: java android sharedpreferences main-activity

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

    }

这是我的主要活动课程。

我看了

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

记录猫错误

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)

第103行

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

所以它是一个空指针异常。对我而言,这意味着EditText尚未初始化。

不确定从这里尝试什么。感觉就像我现在只是抛出代码...

1 个答案:

答案 0 :(得分:0)

在您的代码中:

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

方法findViewById仅在活动中存在具有提供的id的视图而不是片段中时才返回视图。否则它将返回null。

如果视图位于片段内,您应该从片段的onCreateView方法中获取这些视图。

mNotes = (EditText)rootView.findViewById(R.id.editText2);