我对编程非常陌生,因此将非常感谢您的帮助,这是我第一次在这里寻求帮助,希望我做得对。我试图在应用程序中创建一个简单的笔记记录部分,但遇到以下错误:尝试在空对象引用上调用虚拟方法'void android.widget.EditText.setText(java.lang.CharSequence)'>
代码如下:
@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>`
答案 0 :(得分:1)
该特定错误的最常见原因是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>