2016-10-19 6 views
1

현재 앱 개발을 배우고 있으므로 초보자입니다. 내가 TheNewBoston의 튜토리얼을 다음있어, 아무 구문 오류가 없다하지만 난 응용 프로그램을 실행할 때 나는 다음과 같은 얻을 :null 개체 참조에서 가상 메서드 ''을 (를) 호출하려고 시도했습니다.

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); 

} 

}

학년을 다음과 같이

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입니다 빌드 :

 minSdkVersion 19 
    targetSdkVersion 24 

Android Manifest의 내용은 다음과 같습니다.

 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으로 전화하십시오.

의 JavaDoc으로 당 대 Activity#findViewById

보기 findViewById를 (INT 아이디)

가에서 onCreate (번들)에서 처리 된 XML의 id 속성 의해 식별 된 도면을 찾아 .

1

팽창 된 xml 레이아웃에 대한 참조를 만들기 전에 setContentView를 호출해야합니다. 따라서, 귀하의에서 OnCreate는 다음과 같아야합니다

@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에서

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

() 메소드 전에.

관련 문제