2016-10-19 20 views
1

私は現在、アプリケーション開発を学んでいますので、私はかなり初心者です。ヌルオブジェクト参照で仮想メソッド ''を呼び出そうとしました

E/AndroidRuntime: FATAL EXCEPTION: main 
       Process: bakaev.myapplication, PID: 3888 
       java.lang.RuntimeException: Unable to start activity ComponentInfo{bakaev.myapplication/bakaev.myapplication.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.ViewGroup.setOnTouchListener(android.view.View$OnTouchListener)' on a null object reference 
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416) 
        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 'void android.view.ViewGroup.setOnTouchListener(android.view.View$OnTouchListener)' on a null object reference 
        at bakaev.myapplication.MainActivity.onCreate(MainActivity.java:24) 
        at android.app.Activity.performCreate(Activity.java:6237) 
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 
        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)  

次のように私のMainActivityは次のとおりです:

public class MainActivity extends AppCompatActivity { 


private ViewGroup testapp; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    testapp = (ViewGroup) findViewById(R.id.activity_main); 

    testapp.setOnTouchListener(
      new RelativeLayout.OnTouchListener(){ 
       @Override 
       public boolean onTouch(View v, MotionEvent event){ 
        moveButton(); 
        return true; 
       } 
      } 
    ); 
    setContentView(R.layout.activity_main); 

} 


public void moveButton(){ 

    View Button = findViewById(R.id.ChangeButton); 

    TransitionManager.beginDelayedTransition(testapp); 

    RelativeLayout.LayoutParams positionrules = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 
      RelativeLayout.LayoutParams.WRAP_CONTENT); 
    positionrules.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE); 
    positionrules.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE); 
    Button.setLayoutParams(positionrules); 

    ViewGroup.LayoutParams size = Button.getLayoutParams(); 
    size.width = 450 ; 
    size.height = 300; 
    Button.setLayoutParams(size); 

} 

}

グレード私はそこには、構文エラーがありませんが、私は、私は次の取得アプリを実行すると、TheNewBostonのチュートリアルを、以下のよビルドしている:

 minSdkVersion 19 
    targetSdkVersion 24 

Androidのマニフェストがあります。

 android:theme="@style/Theme.AppCompat.Light"> 

に追加されました。

Activity_main.xmlは次のとおりです。findViewByIdをやった後に使用される

<?xml version="1.0" encoding="utf-8"?> 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="bakaev.myapplication.MainActivity">

<Button 
     android:text="Change Me" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:id="@+id/ChangeButton" /> 
</RelativeLayout> 

答えて

0
@Override 
protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
// set view content before doing findViewById. 
setContentView(R.layout.activity_main) 
testapp = (ViewGroup) findViewById(R.id.activity_main); 

削除setContentview。

0

findViewByIdに電話する前にsetContentViewに電話する必要があります。

Activity#findViewById

のJavaDocを1として表示 findViewById(int型のID)

はのonCreate(バンドル)で処理されたXMLからid属性により同定されたビューを検索します。

1

setContentViewは、inflated xmlレイアウトへの参照を行う前に呼び出す必要があります。 onCreate()メソッドで

testapp = (ViewGroup) findViewById(R.id.activity_main)

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

testapp = (ViewGroup) findViewById(R.id.activity_main); 

testapp.setOnTouchListener(
     new RelativeLayout.OnTouchListener(){ 
      @Override 
      public boolean onTouch(View v, MotionEvent event){ 
       moveButton(); 
       return true; 
      } 
     } 
); 

}

0

使用この行を

setContentView(R.layout.activity_main)

:したがって、あなたのOnCreateイベントは次のようになります。

関連する問題

 関連する問題