2012-09-19 2 views
0

왜 그런지 알 수는 없지만 언제든지 E/AndroidRuntime(709): java.lang.NullPointerException을 계속 사용하면 예/아니요라는 제목의 버튼을 호출하려고합니다. 단추는 xml 레이아웃으로 만들어지고 findViewById 메서드를 사용하여 호출됩니다.이 메서드는 동일한 프로그램에서 올바르게 작동하는 여러 단추에서 사용한 것과 비슷한 기술입니다. 이 버튼과 나머지의 유일한 차이점은 XML 코드가 main.xml이 아닌 별도의 레이아웃 파일에 보관된다는 것입니다.Android : Button 호출시 NullPointerException이 발생합니까?

버튼 만 표시하고 자바 코드 내에서 버튼을 호출하지 않으면 정상적으로 표시됩니다. 오류는 setClickable() 및 과 같이 자바 코드에서 아무 것도 적용하려고하면 발생합니다.

아래 코드는 xml에서 버튼 및 레이아웃 생성을 간략히 보여주고 java 코드는 호출시 팝업을 만드는 메서드를 보여줍니다.

자바 코드 :

private void getLogoutOption() { 
     LayoutInflater layoutInflater = (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE); 
     View popupView = layoutInflater.inflate(R.layout.logoutoption, null); 
     final PopupWindow logoutoption = new PopupWindow(popupView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 

     Button yes = (Button) findViewById (R.id.yes); 
     yes.setClickable(true); 
     yes.setOnClickListener(new OnClickListener() { 
      public void onClick(View arg0) { 
       logoutoption.dismiss(); 
       switchActivity(8); 
      } 
     }); 

     Button no = (Button) findViewById (R.id.no); 
     no.setClickable(true); 
     no.setOnClickListener(new OnClickListener() { 
      public void onClick(View arg0) { 
       logoutoption.dismiss(); 
      } 
     }); 

    logoutoption.showAsDropDown(search, 50, -30); 
} 

XML 코드 :

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@android:color/transparent" >" 

    <RelativeLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="@drawable/menu_default" > 

     <TextView 
      android:id="@+id/logoutmessage" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentTop="true" 
      android:text="Are you sure you would like to logout?" 
      android:textColor="@color/gold" /> 

     <Button 
      android:id="@+id/yes" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@id/logoutmessage" 
      android:layout_centerInParent="true" 
      android:text="YES" 
      android:textColor="@color/green" /> 

     <Button 
      android:id="@+id/no" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@id/logoutmessage" 
      android:layout_toRightOf="@id/yes" 
      android:text="NO" 
      android:textColor="@color/red" /> 
    </RelativeLayout> 

</RelativeLayout> 
+0

보이는 것 Button yes = (Button) findViewById (R.id.yes); null가 돌려 주어집니다. onResume() 또는 onCreate()에서 setContentView (..)를 수행 했습니까? – kosa

답변

1

변경 :

Button yes = (Button) findViewById (R.id.yes); 
Button no = (Button) findViewById (R.id.no); 

사람 :

Button yes = (Button) popupView.findViewById (R.id.yes); 
Button no = (Button) popupView.findViewById (R.id.no); 
2
Button yes = (Button) findViewById (R.id.yes); 

기본적 findViewById()으로

Button yes = (Button) popupView.findViewById (R.id.yes); 

이 함께보기에 대해 콘텐츠보기를 검색하는 것이어야한다 지정된 ID (예 : setContentView(R.layout.main)을 사용하면 main.xml 구조를 통해 해당 뷰를 검색하게됩니다. 팽창 된 레이아웃을 검색하는 경우 해당 특정 크기의 레이아웃에서 findViewById()으로 전화해야합니다.

1

안녕 문제는 당신이 된 setContentView (R.layout.main)에 의해 생성 Activity.In 활동보기에 걸릴 당신이 ID를 받고있다. 귀하의 코드에서 당신은 사용해야 새로운보기를 부 풀려야합니다

버튼 예 = (버튼) popupView.findViewById (R.id.yes);