I make two buttons to load the saved text and save it. in OnCreate :

  etText = (EditText)findViewById(R.id.etText); //ΠΏΠΎΠ»Π΅ для Π²Π²ΠΎΠ΄Π° btnSave = (Button)findViewById(R.id.btnSave); //ΠΊΠ½ΠΎΠΏΠΊΠ° сохранСнияя btnSave.setOnClickListener(this); btnLoad = (Button)findViewById(R.id.btnLoad); //ΠΊΠ½ΠΎΠΏΠΊΠ° Π·Π°Π³Ρ€ΡƒΠ·ΠΊΠΈ btnLoad.setOnClickListener(this); 

Farther

  public void onClick(View v){ switch (v.getId()){ case R.id.btnSave: saveText(); break; case R.id.btnLoad: loadText(); break; default: break; } } private void saveText() { sPref = getPreferences(MODE_PRIVATE); SharedPreferences.Editor ed = sPref.edit(); ed.putString(SAVED_TEXT, etText.getText().toString()); ed.commit(); Toast.makeText(MainActivity.this, "Text saved", Toast.LENGTH_SHORT).show(); } private void loadText() { sPref = getPreferences(MODE_PRIVATE); String savedText = sPref.getString(SAVED_TEXT,""); etText.setText(savedText); Toast.makeText(MainActivity.this, "Text loaded", Toast.LENGTH_SHORT).show(); } 

As a result, the application fails with:

 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference at <имя ΠΏΠ°ΠΊΠ΅Ρ‚Π°>.MainActivity.onCreate(MainActivity.java:104) 104 строка: btnSave.setOnClickListener(this); 

Markup Code:

 <?xml version="1.0" encoding="utf-8"?> 

  <Button android:id="@+id/btnLoad" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Load"> </Button> <Button android:id="@+id/btnSave" android:layout_width="105dp" android:layout_height="wrap_content" android:text="Save"> </Button> </LinearLayout> <EditText android:id="@+id/etText" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="top|left" android:inputType="textMultiLine" /> 

  • Can you add markup code? - Maks Brain
  • Added markup code - Big_Energy
  • Just in case, I ask, did you remember to setContentView? - Maks Brain
  • Not forgotten, registered - Big_Energy
  • Show more onCreate () code. And tell me the name of the file with markup - Vladyslav Matviienko

2 answers 2

Variable btnSave you, according to the error, null

This can happen if you

  1. Sealed in the ID button
  2. There are no buttons with such an ID in the markup used.
  3. You are trying to search for a button in the markup before setting the markup via setContentView()
  4. More for a number of more complex reasons such as nested markup with overlapping ID
  • 1. all id are correct, 2. there are buttons, 3. I look for buttons before setting the markup, 4. there are no overlaps - Big_Energy
  • 2
    @AndrijUzhegov how do you think, from where can buttons appear, if the marking is not yet established? The first step in the onCreate method in super.onCreate call setContentView() after calling super.onCreate in order to avoid such situations. - temq
  • setContentView (R.layout.activity_main); goes to install the buttons - Big_Energy
  • Those. Now you are looking for after installing the markup and nothing has changed? - Yuriy SPb ♦
  • Nothing has changed, the same mistake - Big_Energy

Make a Clear / Rebuild Project. Then earn