共享首选项会导致应用崩溃.安卓工作室


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

公共共享首选项导致应用崩溃

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

所以这是一个空指针异常。对我来说,这意味着编辑文本尚未初始化。

不知道从这里尝试什么。感觉我现在只是在扔代码...

在你的代码中:

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

仅当活动中存在具有提供 id 的视图(而不是片段中)时,该方法findViewById返回视图。否则,它将返回空值。

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

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

最新更新