Android studio error: Unfortunately “App” has Stopped. How to solve this?

Android studio error: Unfortunately “App” has Stopped. How to solve this?

我是 android 的编程新手,目前我正在学习它,当我 运行 这个应用程序时它停止了。 这是我的 Logcat ::

12-29 00:35:09.510 3582-3582/? E/AndroidRuntime: FATAL EXCEPTION: main
                                             Process: com.smartrix.learning4, PID: 3582
                                             java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.smartrix.learning4/com.smartrix.learning4.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:68)
                                                 at android.support.v7.app.AppCompatDelegateImplV7.<init>(AppCompatDelegateImplV7.java:145)
                                                 at android.support.v7.app.AppCompatDelegateImplV11.<init>(AppCompatDelegateImplV11.java:28)
                                                 at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:41)
                                                 at android.support.v7.app.AppCompatDelegateImplV23.<init>(AppCompatDelegateImplV23.java:29)
                                                 at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:186)
                                                 at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:170)
                                                 at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:502)
                                                 at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:174)
                                                 at com.smartrix.learning4.MainActivity.<init>(MainActivity.java:16)
                                                 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) 

这是我的 Main_Activity.java ::

package com.smartrix.learning4;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.support.v4.view.GestureDetectorCompat;

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

private GestureDetectorCompat GD;
private TextView UserText = (TextView)findViewById(R.id.Nametext);

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

    // Reference the XML code button with JAVA code button
    Button button = (Button)findViewById(R.id.Center);
    Button button2 = (Button)findViewById(R.id.Right);
    Button button3 = (Button)findViewById(R.id.Left);
    Button button4 = (Button)findViewById(R.id.Bottom);
    Button button5 = (Button)findViewById(R.id.Top);
    this.GD = new GestureDetectorCompat(this,this);
    GD.setOnDoubleTapListener(this);

    //adding listener event
    button.setOnClickListener(
            new Button.OnClickListener(){
                    public void onClick(View v){
                        UserText.setText("CENTER");
                    }
            }
    );

    button.setOnLongClickListener(
            new Button.OnLongClickListener(){
                @Override
                public boolean onLongClick(View v) {
                    UserText.setText("It's a Long Click");
                    return false;
                }
            }
    );

    button2.setOnClickListener(
            new Button.OnClickListener(){
                public void onClick(View v) {
                    UserText.setText("RIGHT");
                }
            }
    );

    button3.setOnClickListener(
            new Button.OnClickListener(){
                public void onClick(View v) {
                    UserText.setText("LEFT");
                }
            }
    );

    button4.setOnClickListener(
            new Button.OnClickListener(){
                public void onClick(View v) {
                    UserText.setText("BOTTOM");
                }
            }
    );

    button5.setOnClickListener(
            new Button.OnClickListener(){
                public void onClick(View v) {
                    UserText.setText("TOP");
                }
            }
    );

}

@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
    UserText.setText("onSingleTapConfirmed");
    return true;
}

@Override
public boolean onDoubleTap(MotionEvent e) {
    UserText.setText("onDoubleTap");
    return true;
}

@Override
public boolean onDoubleTapEvent(MotionEvent e) {
    UserText.setText("onDoubleTapEvent");
    return true;
}

@Override
public boolean onDown(MotionEvent e) {
    UserText.setText("onDown");
    return true;
}

@Override
public void onShowPress(MotionEvent e) {
    UserText.setText("onShowPress");
}

@Override
public boolean onSingleTapUp(MotionEvent e) {
    UserText.setText("onSingleTapUp");
    return true;
}

@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
    UserText.setText("onScroll");
    return true;
}

@Override
public void onLongPress(MotionEvent e) {
    UserText.setText("onLongPress");
}

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
    UserText.setText("onFling");
    return true;
}

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

}

如何找到"App Stopped working"的原因? main_activity代码有问题吗? 我能做些什么来解决这个问题? 请帮助我......

您在 MainActivity 完全初始化并准备好查找视图之前将值分配给 UserText。

这样做。

..
private TextView userText ;  // declare here(this is your UserText)

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

    userText = (TextView)findViewById(R.id.Nametext); //assign value here
    ..
}