获得NullPointerException异常,而试图从EditText上提取文本 [英] Getting NullPointerException while trying to extract text from EditText
本文介绍了获得NullPointerException异常,而试图从EditText上提取文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在这得到一个 NullPointerException异常
的onClick()
- 方法。在与
I'm getting a NullPointerException
on this onClick()
-method. On the line with
String standardT = smsText.getText().toString().
这是 setStandardSMS
方法和 alertDialog
我有问题。
public void setStandardSMS(){
AlertDialog.Builder standardSMSBuilder = new AlertDialog.Builder(this);
LayoutInflater inflater = this.getLayoutInflater();
View view = inflater.inflate(R.layout.standard_text, null)
standardSMSBuilder.setView(view);
final EditText smsText = (EditText)view.findViewById(R.id.standard_sms);
standardSMSBuilder.setPositiveButton(R.string.saveButton, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Context context = getApplicationContext();
CharSequence text = "Du maa fylle inn alle felt!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
String standardT = smsText.getText().toString();
if(TextUtils.isEmpty(standardT)){
Intent newIntent = getIntent();
newIntent.putExtra("tag_text_txt", standardT);
setResult(2, newIntent);
} else {
toast.show();
}
}
});
smsDialog = standardSMSBuilder.create();
standardSMSBuilder.show();
}
standard_text.xml
standard_text.xml
<EditText
android:id="@+id/standard_sms"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:gravity="top"
android:lines="8"
android:background="@drawable/border"
android:inputType="textMultiLine"
/>
我真正想要的是节省了用户写入,到数据库中的文字。但是,code不断打破这里。
All I really want is to save the text which the user writes in, into a database. But the code keeps breaking here.
任何人能发现我在做什么错了?
Can anyone spot what I am doing wrong?
更新:更新与整个setStandardSMS法和standard_text.xml
UPDATE: Updated with the whole setStandardSMS-method, and standard_text.xml.
推荐答案
尝试使用这种
public void setStandardSMS(){
AlertDialog.Builder standardSMSBuilder = new AlertDialog.Builder(this);
LayoutInflater inflater = this.getLayoutInflater();
View view = inflater.inflate(R.layout.standard_text, null)
standardSMSBuilder.setView(view);
final EditText smsText = (EditText)view.findViewById(R.id.standard_sms);
standardSMSBuilder.setPositiveButton(R.string.saveButton, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Context context = getApplicationContext();
CharSequence text = "Du maa fylle inn alle felt!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
String standardT = smsText.getText().toString();
if(TextUtils.isEmpty(standardT)){
Intent newIntent = getIntent();
newIntent.putExtra("tag_text_txt", standardT);
setResult(2, newIntent);
} else {
toast.show();
}
}
});
smsDialog = standardSMSBuilder.create();
standardSMSBuilder.show();
}
这篇关于获得NullPointerException异常,而试图从EditText上提取文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文