java.lang.NullPointerException error in android application

0

In this code the mentioned error occurs, pointing to the CarregarTelaPrincipal() method. Why is it occurring?

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

            }

        });
    }
}
    
asked by anonymous 22.08.2016 / 20:41

1 answer

1

Try 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

    
23.08.2016 / 03:56