I cannot figure out why but I keep getting E/AndroidRuntime(709): java.lang.NullPointerException at any point I try to call the buttons titled yes and no. The buttons are created in an xml layout and called using the findViewById method, a similar technique I’ve used in several properly working buttons in the same program. The only difference between these buttons and the rest is that their xml code is kept in a separate layout file that is not the main.xml.
If I only want to show the buttons and don’t call them within the java code they will show up just as they should. The error occurs when I try to apply anything to them in the java code, such as setClickable() and setOnClickListener().
The code below outlines the creation of the button and layout in the xml and the java code shows a method that creates a popup when called.
Java Code:
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 CODE:
<?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>
Change:
To: