App Closes when installing when I will test

Asked

Viewed 43 times

1

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

I did 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 looked here and I 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, that’s all 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>

2 answers

2

Hello, the problem has been solved

I had this problem and was told to put "setContentView(R.layout.activity_main);" in my code, because it was missing, however, I don’t 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 found and removed this "Abstract class".

Now it’s working. Thank you all

Ps. don’t know where to close the thread :)

0

The error occurs when, even before the Activity be created, you are assigning a value that does not yet exist of the layout Ids. Learn more about the Lifecycle android.

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....
  • Unfortunately it didn’t work, the strange thing is that if I create a new App and don’t program anything it works normal, but as soon as I program it gives that error

  • I copied your code and had the same problem that you had on line 13 (at with.example.Emperor.idadedecachorro.Mainactivity.<init>(Mainactivity.java:13)) If you click on the single blue link, as your code was, it will indicate an error on the Edittext Idadecachorro startup. Putting the code as it is in my answer, ran normally. You can enter the Logcat, for me to analyze?

  • Another detail, in the variable creation convention, the first letter should always be lowercase. Uppercase letter in first letter, only for Classes. :)

  • Nothing, I do not know if it has to do, but my logcat does not stop, a message appears between them many E/ . Tipo assim:&#xA; E/SensorService: activeConnections...&#xA;D/SensorService: SensorService hasOneShotSensors handle:1 mode:0&#xA;E/SensorService: activeConnections...&#xA; D/SensorService: SensorService hasOneShotSensors handle:5 mode:1&#xA; - W/ActivityManager: Skip forceProcessStateUpTo() to newState 8

  • You can filter your logcat for your project package. On the upper right side of the logcat view, you will find a combo box, it will probably be written "No Filters". Select the "Edit Filter Configuration", on the creation screen, you can modify the name of the filter and in the "Package Name" field, enter your package "with.example.emperor.idadedecachorro". If you have questions, see this video.

Browser other questions tagged

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