Reputation: 271
I'm getting a NullPointerException
on this onClick()
-method. On the line with
String standardT = smsText.getText().toString().
This is the setStandardSMS
method and alertDialog
I'm having problems with.
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"
/>
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?
UPDATE: Updated with the whole setStandardSMS-method, and standard_text.xml.
Upvotes: 0
Views: 139
Reputation: 44118
getText()
might return null. That means your call toString()
possibly throws a NullPointerException
. To avoid that you can instead use String.valueOf()
.
Here's how you properly test if a string is empty (null or 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();
}
Upvotes: 0
Reputation: 18112
Try using this
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();
}
Upvotes: 1