I do not understand why application closes

1
package com.pedrogouveia.teste1;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.view.MotionEvent;
import android.view.GestureDetector;
import android.support.v4.view.GestureDetectorCompat;
import org.w3c.dom.Text;

public class MainActivity extends AppCompatActivity implements GestureDetector.OnGestureListener, GestureDetector.OnDoubleTapListener {

    //reference to xml widgets
    Button ChangeNameButton = (Button) findViewById(R.id.ChangeNameButton);
    final TextView DogText = (TextView) findViewById(R.id.DogText);
    final TextView FishText = (TextView) findViewById(R.id.FishText);
    final TextView ExplainingText = (TextView) findViewById(R.id.ExplainingText);
    private GestureDetectorCompat gestureDetector;


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

        setContentView(R.layout.activity_main);

        this.gestureDetector = new GestureDetectorCompat(this, this);
        gestureDetector.setOnDoubleTapListener(this);


        final String animais[] = {
            "Dog",
            "Cat",
            "Rat",
            "Horse",
            "Mouse",
            "Fish"
        };
        final int[] contador = {
            0
        };
        final int[] contador1 = {
            1
        };

        ChangeNameButton.setOnClickListener(
            new Button.OnClickListener() {
                @Override
                public void onClick(View view) {
                    final TextView FishText = (TextView) findViewById(R.id.FishText);
                    FishText.setText(animais[contador[0]]);
                    contador[0]++;
                    if (contador[0] == 5) {
                        contador[0] = contador[0] - 5;
                    };
                    final TextView DogText = (TextView) findViewById(R.id.DogText);
                    DogText.setText(animais[contador1[0]]);
                    contador1[0]++;
                    if (contador1[0] == 6) {
                        contador1[0] = contador1[0] - 5;
                    };
                    ExplainingText.setVisibility(View.VISIBLE);
                }


            });
        ChangeNameButton.setOnLongClickListener(
            new Button.OnLongClickListener() {
                public boolean onLongClick(View view) {
                    final TextView FishText = (TextView) findViewById(R.id.FishText);
                    FishText.setText("Im done animals go away!");

                    final TextView DogText = (TextView) findViewById(R.id.DogText);
                    DogText.setText("Thank you kiddo !");

                    ExplainingText.setVisibility(View.INVISIBLE);

                    return false;


                }
            }
        );
    }

    @Override
    public boolean onSingleTapConfirmed(MotionEvent motionEvent) {
        return true;
    }

    @Override
    public boolean onDoubleTap(MotionEvent motionEvent) {
        return true;
    }

    @Override
    public boolean onDoubleTapEvent(MotionEvent motionEvent) {
        return true;
    }

    @Override
    public boolean onDown(MotionEvent motionEvent) {
        return true;
    }

    @Override
    public void onShowPress(MotionEvent motionEvent) {

    }

    @Override
    public boolean onSingleTapUp(MotionEvent motionEvent) {
        return true;
    }

    @Override
    public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
        ExplainingText.setText("You are doomed!");
        DogText.setText("You piece of shit!");
        FishText.setText("Why the fuck you screwd my app you ?!");
        return true;
    }

    @Override
    public void onLongPress(MotionEvent motionEvent) {

    }

    @Override
    public boolean onFling(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
        return true;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        this.gestureDetector.onTouchEvent(event);
        return super.onTouchEvent(event);
    }
}

He gave me this error:

06 - 23 19: 26: 13.743 6524 - 6524 / com.pedrogouveia.teste1 E / 
AndroidRuntime: FATAL EXCEPTION: main
    Process: com.pedrogouveia.teste1, PID: 6524
    java.lang.RuntimeException: Unable to instantiate activity ComponentInfo {
        com.pedrogouveia.teste1 / com.pedrogouveia.teste1.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: 2327)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java: 2476)
    at android.app.ActivityThread. - wrap11(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java: 1344)
    at android.os.Handler.dispatchMessage(Handler.java: 102)
    at android.os.Looper.loop(Looper.java: 148)
    at android.app.ActivityThread.main(ActivityThread.java: 5417)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java: 726)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java: 616)
    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: 118)
    at android.support.v7.app.AppCompatDelegateImplV9. < init > (AppCompatDelegateImplV9.java: 152)
    at android.support.v7.app.AppCompatDelegateImplV11. < init > (AppCompatDelegateImplV11.java: 29)
    at android.support.v7.app.AppCompatDelegateImplV14. < init > (AppCompatDelegateImplV14.java: 53)
    at android.support.v7.app.AppCompatDelegateImplV23. < init > (AppCompatDelegateImplV23.java: 31)
    at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java: 202)
    at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java: 184)
    at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java: 518)
    at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java: 189)
    at com.pedrogouveia.teste1.MainActivity. < init > (MainActivity.java: 22)
    at java.lang.Class.newInstance(Native Method)
    at android.app.Instrumentation.newActivity(Instrumentation.java: 1067)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java: 2317)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java: 2476) 
    at android.app.ActivityThread. - wrap11(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java: 1344) 
    at android.os.Handler.dispatchMessage(Handler.java: 102) 
    at android.os.Looper.loop(Looper.java: 148) 
    at android.app.ActivityThread.main(ActivityThread.java: 5417) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java: 726) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java: 616) 
    
asked by anonymous 23.06.2017 / 20:30

2 answers

4

You have to instantiate views within onCreate . Here's how it should look:

public Button ChangeNameButton;
public TextView DogText;
public TextView FishText;
public TextView ExplainingText;
public GestureDetectorCompat gestureDetector;

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

    ChangeNameButton = (Button) findViewById(R.id.ChangeNameButton);
    DogText = (TextView) findViewById(R.id.DogText);
    FishText = (TextView) findViewById(R.id.FishText);
    TextView ExplainingText = (TextView) findViewById(R.id.ExplainingText);
    .
    .
    .
    
23.06.2017 / 22:10
1

Pedro,

This error happens because you are declaring Layout components, even before you declare the layout.

These references should stay within onCreate and after setting the layout, for example:

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

        setContentView(R.layout.activity_main);

        //reference to xml widgets
        Button ChangeNameButton = (Button) findViewById(R.id.ChangeNameButton);
        final TextView DogText = (TextView) findViewById(R.id.DogText);
        final TextView FishText = (TextView) findViewById(R.id.FishText);
        final TextView ExplainingText = (TextView) findViewById(R.id.ExplainingText);

        //resto do código

}
    
23.06.2017 / 22:10