我正在做一個應用程序,我應該使用該服務在特定的時間間隔打開PopUpwindow。我試圖通過將彈出窗口代碼保存在其他活動中作爲方法並在服務中調用它。但結果徒然。所以我已經將所有的popupWindow代碼複製到了這裏的服務中,我無法使用findViewById。無法在服務中使用findViewbyId
我的服務代碼如下
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();
}
}
});
}
}
}
我收到錯誤行
layout = inflater.inflate(R.layout.layout_popup, (ViewGroup) findViewById(R.id.popup_1));
和錯誤是無法化解的方法findViewbyId(INT)。
請幫我編寫代碼。我做錯了什麼。
在此先感謝。
我已經使用的代碼
layout = inflater.inflate(R.layout.layout_popup, null);
我收到錯誤或堆棧跟蹤如下的第二個參數
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)
閱讀本 - http://stackoverflow.com/questions/6992748/android-findviewbyid-in-service –
@ mayurvpatel110我已閱讀並嘗試過,但它的力量達到了我的要求,我已經這樣做了。當我使參數爲空時,U可以看到系統錯誤。 –
@KarrthikReddy你解決了這個問題 – SaravInfern