App Closes when I'm going to test

1

I am a beginner and already in my first tutorial I have this error

I made the whole process as it is in the video tutorial, and when I try to test it on my phone, it compiles, installs, opens and closes the application.

I looked everywhere, I searched here and found nothing.

This is my logcat.

10-27 14:46:05.342 20653-20653/com.example.imperador.idadedecachorro E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.imperador.idadedecachorro, PID: 20653
    java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.imperador.idadedecachorro/com.example.imperador.idadedecachorro.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:2426)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2595)
        at android.app.ActivityThread.access$800(ActivityThread.java:178)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1470)
        at android.os.Handler.dispatchMessage(Handler.java:111)
        at android.os.Looper.loop(Looper.java:194)
        at android.app.ActivityThread.main(ActivityThread.java:5631)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)
     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.AppCompatDelegateImplBase.<init>(AppCompatDelegateImplBase.java:117)
        at android.support.v7.app.AppCompatDelegateImplV9.<init>(AppCompatDelegateImplV9.java:149)
        at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:56)
        at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:202)
        at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:183)
        at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:519)
        at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:190)
        at com.example.imperador.idadedecachorro.MainActivity.<init>(MainActivity.java:13)
        at java.lang.reflect.Constructor.newInstance(Native Method)
        at java.lang.Class.newInstance(Class.java:1606)
        at android.app.Instrumentation.newActivity(Instrumentation.java:1071)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2595) 
        at android.app.ActivityThread.access$800(ActivityThread.java:178) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1470) 
        at android.os.Handler.dispatchMessage(Handler.java:111) 
        at android.os.Looper.loop(Looper.java:194) 
        at android.app.ActivityThread.main(ActivityThread.java:5631) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at java.lang.reflect.Method.invoke(Method.java:372) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754) 
10-27 14:46:08.385 20653-20653/com.example.imperador.idadedecachorro I/Process: Sending signal. PID: 20653 SIG: 9

My MainActivity:

package com.example.imperador.idadedecachorro;

import android.annotation.SuppressLint;
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 {

    private EditText IdadeCachorro = findViewById(R.id.textoIdadeID);
    private Button BotaoCalcular = findViewById(R.id.botaoCalcularID);
    private TextView Resultado = findViewById(R.id.resultadoID);

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

        BotaoCalcular.setOnClickListener ( new View.OnClickListener ( ) {
            @SuppressLint ( "SetTextI18n" )
            @Override
            public
            void onClick ( View v ) {

                String TextoDigitado = IdadeCachorro.getText().toString();

                if(TextoDigitado.isEmpty()) Resultado.setText("Não digitaste nada babaca");
                else {

                    int IdadeDigitada = Integer.parseInt(TextoDigitado);
                    int ResultadoFinal = IdadeDigitada * 7;

                    Resultado.setText(ResultadoFinal + " Anos");


                }

            }
        } );

    }


}

My Android Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.imperador.idadedecachorro">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

My MainActivity, There's only this in the app

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#cafbab"
    tools:context=".MainActivity"
    tools:layout_editor_absoluteY="81dp">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="72dp"
        android:layout_marginBottom="12dp"
        android:layout_marginEnd="144dp"
        android:layout_marginLeft="144dp"
        android:layout_marginRight="144dp"
        android:layout_marginStart="144dp"
        android:layout_marginTop="16dp"
        android:contentDescription="@string/Cont_Descr_Imagem"
        app:layout_constraintBottom_toTopOf="@+id/textView2"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.289"
        app:srcCompat="@mipmap/ic_launcher_round" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="325dp"
        android:layout_height="0dp"
        android:layout_marginBottom="14dp"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="128dp"
        android:text="@string/Texto_informativo"
        android:textSize="18sp"
        app:layout_constraintBottom_toTopOf="@+id/textoIdadeID"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.526"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/textoIdadeID"
        android:layout_width="136dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="31dp"
        android:background="#f3fac7"
        android:ems="10"
        android:hint="@string/Hint_Texto_editavel"
        android:inputType="number"
        android:textSize="24sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toTopOf="@+id/botaoCalcularID"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView2" />

    <Button
        android:id="@+id/botaoCalcularID"
        android:layout_width="213dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="47dp"
        android:text="@string/Texto_botao_calcular"
        android:textSize="18sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toTopOf="@+id/resultadoID"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.503"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textoIdadeID" />

    <TextView
        android:id="@+id/resultadoID"
        android:layout_width="174dp"
        android:layout_height="0dp"
        android:layout_marginBottom="102dp"
        android:textColor="@android:color/background_dark"
        android:textSize="30sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/botaoCalcularID" />
</android.support.constraint.ConstraintLayout>
    
asked by anonymous 28.10.2018 / 16:05

2 answers

2

Hello, the problem has been resolved

I was having this problem and was told to put "setContentView (R.layout.activity_main);" in my code, since it was missing, however, I do not know when I ended up putting "Abstract class" in my class, and so the "setContentView (R.layout.activity_main);" did not help, after much trying I found and removed this "Abstract class".

Now it's working. Thank you all

Ps. I do not know where to close the topic:)

    
02.11.2018 / 19:33
0

The error occurs when, even before Activity is created, you are assigning a value that does not yet exist for layout IDs. Learn more about Android's Lifecycle .

Change your code to:

private EditText IdadeCachorro;
private Button BotaoCalcular;
private TextView Resultado;

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

    IdadeCachorro = findViewById(R.id.textoIdadeID);
    BotaoCalcular = findViewById(R.id.botaoCalcularID);
    Resultado = findViewById(R.id.resultadoID);
    //restante do código....
    
29.10.2018 / 15:30