problems in passing data to new Activity

Asked

Viewed 113 times

0

I’m having difficulty when I pass a dice using the put. Extras.

My app is crashing and I can’t identify the reason.

He is unable to pass the data from one Activity to another.

Activity that sends the data:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_settings);

    edtNameField = (EditText) findViewById(R.id.edtName);
    edtPhoneField = (EditText) findViewById(R.id.edtPhone);
    edtCompanySettings = (EditText) findViewById(R.id.edtCompanySettings);
    edtEmailSettings = (EditText) findViewById(R.id.edtEmailSettings);
    pd = new SpotsDialog(this, R.style.CustomPD);

    imgProfileImage = (ImageView) findViewById(R.id.imgProfileImage);

    btnBack = (Button) findViewById(R.id.btnBack);
    btnConfirm = (Button) findViewById(R.id.btnConfirm);

    mAuth = FirebaseAuth.getInstance();
    userID = mAuth.getCurrentUser().getUid();
    mCustomerDatabase = FirebaseDatabase.getInstance().getReference().child("Users").child(userID);

    getUserInfo();

    imgProfileImage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(Intent.ACTION_PICK);
            intent.setType("image/*");
            startActivityForResult(intent, 1);
        }
    });

    btnConfirm.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            pd.show();
            saveUserInformation();
            //enviarDados();
            enviarDadosShare();
            Toast.makeText(SettingsActivity.this, "Os dados foram salvos!", Toast.LENGTH_LONG).show();
        }
    });

    btnBack.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            finish();
            return;
        }
    });
}

public void enviarDadosShare(){
    Intent enviarDados = new Intent(this, TelaPrincipalActivity.class);
    EditText nome = (EditText) findViewById(R.id.edtName);
    SharedPreferences prefs =  getSharedPreferences("preferencias",
            Context.MODE_PRIVATE);

    SharedPreferences.Editor ed =  prefs.edit();
    ed.putString("nome",  nome.getText().toString());
    ed.apply();
    Toast.makeText(getBaseContext(), "Gravado  com  sucesso", Toast.LENGTH_SHORT).show();
    enviarDados.putExtra("nome", nome.getText().toString());
    startActivity(enviarDados);
}

I’m trying to pass the data to my side menu

inserir a descrição da imagem aqui

This is my method to receive the data from the other Activity

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_tela_principal);

        inicializarComponentes();
        inicializarFirebase();
        eventoEdit();
    receberDadosSahred();

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.addDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

}

public void receberDadosSahred(){
    Intent intent2 = getIntent();

    if (intent2 != null){
        Bundle params = intent2.getExtras();

        if (params != null){
            TextView textView = (TextView) findViewById(R.id.textView);

            String str = intent2.getStringExtra("nome");

            textView.setText(str);
        }
    }
}

In debug mode:

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

Error that is presenting:

