Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

解決 android.os.BinderProxy cannot be cast to ActivityRecord$Token 錯誤

編輯:關於 Android 程式設計

在 Android 開發中使用百度地圖時,試圖透過百度地圖取得目前的經緯度,控制台卻報錯:

Bad activity token: android.os.BinderProxy
java.lang.ClassCastException: android.os.BinderProxy cannot be cast to com.android.server.am.ActivityRecord$Token

問題原因

原因是初始化 LocationClient 時,使用了:

locationClient = new LocationClient(this);

如果當前的 activity 並不是由 Main(主 Activity)啟動的,就會出問題。

解決方法

把上面的程式碼改成傳入 getApplicationContext():

locationClient = new LocationClient(getApplicationContext());

這樣就能解決該錯誤了。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved