I am doing an application where I should use the Service to open a PopUpwindow at certain Intervals of time. I tried it by keeping the popup window code in other activity as method and calling it in the service. But it ended up in vain. So I have copied all popupWindow code to the service here I cannot use findViewById.
My service code is as follows
public class TimeService extends Service {
View layout;
LayoutInflater inflater;
public PopupWindow pw;
private RadioGroup options_group;
private RadioButton option_button;
Button submit_popup,close_popup;
// constant
public static final long NOTIFY_INTERVAL = 10 * 10000; // 100 seconds
// run on another Thread to avoid crash
private Handler mHandler = new Handler();
// timer handling
private Timer mTimer = null;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
// cancel if already existed
if (mTimer != null) {
mTimer.cancel();
} else {
// recreate new
mTimer = new Timer();
}
// schedule task
mTimer.scheduleAtFixedRate(new TimeDisplayTimerTask(), 0, NOTIFY_INTERVAL);
}
class TimeDisplayTimerTask extends TimerTask {
@Override
public void run() {
// run on another thread
mHandler.post(new Runnable() {
@Override
public void run() {
// display toast
try {
inflater = (LayoutInflater) TimeService.this.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layout = inflater.inflate(R.layout.layout_popup, (ViewGroup) findViewById(R.id.popup_1));
pw = new PopupWindow(layout, 300, 370, true);
pw.showAtLocation(layout, Gravity.CENTER, 0, 0);
submit_popup = (Button) layout.findViewById(R.id.popup_submit);
close_popup = (Button) layout.findViewById(R.id.popup_cancel);
options_group=(RadioGroup) layout.findViewById(R.id.radioGroup);
submit_popup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
pw.dismiss();
}
});
submit_popup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int selectedId=options_group.getCheckedRadioButtonId();
option_button=(RadioButton) layout.findViewById(selectedId);
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
}
I am getting error in the line
layout = inflater.inflate(R.layout.layout_popup, (ViewGroup) findViewById(R.id.popup_1));
and the error is cannot resolve method findViewbyId(int).
Please help me with the code. What I am doing wrong.
Thanks in advance.
I have tried using the code
layout = inflater.inflate(R.layout.layout_popup, null);
I am getting the error or stacktrace as follows
07-22 01:17:39.789 940-957/system_process W/WindowManager: Attempted to add window with token that is not a window: null. Aborting.
07-22 01:17:39.790 27060-27060/highski.developers.cflash W/System.err: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
07-22 01:17:39.790 27060-27060/highski.developers.cflash W/System.err: at android.view.ViewRootImpl.setView(ViewRootImpl.java:562)
07-22 01:17:39.790 27060-27060/highski.developers.cflash W/System.err: at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:282)
07-22 01:17:39.790 27060-27060/highski.developers.cflash W/System.err: at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:85)
07-22 01:17:39.790 27060-27060/highski.developers.cflash W/System.err: at android.widget.PopupWindow.invokePopup(PopupWindow.java:1104)
07-22 01:17:39.790 27060-27060/highski.developers.cflash W/System.err: at android.widget.PopupWindow.showAtLocation(PopupWindow.java:933)
07-22 01:17:39.790 27060-27060/highski.developers.cflash W/System.err: at android.widget.PopupWindow.showAtLocation(PopupWindow.java:897)
07-22 01:17:39.791 27060-27060/highski.developers.cflash W/System.err: at highski.developers.cflash.service.TimeService$TimeDisplayTimerTask$1$override.run(TimeService.java:85)
07-22 01:17:39.791 27060-27060/highski.developers.cflash W/System.err: at highski.developers.cflash.service.TimeService$TimeDisplayTimerTask$1$override.access$dispatch(TimeService.java)
07-22 01:17:39.791 27060-27060/highski.developers.cflash W/System.err: at highski.developers.cflash.service.TimeService$TimeDisplayTimerTask$1.run(TimeService.java:0)
07-22 01:17:39.791 27060-27060/highski.developers.cflash W/System.err: at android.os.Handler.handleCallback(Handler.java:739)
07-22 01:17:39.791 27060-27060/highski.developers.cflash W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
07-22 01:17:39.791 27060-27060/highski.developers.cflash W/System.err: at android.os.Looper.loop(Looper.java:135)
07-22 01:17:39.791 27060-27060/highski.developers.cflash W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5254)
07-22 01:17:39.791 27060-27060/highski.developers.cflash W/System.err: at java.lang.reflect.Method.invoke(Native Method)
07-22 01:17:39.791 27060-27060/highski.developers.cflash W/System.err: at java.lang.reflect.Method.invoke(Method.java:372)
07-22 01:17:39.791 27060-27060/highski.developers.cflash W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
07-22 01:17:39.791 27060-27060/highski.developers.cflash W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)