I've done pretty much the same way, changing only the names of the variables in another file of the same project. However, on reaching the line to call the class the application stops and gives error.
Below is the part where I hit the variables and call the class:
//IMEI de teste
IMEI = "005298741256897";
String licenca_final = (IMEI.substring(0, 4) + "-" + IMEI.substring(4, 8) + "-" + IMEI.substring(8, 12) + "-" + String.format("%4s", IMEI.substring(12, 15)).replace(' ', '0')).toUpperCase();
//Pega o que o IMEI e insere no array de String cod2
String[] cod2 = new String[licenca_final.length()];
for (int i = 0; i < licenca_final.length(); i++) {
cod2[i] = (licenca_final.substring(i,i+1));
}
ClasGenerateRegCod instancia = new ClasGenerateRegCod();
String resposta = null;
try {
resposta = instancia.GenerateRegCod(cod2, "PRINCIPIODACORRESPONDENCIA");
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
licenca.setText(resposta);
And below the errors that appeared in LOGCAT:
05-27 13:37:19.682: D/AndroidRuntime(1967): Shutting down VM
05-27 13:37:19.682: W/dalvikvm(1967): threadid=1: thread exiting with uncaught exception (group=0xb2ac5ba8)
05-27 13:37:20.362: E/AndroidRuntime(1967): FATAL EXCEPTION: main
05-27 13:37:20.362: E/AndroidRuntime(1967): Process: com.example.minhaslicencasnobre, PID: 1967
05-27 13:37:20.362: E/AndroidRuntime(1967): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.minhaslicencasnobre/com.example.minhaslicencasnobre.Verificacao}: java.lang.ArrayIndexOutOfBoundsException: length=46; index=46
05-27 13:37:20.362: E/AndroidRuntime(1967): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
05-27 13:37:20.362: E/AndroidRuntime(1967): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-27 13:37:20.362: E/AndroidRuntime(1967): at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-27 13:37:20.362: E/AndroidRuntime(1967): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-27 13:37:20.362: E/AndroidRuntime(1967): at android.os.Handler.dispatchMessage(Handler.java:102)
05-27 13:37:20.362: E/AndroidRuntime(1967): at android.os.Looper.loop(Looper.java:136)
05-27 13:37:20.362: E/AndroidRuntime(1967): at android.app.ActivityThread.main(ActivityThread.java:5017)
05-27 13:37:20.362: E/AndroidRuntime(1967): at java.lang.reflect.Method.invokeNative(Native Method)
05-27 13:37:20.362: E/AndroidRuntime(1967): at java.lang.reflect.Method.invoke(Method.java:515)
05-27 13:37:20.362: E/AndroidRuntime(1967): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-27 13:37:20.362: E/AndroidRuntime(1967): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-27 13:37:20.362: E/AndroidRuntime(1967): at dalvik.system.NativeStart.main(Native Method)
05-27 13:37:20.362: E/AndroidRuntime(1967): Caused by: java.lang.ArrayIndexOutOfBoundsException: length=46; index=46
05-27 13:37:20.362: E/AndroidRuntime(1967): at com.example.minhaslicencasnobre.ClasGenerateRegCod.GenerateRegCod(ClasGenerateRegCod.java:101)
05-27 13:37:20.362: E/AndroidRuntime(1967): at com.example.minhaslicencasnobre.Verificacao.onCreate(Verificacao.java:97)
05-27 13:37:20.362: E/AndroidRuntime(1967): at android.app.Activity.performCreate(Activity.java:5231)
05-27 13:37:20.362: E/AndroidRuntime(1967): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-27 13:37:20.362: E/AndroidRuntime(1967): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
05-27 13:37:20.362: E/AndroidRuntime(1967): ... 11 more
I could not identify what is wrong with the other file that I also called the class worked on, its difference is in the output of the return that I did not format to show and the insertion of a if
for wrong code verification that if true it shows a message, otherwise it shows the returned code in ####-####-####-####
format; below:
'//Pega o que o usuário digitou e insere no array de String cod2
String[] cod2 = new String[cod.getText().toString().length()];
for (int i = 0; i < cod.getText().toString().length(); i++) {
cod2[i] = (cod.getText().toString().substring(i,i+1));
}
//Chama a classe GenerateRegCod para executar o algoritmo de criptografia
try {
ClasGenerateRegCod instancia = new ClasGenerateRegCod();
String m = instancia.GenerateRegCod(cod2,"PRINCIPIODACORRESPONDENCIA");
if(m==""){//Se o código for diferente de 16 caracteres a classe retornará vazio e então o código é dado como inválido
//Mostra mensagem: código inválido
AlertDialog alerta;
//Cria o gerador do AlertDialog
AlertDialog.Builder builder = new AlertDialog.Builder(this);
//define o titulo
builder.setTitle("Erro");
//define a mensagem
builder.setMessage("Código inválido.");
//define um botão como positivo
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
//conteudo do botão
cod.setText("");
}
});
//cria o AlertDialog
alerta = builder.create();
//Exibe
alerta.show();
}else{//Se o código não for inválido
//Insere o código retornado da classe em 4 grupos de 4 caracteres separados por '-' na variável finale
finale = m.substring(0,4) + "-" + m.substring(4,8) + "-" + m.substring(8, 12) + "-" + m.substring(12);
'