android.view.WindowManager$BadTokenException: Unable to add window

來源:互聯網
上載者:User
android.view.WindowManager$BadTokenException: Unable to add window -- token android.app.LocalActivityManager$LocalActivityRecord@45f48c40 is not valid; is your activity running?

今天在寫android 的google 地圖時遇到一個問題,我在地圖上添加一個Overlay 然後想在點擊這個overlay 表徵圖的時候彈對話方塊Dialog,
按這樣的思路寫完後,測試報出了上面的異常。
代碼如下:
//這是一個繼承了ItemizedOverlay 的類中的方法。
@Override
public boolean onTap(int i) {
// TODO Auto-generated method stub

OverlayItem item = allMapOverlays.get(i);
AlertDialog.Builder dialog = new AlertDialog.Builder(context);
dialog.setTitle(item.getTitle());
dialog.setMessage(item.getSnippet());
dialog.show();

return true;
}

查資料說問題在於 context. 說不能使用 getApplicationContext(),this 所得到的Context 必須用your_Activity.this 所擷取的Context.
而我就是用最後一種的,還是出差。
於是我想下我的實際的運行環境,我的地圖是在tabActivity 中的,那麼得到當前的表單管理者應該是上一級TabActivity 所有,而非我的地圖Activity
於是我 用 getParent()所得到的Context,運行成功。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.