[英]App crashing because of setText being called on a null object reference
我对编程非常陌生,因此非常感谢您的帮助,这是我第一次在这里寻求帮助,所以希望我做得对。
我正在尝试在应用程序中创建一个简单的笔记记录部分,但出现以下错误:
Attempt to invoke virtual method 'void android.widget.EditText.setText(java.lang.CharSequence)' on a null object reference
这是代码:
@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();
这是我的 XML 文件:
<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>`
该特定错误的最常见原因是 XML 文件中缺少“et_memo”。 问题在于,如果您的项目中的任何XML 文件包含“@+id/et_memo”,则代码“findViewById(R.id.et_memo);” 会编译得很好。 但是,如果在调用“setContentView”时指定了不包含“et_memo”的 XML 文件,则不会找到该视图,而是将其设置为“null”。 如果您调用空对象的任何方法,则会立即发生崩溃。 另外,如果您在其他布局目录中有任何其他版本的“activity_main.xml”,请确保它们也具有所有必要的视图。
下面的这段代码在我的环境中运行得很好。
代码:
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();
}
});
}
}
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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.