簡體   English   中英

Android studio錯誤:不幸的是“ App”已停止。 如何解決呢?

[英]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);
}

}

如何找到“應用停止運行”的原因? 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
    ..
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM