2016-12-28 19 views
0

私はアンドロイドのプログラミングには慣れていませんが、現在私はそれを学んでおり、このアプリを実行すると停止しました。ここ は私のLogcatです::ここAndroidスタジオエラー:残念ながら「App」が停止しました。これを解決するには?

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

AndroidManifest.xmlファイルにMainActivityを入れましたか? –

+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 
    .. 
} 
関連する問題