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

Deu me este erro:

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) 
4
  • Você não conseguiu identificar o erro através do LogCat? Commented 23/06/2017 às 18:34
  • Como é que faço isso ? Commented 23/06/2017 às 18:42
  • No canto inferior esquerdo do AndroidStudio, aonde tem Run, Debug e etc... uma das abas é o AndroidMonitor, nele vai mostrar os erros Commented 23/06/2017 às 19:00
  • ja editei conssegue me dizer , com esta informação, porque fecha? Commented 23/06/2017 às 20:01

2 Respostas 2

4

Você tem instanciar as views dentro do onCreate. Veja como deve ficar:

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);
    .
    .
    .
0
1

Pedro,

Esse erro acontece, porque você está declarando os componentes do Layout, antes mesmo de declarar o layout.

Essas referências, devem ficar dentro do onCreate e depois de setar o layout, por exemplo:

 @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

}

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .