2016-12-28 52 views
0

我是新來的android編程,目前我正在學習它,當我運行這個應用程序停止。 這裏是我的logcat ::Android工作室錯誤:不幸的是,「應用程序」已停止。如何解決這個問題?

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代碼有問題嗎? 我能做些什麼來解決這個問題? 請幫幫我......

+0

你把MainActivity放在AndroidManifest.xml文件中? –

+0

是的,我在@Surace –

+0

是的,你在加載activity_main.xml之前給UserText賦值。這是個問題。 –

回答

0

在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 
    .. 
} 
相關問題