尝试从EditText中提取文本时获取NullPointerException

时间:2014-10-24 11:23:24

标签: android android-edittext

我在这个NullPointerException方法上获得了onClick()。在

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

<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"
/>

我真正想要的是将用户写入的文本保存到数据库中。但是代码在这里不断打破。

有人能发现我做错了吗?

更新:使用整个setStandardSMS方法和standard_text.xml进行更新。

2 个答案:

答案 0 :(得分:1)

尝试使用此

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();
}

答案 1 :(得分:0)

getText()可能会返回null。这意味着您的通话toString()可能会引发NullPointerException。为避免这种情况,您可以使用String.valueOf()

以下是如何正确测试字符串是否为空(null或length = 0):

String standardT = String.valueOf(smsText.getText());

if(TextUtils.isEmpty(standardT)) {
    Intent newIntent = getIntent();
    newIntent.putExtra("tag_text_txt", standardT);
    setResult(2, newIntent);
} else {
    toast.show();
}