2016-07-22 60 views
0

我正在做一個應用程序,我應該使用該服務在特定的時間間隔打開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) 
+0

閱讀本 - http://stackoverflow.com/questions/6992748/android-findviewbyid-in-service –

+0

@ mayurvpatel110我已閱讀並嘗試過,但它的力量達到了我的要求,我已經這樣做了。當我使參數爲空時,U可以看到系統錯誤。 –

+0

@KarrthikReddy你解決了這個問題 – SaravInfern

回答

1

傳遞null值試圖

layout = inflater.inflate(R.layout.layout_popup, (ViewGroup) findViewById(R.id.popup_1)); 

layout = inflater.inflate(R.layout.layout_popup, null); 

更新:

private Handler mHandler; 

@Override 
public void onCreate() { 
    mHandler = new Handler(Looper.getMainLooper()); 
    //... do like above and remove perivous updated 
} 
+0

我做了一個編輯問題,請看看它。 –

+0

@KarrthikReddy檢查更新。 –

+0

這並沒有幫助我得到了另一個系統錯誤java.lang.RuntimeException:無法創建處理程序內部線程沒有調用Looper.prepare() –

1

你不能直接進行,而不是使用廣播接收器和意圖過濾從那裏

通知您活動/片段和更新服務中的用戶界面的變化如何將服務工作 see how service works

一些使用參考onservices

http://www.vogella.com/tutorials/AndroidServices/article.html

https://androidexperinz.wordpress.com/2012/02/14/communication-between-service-and-activity-part-1/

+0

謝謝@SaravInfern。我正在看看它。當我完全看到時會回來。 –

+0

如果您試圖顯示警報對話框,您也可以在這裏參考http://stackoverflow.com/a/21929839/6414107 – SaravInfern

0

您需要使用處理器

private Handler handler = new Handler(new Handler.Callback() { 
    @Override 
    public boolean handleMessage(Message msg) { 
     if (msg.what == 0) { 
      // add your code here. 
     } 
     return false; 
    } 
}); 

添加通話中UA的run()方法的處理程序。

handler.sendEmptyMessage(0); 

一切順利。

+0

您的答案與所有​​問題要求不符 –

0

AlertDialog應該由活動管理其中有一個窗口,因爲服務是一個非UI組件,所以你不能顯示在服務對話框,否則應用程序會崩潰:

產生的原因:android.view.WindowManager $ BadTokenException:無法添加窗口 - 令牌null不適用於應用程序

在android.app.Dialog.show(Dialog.java:325)

有兩種方式在服務上顯示對話框:

1,啓動對話的主題活動,並顯示一個對話框在此活動的onCreate方法中。

android:theme="@android:style/Theme.Holo.Light.Dialog" 

OR

2,設置對話框,系統警報:

AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext()); 
    builder.setTitle("title") 
      .setMessage("message"); 
    AlertDialog dialog = builder.create(); 
    // set the dialog as system alert 
    dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT); 
    dialog.show();