【发布时间】:2017-10-08 01:08:29
【问题描述】:
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 尚未初始化。
不确定从这里尝试什么。感觉我现在只是在乱扔代码……
【问题讨论】:
-
ID 为
editText的<EditText>似乎不在activity_main布局中,无论是直接还是在<include>d 布局中。在这种情况下,findViewById(R.id.editText)将返回 null。 -
对,它不在同一个布局中。它是片段的一部分。因此,当我尝试在片段的 onCreate() 方法的代码中使用它时。同样的事情也会发生。
-
Fragment的Views 在其onCreate()方法中尚不可用,因为它在onCreateView()方法之前运行。 -
好的。也许我会尝试在片段中创建一个方法。 onCreateView() ...developer.android.com/guide/components/fragments.html
-
其实这就是我尝试过的。已经。但是我再试一次。
标签: java android sharedpreferences main-activity