I am extremely new to programming so your help will be greatly appreciated and this is the first time I am asking for help here so hope I am doing it right.
I am trying to create a simple notes taking section in an app but I am getting the following error:
Attempt to invoke virtual method 'void android.widget.EditText.setText(java.lang.CharSequence)' on a null object reference
Here is the code:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
et_memo = (EditText) findViewById(R.id.et_memo);
b_clear = (Button) findViewById(R.id.b_clear);
b_save = (Button) findViewById(R.id.b_save);
SharedPreferences preferences = getSharedPreferences("PREFS", 0);
memo = preferences.getString("memo","");
et_memo.setText(memo);
b_clear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
memo ="";
et_memo.setText(memo);
}
});
b_save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
memo = et_memo.getText().toString();
SharedPreferences preferences = getSharedPreferences("PREFS", 0);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("memo", memo);
editor.commit();
Toast.makeText(MainActivity.this, "Note saved!",
Toast.LENGTH_SHORT).show();
Here is my XML file:
<android.support.v7.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:orientation="vertical">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="70dp"
android:inputType="textNoSuggestions"
android:textSize="16sp"
android:textColor="@color/black"
android:id="@+id/et_memo"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="2"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="CLEAR"
android:id="@+id/b_clear"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="SAVE"
android:id="@+id/b_save"/>
</LinearLayout>
</android.support.v7.widget.LinearLayoutCompat>`
The most common cause for that particular error is that "et_memo" is missing from the XML file. The problem is that if any XML file in your project contains "@+id/et_memo" the code "findViewById(R.id.et_memo);" will compile just fine. BUT, if when calling "setContentView" an XML file is specified that does not contain "et_memo", the view will not be found and instead it will be set to "null". If you invoke any methods of a null object an immediate crash will happen. Also, be ware if you have any other versions of "activity_main.xml" in other layout directories, make sure they have all the necessary views as well.
This code below runs just fine in my environment.
Code:
package com.example.elletlar.simpletests;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private String memo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
final EditText et_memo = findViewById(R.id.et_memo);
final Button b_clear = findViewById(R.id.b_clear);
final Button b_save = findViewById(R.id.b_save);
SharedPreferences preferences = getSharedPreferences("PREFS", 0);
memo = preferences.getString("memo","");
et_memo.setText(memo);
b_clear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
et_memo.setText("");
SharedPreferences preferences = getSharedPreferences("PREFS", 0);
SharedPreferences.Editor editor = preferences.edit();
editor.remove("memo");
editor.apply();
}
});
b_save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
memo = et_memo.getText().toString();
SharedPreferences preferences = getSharedPreferences("PREFS", 0);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("memo", memo);
editor.apply();
Toast.makeText(MainActivity.this, "Note saved!",
Toast.LENGTH_SHORT).show();
}
});
}
}
The XML:
<android.support.v7.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="70dp"
android:inputType="textNoSuggestions"
android:textSize="16sp"
android:textColor="#000000"
android:id="@+id/et_memo"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="2"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="CLEAR"
android:id="@+id/b_clear"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="SAVE"
android:id="@+id/b_save"/>
</LinearLayout>
</android.support.v7.widget.LinearLayoutCompat>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.