0
In this code occurs the cited error, pointing to the method CarregarTelaPrincipal()
. Why is it happening?
package com.example.usuario.myapp;
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
public class MyActivity extends Activity {
ImageButton button1;
Button btvoltar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
CarregarTelaPrincipal();
}
public void CarregarTelaPrincipal(){
button1 = (ImageButton) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CarregarTela1();
}
});
}
public void CarregarTela1(){
setContentView(R.layout.tela_1);
btvoltar = (Button) findViewById(R.id.btvoltar);
btvoltar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CarregarTelaPrincipal();
}
});
}
}
Post the error log..
– Marco Giovanni
Mika, by the description you placed (the ideal would be the complete log of the error) the R.id.button1 object was not found by the findViewById method and therefore returns a null pointer. When accessing this is generated a Nullpointerexception because it is accessing an undefined memory location.
– Bruno Bermann
According to Google documentation, it is not advisable for you to reuse an Activity. The interesting thing would be for you to create another Activity for you to use
setContentView(R.layout.tela_1)
– viana
Actually it stops the app when I click the back button, it would load the main screen and it’s not rolling
– Mika