java.lang.Nullpointerexception error on android application

Asked

Viewed 679 times

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();

            }

        });
    }
}
  • 1

    Post the error log..

  • 1

    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.

  • 3

    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)

  • Actually it stops the app when I click the back button, it would load the main screen and it’s not rolling

1 answer

1

Try it this way then:

Classes

public class PrimeiroLayout extends Activity {

    private Button botao;
    private RelativeLayout relative;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout);
        relative = (RelativeLayout) findViewById(R.id.relative);
        relative.setBackgroundColor(Color.parseColor("#2095F2"));
        botao = (Button) findViewById(R.id.botao);
        botao.setText("Ir Segunda Tela");
        botao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                carregaSegundoLayout();
            }
        });
    }

    /**
     * Este metodo carrega o segundo layout com backgroud de cor amarela
     */
    public void carregaSegundoLayout() {
        Intent i = new Intent(PrimeiroLayout.this, SegundoLayout.class);
        startActivity(i);
        overridePendingTransition(android.R.anim.fade_in,android.R.anim.fade_out);
    }

    public static class SegundoLayout extends AppCompatActivity {

        private Button botao;
        private RelativeLayout relative;

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

            relative = (RelativeLayout) findViewById(R.id.relative);
            relative.setBackgroundColor(Color.parseColor("#FEC107"));
            botao = (Button) findViewById(R.id.botao);
            botao.setText("Voltar Primeira Tela");
            botao.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    carregaPrimeiroLayout();
                }
            });
        }

        /**
         * Este metodo volta para o primeiro layout com backgroud de cor azul
         */
        public void carregaPrimeiroLayout() {
            super.onBackPressed();
            overridePendingTransition(android.R.anim.fade_in,android.R.anim.fade_out);
        }
    }
}

Layout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relative"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/botao"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:layout_centerInParent="true"
        android:text="New Button" />
</RelativeLayout>

Manifest

    <activity
        android:name=".PrimeiroLayout"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity
        android:name=".PrimeiroLayout$SegundoLayout"
        android:label="@string/app_name">
    </activity>

Screenshots

inserir a descrição da imagem aqui

  • Valew man is working out here

  • @Mika if my answer answered your question, mark it as answered. Need anything else, give a shout here. Bro hugs

Browser other questions tagged

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