Nullpointerexception error in the Java language using Android Studio

Asked

Viewed 47 times

-1

I’m using android studio to program apps and recently all apps I try to program, while trying to run on emulator and real device, receive the same error.

04/23 00:21:55: Launching app
$ adb shell am start -n "com.lucasrodrigues.gasolinaoualcool/com.lucasrodrigues.gasolinaoualcool.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Client not ready yet..Waiting for process to come online
Connected to process 4121 on device motorola-moto_g_6__plus-0046152726
Capturing and displaying logcat messages from application. This behavior can be disabled in the "Logcat output" section of the "Debugger" settings page.
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.lucasrodrigues.gasolinaoualcool, PID: 4121
    java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.lucasrodrigues.gasolinaoualcool/com.lucasrodrigues.gasolinaoualcool.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
        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 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
        at android.support.v7.app.AppCompatDelegateImpl.<init>(AppCompatDelegateImpl.java:249)
        at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:182)
        at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:520)
        at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:191)
        at com.lucasrodrigues.gasolinaoualcool.MainActivity.<init>(MainActivity.java:13)
        at java.lang.Class.newInstance(Native Method)
        at android.app.Instrumentation.newActivity(Instrumentation.java:1195)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2721)
        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) 

I’ve changed the Ids and I’ve redid the code but nothing changes. My mainActivity is this:

package com.lucasrodrigues.gasolinaoualcool;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    //Recuperar os widgets
    private Button botao = (Button) findViewById(R.id.botaoVerificaId);
    private EditText valorAlcool = (EditText) findViewById(R.id.numeroAlcoolId);
    private EditText valorGasolina = (EditText) findViewById(R.id.numeroGasolinaId);
    private TextView caixaTextoResultado = (TextView) findViewById(R.id.resultadoId);
    private double resultado = 0;


//Método principal
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Evento de botão - Clicar
        botao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                //Recuperar os valores digitados convertendo para String
                String textoGasolina = valorGasolina.getText().toString();
                String textoAlcool   = valorAlcool.getText().toString();

                //Converter de String para número
                Double precoGasolina = Double.parseDouble( textoGasolina );
                Double precoAlcool = Double.parseDouble( textoAlcool );

                //Calculo
                resultado = precoAlcool/precoGasolina;

                //Condicional
                if(resultado > 0.7){

                    caixaTextoResultado.setText("Melhor utilizar a Gasolina");

                }else{

                    caixaTextoResultado.setText("Melhor utilizar o Alcool");
                }



            }
        });


    }
}

I can’t solve for anything and it’s happening in any code I write. Any project, it’s the same mistake.

1 answer

1

I believe the problem is occurring because you are trying to get the elements at the root of the class, before the onCreate, that is, before executing the setContentView(R.layout.activity_main);

//Recuperar os widgets
private Button botao = (Button) findViewById(R.id.botaoVerificaId);
private EditText valorAlcool = (EditText) findViewById(R.id.numeroAlcoolId);
private EditText valorGasolina = (EditText) findViewById(R.id.numeroGasolinaId);
private TextView caixaTextoResultado = (TextView) findViewById(R.id.resultadoId);
private double resultado = 0;

So when the onCreate wheel he performs his setContentView(R.layout.activity_main); and you try to use var botao that is void.

Possible solution

public class MainActivity extends AppCompatActivity {

    //Recuperar os widgets
    private Button botao;                // <- talvez tenha que colocar = null;
    private EditText valorAlcool;        // <- 
    private EditText valorGasolina;      // <-  
    private TextView caixaTextoResultado;// <- 
    private double resultado = 0;


    //Método principal
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        /// fazer a associação dentro da onCreate, apos o setContentView
        botao = (Button) findViewById(R.id.botaoVerificaId);            // <-
        valorAlcool = (EditText) findViewById(R.id.numeroAlcoolId);     // <-
        valorGasolina = (EditText) findViewById(R.id.numeroGasolinaId); // <-
        caixaTextoResultado = (TextView) findViewById(R.id.resultadoId);// <-

        //Evento de botão - Clicar
        botao.setOnClickListener(new View.OnClickListener() {


       ...
  • 1

    Thank you very much Icaro Martins, that was the mistake. I was trying to reference the Id’s before the onCreate method. Now everything solved.

Browser other questions tagged

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