왜 그런지 알 수는 없지만 언제든지 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>
보이는 것 Button yes = (Button) findViewById (R.id.yes); null가 돌려 주어집니다. onResume() 또는 onCreate()에서 setContentView (..)를 수행 했습니까? – kosa