E/Androidruntime: FATAL EXCEPTION: main Process: homologa.cappta.com.br.capptahomologa, PID: 12973 java.lang.RuntimeException: Unable to start activity ComponentInfo{homologa.cappta.com.br.capptahomologa/homologa.cappta.com.br.capptahomologa.Activity.TelaPrincipalActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.Charsequence)' on a null Object Reference at android.app.Activitythread.performLaunchActivity(Activitythread.java:2830) at android.app.Activitythread.handleLaunchActivity(Activitythread.java:2909) at android.app.Activitythread. -wrap11(Unknown Source:0) at android.app.Activitythread$H.handleMessage(Activitythread.java:1606) at android.os.Handler.dispatchMessage(Handler.java:105) at android.os.Looper.loop(Looper.java:164) at android.app.Activitythread.main(Activitythread.java:6592) at java.lang.reflect.Method.invoke(Native Method) at com.android.Internal.os.Zygote$Methodandargscaller.run(Zygote.java:240) at com.android.Internal.os.Zygoteinit.main(Zygoteinit.java:769) Caused by: java.lang.Nullpointerexception: Attempt to invoke virtual method 'void android.widget.Textview.setText(java.lang.Charsequence)' on a null Object Reference at homologa.cappta.com.br.capptahomologa.Activity.Telaprincipalactivity.receivedSahred(Telaprincipalactivity.java:118) at homologa.cappta.com.br.capptahomologa.Activity.Telaprincipalactivity.onCreate(Telaprincipalactivity.java:91) at android.app.Activity.performCreate(Activity.java:6984) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1235) at android.app.Activitythread.performLaunchActivity(Activitythread.java:2783) at android.app.Activitythread.handleLaunchActivity(Activitythread.java:2909)  at android.app.Activitythread. -wrap11(Unknown Source:0)  at android.app.Activitythread$H.handleMessage(Activitythread.java:1606)  at android.os.Handler.dispatchMessage(Handler.java:105)  at android.os.Looper.loop(Looper.java:164)  at android.app.Activitythread.main(Activitythread.java:6592)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.Internal.os.Zygote$Methodandargscaller.run(Zygote.java:240)  at com.android.Internal.os.Zygoteinit.main(Zygoteinit.java:769)  E/UncaughtException: java.lang.RuntimeException: Unable to start activity ComponentInfo{homologa.cappta.com.br.capptahomologa/homologa.cappta.com.br.capptahomologa.Activity.TelaPrincipalActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Textview.setText(java.lang.Charsequence)' on a null Object Reference at android.app.Activitythread.performLaunchActivity(Activitythread.java:2830) at android.app.Activitythread.handleLaunchActivity(Activitythread.java:2909) at android.app.Activitythread. -wrap11(Unknown Source:0) at android.app.Activitythread$H.handleMessage(Activitythread.java:1606) at android.os.Handler.dispatchMessage(Handler.java:105) at android.os.Looper.loop(Looper.java:164) at android.app.Activitythread.main(Activitythread.java:6592) at java.lang.reflect.Method.invoke(Native Method) at com.android.Internal.os.Zygote$Methodandargscaller.run(Zygote.java:240) at com.android.Internal.os.Zygoteinit.main(Zygoteinit.java:769) Caused by: java.lang.Nullpointerexception: Attempt to invoke virtual method 'void android.widget.Textview.setText(java.lang.Charsequence)' on a null Object Reference at homologa.cappta.com.br.capptahomologa.Activity.Telaprincipalactivity.receivedSahred(Telaprincipalactivity.java:118) at homologa.cappta.com.br.capptahomologa.Activity.Telaprincipalactivity.onCreate(Telaprincipalactivity.java:91) at android.app.Activity.performCreate(Activity.java:6984) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1235) at android.app.Activitythread.performLaunchActivity(Activitythread.java:2783) at android.app.Activitythread.handleLaunchActivity(Activitythread.java:2909)  at android.app.Activitythread. -wrap11(Unknown Source:0)  at android.app.Activitythread$H.handleMessage(Activitythread.java:1606)  at android.os.Handler.dispatchMessage(Handler.java:105)  at android.os.Looper.loop(Looper.java:164)  at android.app.Activitythread.main(Activitythread.java:6592)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.Internal.os.Zygote$Methodandargscaller.run(Zygote.java:240)  at com.android.Internal.os.Zygoteinit.main(Zygoteinit.java:769)  Application terminated.

3 answers

1

The error is in the statement:

TextView nomeView = (EditText) findViewById(R.id.emailPrincipal);

It is a Textview, but after = you are putting (Edittext).

It has nothing to do with the Bundle or the Intent.

  • I made the change, but still keeps crashing the app :(

  • Same error? Have ctz? Take a look at Logcat if the error is the same

  • TextView nomeView = (TextView) findViewById(R.id.emailPrincipal);

0

I found some divergences in the print of your debug and the error message and the sent code.

First, make sure you’ve fixed the cast for the TextView, declared as nomeView. According to Leonardo Dias' answer.

Then the error message itself, since the error says cannot be cast to android.widget.EditText at. Indicating the Casting Fault Itself.

Note: Update your project and Android Studio, as this casting is redundant from API 26. Making reading easier, reducing and avoiding code errors

Then, in the print of your debug, the parameter params received by Intent. You are receiving the "name" key, different from the code String email = params.getString("email");.

To finish, confirm that the id is correct, because the print is id.txtNomePrinc and the code is id.emailPrincipal. Since even after the declaration of the Textview variable called nomeView , remains null according to its debug.

  • Rodrigo, I am trying to bring the information in my nav_header (onNavigationItemSelected) and I believe that for some reason he cannot find the id of my textview. I added a photo of where I’m trying to bring the information.

  • This is only a problem if you are not bringing the layout through the setContentView(R.layout.nav_header_tela_principal); Is this the Activity you’re using? The notes I mentioned, you analyzed?

  • I did look at the notes, I made the changes, and yet the mistake continues. The Activity I’m using is setContentView(R.layout.activity_tela_main); Will be if I have to create a new Activity and call that setContentView(R.layout.nav_header_tela_main); ?

  • If it’s a side tab menu, you don’t need to create an Activity for it. But I have the habit of inserting the navigation layout into my own main_activity. I don’t know exactly how you are doing.

  • It’s a side menu. Mine looks like this: protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_tela_principal);

 inicializarComponentes();
 inicializarFirebase();
 eventoEdit();

 Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
 setSupportActionBar(toolbar);

 DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
 ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
 this, drawe }

  • Can you Edit your question with this part? Already with the change of how your code is at the moment? If you can, change the error message you are receiving in the class where you are in error?

  • I edited my question with the changes, I am using API 27 in my project.

Show 2 more comments

0


Browser other questions tagged

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