Error sending an int parameter through an Intent

Asked

Viewed 343 times

1

Guys I’m making an app that gets a user code and it’s in hexadecimal. I convert it to decimal and insert it into Shared preference. So far so good.

At the time he will send the parameter to the other Activity gives error:

03-18 17:59:47.071: E/androidruntime(1983): FATAL EXCEPTION: main 03-18 17:59:47.071: E/AndroidRuntime(1983): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.testegerador/com.example.testegerador.Telagerador}: android.content.res.Resources$NotFoundException: String resource ID #0xa 03-18 17:59:47.071: E/androidruntime(1983): at android.app.Activitythread.performLaunchActivity(Activitythread.java:2180) 03-18 17:59:47.071: E/androidruntime(1983): at android.app.Activitythread.handleLaunchActivity(Activitythread.java:2230) 03-18 17:59:47.071: E/androidruntime(1983): at android.app.Activitythread.access$600(Activitythread.java:141) 03-18 17:59:47.071: E/androidruntime(1983): at android.app.Activitythread$H.handleMessage(Activitythread.java:1234) 03-18 17:59:47.071: E/androidruntime(1983): at android.os.Handler.dispatchMessage(Handler.java:99) 03-18 17:59:47.071: E/androidruntime(1983): at android.os.Looper.loop(Looper.java:137) 03-18 17:59:47.071: E/androidruntime(1983): at android.app.Activitythread.main(Activitythread.java:5039) 03-18 17:59:47.071: E/Androidruntime(1983): at java.lang.reflect.Method.invokeNative(Native Method) 03-18 17:59:47.071: E/Androidruntime(1983): at java.lang.reflect.Method.invoke(Method.java:511) 03-18 17:59:47.071: E/androidruntime(1983): at com.android.Internal.os.Zygoteinit$Methodandargscaller.run(Zygoteinit.java:793) 03-18 17:59:47.071: E/androidruntime(1983): at com.android.Internal.os.Zygoteinit.main(Zygoteinit.java:560) 03-18 17:59:47.071: E/androidruntime(1983): at Dalvik.system.Nativestart.main(Native Method) 03-18 17:59:47.071: E/androidruntime(1983): Caused by: android.content.res.Resources$Notfoundexception: String Resource ID #0xa 03-18 17:59:47.071: E/androidruntime(1983): at android.content.res.resources.gettext(Resources.java:230) 03-18 17:59:47.071: E/androidruntime(1983): at android.widget.Toast.makeText(Toast.java:265) 03-18 17:59:47.071: E/Androidruntime(1983): at com.example.testegenerator.Telagerador.onCreate(Telagerador.java:28) 03-18 17:59:47.071: E/androidruntime(1983): at android.app.Activity.performCreate(Activity.java:5104) 03-18 17:59:47.071: E/Androidruntime(1983): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 03-18 17:59:47.071: E/androidruntime(1983): at android.app.Activitythread.performLaunchActivity(Activitythread.java:2144) 03-18 17:59:47.071: E/Androidruntime(1983): ... 11 more

And get out of the application.

The part of the code that gives error is this, because when I leave this as comment the app runs normally:

