Reputation: 39
So I have the following code for a popup window in an activity:
private void initiatePopupWindow(String popupText, boolean hasEditText){
LayoutInflater inflater = (LayoutInflater) PlayActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View layout= inflater.inflate(R.layout.popup_layout, (ViewGroup) findViewById(R.id.popup_layout));
final PopupWindow popupWindow= new PopupWindow(layout, 300, 370, true);
findViewById(R.id.play_activity_layout).post(new Runnable() {
@Override
public void run() {
popupWindow.showAtLocation(layout, Gravity.CENTER, 0, 0);
}
});
TextView textPopup= (TextView) layout.findViewById(R.id.tvPopupText);
textPopup.setText(popupText);
if(hasEditText) {
EditText et = (EditText) findViewById(R.id.etNumber);
MAX_NUMBER_OF_MOVES = Integer.parseInt(et.getText().toString());
}
btnClosePopup= (Button) layout.findViewById(R.id.btnClosePopup);
btnClosePopup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
popupWindow.dismiss();
//finish();
}
});
}
I get the NullPointerException for this row:
MAX_NUMBER_OF_MOVES = Integer.parseInt(et.getText().toString());
I tried to fix this by searching on the site for ideas but still dont know why it isnt proper.
EDIT:
My popup_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/popup_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#444444"
android:orientation="vertical"
android:padding="10sp" >
<TextView
android:id="@+id/tvPopupText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5sp"
android:text="@string/under_construction"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"/>
<Button
android:id="@+id/btnClosePopup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/ok"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/etNumber"
android:inputType="number"
android:layout_alignRight="@+id/tvPopupText"/>
</RelativeLayout>
Upvotes: 0
Views: 79
Reputation: 136
After you've fix the layout.findViewById....
Initialize the EditText to some default value...see the android:text="0" below
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/etNumber"
android:inputType="number"
android:layout_alignRight="@+id/tvPopupText"
android:text="0"
/>
And you should also ALWAYS wrap Integer.parseInt("string_value") in a try/catch block in case string_value is not a valid number:
try {
MAX_NUMBER_OF_MOVES = Integer.parseInt(et.getText().toString());
}
catch (NumberFormatException nfe_) {
MAX_NUMBER_OF_MOVES = 5; // Or whatever your default number should be
}
Upvotes: 0
Reputation: 2057
Try to change this:
final View layout= inflater.inflate(R.layout.popup_layout, (ViewGroup) findViewById(R.id.popup_layout));
for this other one:
// container = get it from param of onCreateView
final View layout= inflater.inflate(R.layout.popup_layout, container);
...
EditText et = (EditText) layout.findViewById(R.id.etNumber);
Upvotes: 1
Reputation: 25050
I think you have to use
EditText et = (EditText) layout.findViewById(R.id.etNumber);
see layout.
missing.
Note that getText()
never returns null. So only findViewById
should raise the NullPointerException.
Upvotes: 2