Hi I'w working into a ShowDialog and I want it to show a message with 2 local variables. For that reason It says there is an error so I would appreciate some help if there is a way of showing these variables. Here is my code and the line where the error is is this one:
.setMessage("The NIF is" +user +DNIWord "-")
The rest of the code is this one:
protected Dialog onCreateDialog (int id){
switch (id) {
case DIALOG_ERROR_CANDIDATO:
return new AlertDialog.Builder(this)
.setIcon(R.drawable.ic_error)
.setTitle(R.string.error)
.setMessage("The NIF is" +user +DNIWord "-")
.setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
})
.create();
}
return null;
}
The 2 variables are obtained from here:
int user = Integer.parseInt(dniText.getText().toString());
public static String DNIWord(int user) {
return String.valueOf(user) + NIF_STRING_ASOCIATION.charAt(user % 23);
}
Thank you
Edit:
package com.prueba.inicio;
import com.prueba.R;
import com.prueba.R.drawable;
import com.prueba.R.id;
import com.prueba.R.layout;
import com.prueba.R.string;
import com.prueba.inicio.Autorizacion;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.content.DialogInterface;
import android.content.Intent;
import android.text.style.BackgroundColorSpan;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
public class Ident extends Activity {
private static final int DIALOG_ERROR_CANDIDATO= 0;
public static final String NIF_STRING_ASOCIATION = "TRWAGMYFPDXBNJZSQVHLCKE";
public static String DNIWord(int user) {
return String.valueOf(user) + NIF_STRING_ASOCIATION.charAt(user % 23);
}
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.identificacion);
Button bSiguiente = (Button) findViewById(R.id.btn_siguiente);
final EditText dniText = (EditText) findViewById(R.id.dni_candidato);
bSiguiente.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
int user = Integer.parseInt(dniText.getText().toString());
String longitud = dniText.getText().toString();
if(user==8){
Intent i = new Intent(Ident.this,Autorizacion.class);
startActivity(i);
}
else{
showDialog(DIALOG_ERROR_CANDIDATO);
}
}
});
}
protected Dialog onCreateDialog (int id){
switch (id) {
case DIALOG_ERROR_CANDIDATO:
return new AlertDialog.Builder(this)
.setIcon(R.drawable.ic_error)
.setTitle(R.string.error)
.setMessage("The NIF is" +user +DNIWord (user)+ "-")
.setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
})
.create();
}
return null;
}
}
user
is a class variable, DNIWord
is just a method. Modify the call so it is like this:
.setMessage("The NIF is" +user +DNIWord (user)+ "-")
DNIWord though works with a shadowed user
variable, so
("The NIF is" +user +DNIWord (user)+ "-")
could be shortened to just
("The NIF is" + DNIWord (user)+ "-")
However, since user
seems to be class-scoped and you try to get the value of dniText
, which seems to be an EditText/TextView
, it is very likely that you will also get a NullPointerException
when you actually run your app. Move the assignment of user to somewhere like inside
onCreateDialog()
.
Edit: user
is not class scoped but instead local to onCreate()
local variables, like the name implies are local to the enclosing method.
Move user outside onCreate()
then take out the int
user in onCreate and write it just as user
.
int user = 0; //class instance variable
public void onCreate(Bundle icicle)
{
//...
try{
user = Integer.parseInt(dniText.getText().toString()); //no "int", just user
}
catch (NumberFormatException e)
{
}
}
Then in your onCreateDialog()
method, you should be able to access user
.