@Override
public void onResume () { //Verifica se o usuário é licenciado ou não
 super.onResume(); 

    //teste para ver se o sharedpreference codfinal nao esta nulo mas esta vazio ("")
    int teste = cod_final.getInt("codfinal",0);

    //verificacao de primeiro launcher
    //  if (sPreferences.getBoolean("firstRun", true)) {

    //verificacao se o usuario digitou o codigo final se ele estiver null suponhamos que ele ainda nao validou a licenca pois nao digitou o codigo fornecido na ligacao
    if(cod_final==null || teste==0){


     //mensagem de não licenciado           
      final ProgressDialog ringProgressDialog = ProgressDialog.show(MainActivity.this, "Você não está licenciado!", "Processando sua licença ...", true);
      ringProgressDialog.setCancelable(true);
      new Thread(new Runnable() {   
       @Override
       public void run() {
        try {
         Thread.sleep(10000);
        } catch (Exception e) {
          }
        ringProgressDialog.dismiss();
        }           
       }).start();


      //captura o IMEI do dispositivo           
      TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE); 
      String IMEI = telephonyManager.getDeviceId().toString();
      licenca.setText(IMEI);


       //marca como false o sharedpreference: quer dizer que usuario esta licenciado
      //sPreferences.edit().putBoolean("firstRun", false).apply();

       Toast.makeText(getApplicationContext(), "codigo final NULO", Toast.LENGTH_LONG).show();

    } /*Fim if(cod_final==null || teste==""){*/

    else{/*se houver codigo final quer dizer que o usuario esta licenciado
           entao abre a tela para gerar os codigos enviando o parametro que é o numero do cod_final
           que aparecera para o usuario como licencas restantes*/                    
                Toast.makeText(getApplicationContext(), "codigo final NAO nulo", Toast.LENGTH_LONG).show();

                //pega o conteudo da sharedpreference e converte para string
               int licencas_resto = cod_final.getInt("codfinal",0);


               //aqui mostra o que realmente está no sharedpreference que no caso é um valor int , 
               //que é o valor hex digitado pelo usuario convertido em decimal. 
               //Isso está numa outra parte do código, não coloquei aqui porque está funcionando normalmente. 
               Log.i("CODIGO FINAL", "o que esta inserido em cod final" + licencas_resto); 

               //abre tela de geração de códigos
                Intent X = new Intent("tela_geralicencas");//chama a tela de geracao de codigo
                X.addCategory("gerar");
                Bundle params = new Bundle();
                params.putInt("mensagem",licencas_resto);
                //params.putString("mensagem", licencas_resto);
                X.putExtras(params);
                startActivity(X);
            }/*Fim else*/
  }/*Fim   public void onResume () {*/`

Code of the other Activity:

package com.example.testegerador;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

public class Telagerador extends Activity {

  TextView tvResto;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_telagerador);

      tvResto=(TextView)findViewById(R.id.textView5);

      //Recebe o parametro da outra tela que no caso e o numero do cod final
      Intent intent = getIntent();
      Bundle params = intent.getExtras();   
      if(params!=null)
      {   
       //converte o valor recebido pelo parametro em string   
        int mostraTexto = params.getInt("mensagem");

        Toast.makeText(getApplicationContext(), mostraTexto, Toast.LENGTH_LONG).show();

       //insere no textView o valor de licenças restantes
        tvResto.setText(mostraTexto);
      }/*Fim if(params!=null)*/

  }/*Fim OnCreate*/

}/*Fim  public class Telagerador extends Activity {*/

Could someone help me? The guys are right I don’t know what happens.

Just remembering I am beginner in this area. From now on, thank you!

  • A suggestion to find the error would be to create a breakpoint at the beginning of the method, and when calling the method, go through line by line to find out which line generates the FATAL EXCEPTION.

  • breakpoint would be the debug?

  • Breakpoint would be the instruction that the program will stop for you to analyze. When the program reaches the breakpoint, the IDE will display the data existing in the class BEFORE the execution of that instruction. Then you can know exactly what instruction does the FATAL EXCEPTION.

  • Put your onCreate() code from the Telagerador.java. And at first you’re using, in a Toast, a Resource String that doesn’t exist.

  • I put Luidne. I’ll try muting.

  • @I mutlei I put the breakpoints and rotated the whole part of the screen call. Before going through the keys that close Else and Onresume the debug goes to some strange files and to the Threadgroup.java file specifically on the line if (parent != null) {. And then there’s the bug in the logcat.

  • When he gets on the line if (parent != null), what is the value of Parent?

Show 2 more comments

1 answer

0


I believe you’re making a mistake when you’re trying to pass values. Check the code below.

Send license code

    startActivity(new Intent(getApplicationContext(), Telagerador.class) // Acredito que Telagerador seja a tela que você deseja receber valores...
            .putExtra("licencas", licencas_resto);

Receive license code

    int mostrarTexto = getIntent().getIntExtra("licencas", -1);
    tvResto.setText(String.valueOf(mostrarTexto));

See if it works. I analyzed your code, and the only thing me vi incorrect was the way you called the Activity, because I didn’t find any class with the name tela_geralicencas.

But if this doesn’t solve your problem, leave a comment I will redo the answer to give you a solution.

  • It worked! Thank you @array!

  • It’s nothing, @Kaamis! =]

  • I’m sorry for the mistakes in the code, I did in the notebook. haha!

  • 1

    Magina, helped a lot!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.