gpt4 book ai didi

java - 如何解决此问题 : Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference

转载 作者:行者123 更新时间:2023-11-29 18:31:16 25 4
gpt4 key购买 nike

我已经阅读了此处的另一篇文章,但似乎无法弄清楚为什么会出现此错误。错误在标题中,也在此处:Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference

我尝试添加 msg = (EditText)v.findViewById(R.id.msgTxt);但这仍然没有解决任何问题。

public class HelpFragment extends Fragment {
Button sendEmail;
EditText msg;


@Nullable

protected void process(View view) {

}

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_help, null);
sendEmail = (Button)v.findViewById(R.id.SendBtn);
sendEmail.setOnClickListener(new View.OnClickListener() {

public void onClick(View v){
msg = (EditText)v.findViewById(R.id.msgTxt);
String message = msg.getText().toString();
sendEmail(message);
}

});
return v;
}
protected void sendEmail(String message){
String to= new String("em@sd.xo");
String subject=("a message from app");
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.putExtra(Intent.EXTRA_EMAIL, to);
emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
emailIntent.putExtra(Intent.EXTRA_TEXT, message);
emailIntent.setType("message/rfc822");
startActivity(Intent.createChooser(emailIntent, "Email"));
}

}

最佳答案

您引用了 onClickView。您应该在 onClick() 方法之前实现 msg = (EditText)v.findViewById(R.id.msgTxt);

您应该使用 editText.getText().toString(),因为 editText.getText() 返回 Editable


public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_help, null);
sendEmail = (Button)v.findViewById(R.id.SendBtn);
msg = (EditText)v.findViewById(R.id.msgTxt);

sendEmail.setOnClickListener(new View.OnClickListener() {

public void onClick(View v){
String message = msg.getText().toString();
sendEmail(message);
}

});
return v;
}

关于java - 如何解决此问题 : Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56173932/

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