gpt4 book ai didi

android - EditText.getText().toString() 错误

转载 作者:行者123 更新时间:2023-11-29 14:59:09 25 4
gpt4 key购买 nike

<分区>

我正在使用这段代码,日志显示存在终止应用程序的错误,如

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
at com.example.amuna.project1.addWord.confirmBtnClicked(addWord.java:25)

代码是

import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class addWord extends AppCompatActivity {
EditText wordEdit, defEdit;
Button confirmButton;
@Override
public void onCreate(Bundle savedInstanceState) {
wordEdit=(EditText)findViewById(R.id.wordAdd);
defEdit=(EditText)findViewById(R.id.defAdd);
confirmButton=findViewById(R.id.confirmBtn);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_word);
}
public void confirmBtnClicked(View v){
SharedPreferences pref = getSharedPreferences("pref", MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
String newWord = wordEdit.getText().toString();
String newDef = defEdit.getText().toString();
editor.putString("word", newWord);
editor.putString("def", newDef);
editor.commit();

}
}

请帮忙!这是 String newWord = wordEdit.getText().toString();有错误,如果我还有什么错误,请通知我。谢谢!

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com