【问题标题】:handleWindowVisibility: no activity for token android.os.BinderProxyhandleWindowVisibility:令牌 android.os.BinderProxy 没有活动
【发布时间】:2020-01-10 21:10:36
【问题描述】:

我有一个登录屏幕,成功登录后,它完成并显示另一个页面,其中包含有关用户的信息。
我读到了this postthis post
我还阅读了很多关于我们如何扩展 Application 类的信息,但我仍然无法运行此代码。
您可以在下面找到我的代码,我也会解释错误。

这就是我使用 Volley 调用 AsyncTask 的方式:
错误类似于no activity for token android.os.BinderProxy,当我调用startActivity(intent); 时会出现。
我知道这个错误是因为活动被杀死,并且 Volley 响应后的 AsyncTask 想要使用被杀死的上下文,但我不知道如何修复它。

Util.request_function(
     activity,
     MainActivity.user_session,
     key_value,
     new VolleyCallback() {
          @Override
          public void onSuccess(JSONObject result, Context context) {

                 Activity activity = 
                 MyBaseActivity.myCustomApplication.getCurrentActivity();
                 Intent intent = new Intent(activity, SelfieCapture.class);
                 startActivity(intent);
                 finish();
          }
          @Override
          public void onError(String result) {

          }
});

我的接口如下:
VolleyCallback.java:

public interface VolleyCallback {
    void onSuccess(JSONObject result) throws JSONException;
    void onError(String result) throws Exception;
}

Util.java

public static void request_function(Context context, CognitoUserSession cognitoUserSession, Map<String, String> key_value, final VolleyCallback callback) {
        JSONObject jsonBody = new JSONObject();
        CustomJSONObjectRequest postRequest = new CustomJSONObjectRequest(Request.Method.POST,
                MainActivity.API_URL,
                null,
                response -> {
                   JSONObject jsonObject = (JSONObject) response;
                   //SoMe Stuff//
                   callback.onSuccess(null);
             }, error -> {
                   //Log Error//
             }){
                  @Override
                  public String getBodyContentType() {
                         return "application/json; charset=utf-8";
                  }

                  @Override
                  public Map<String, String> getHeaders() {
                  final Map<String, String> headers = new HashMap<>();
                  headers.put("Content-Type", "application/json");
                         return headers;
                  } 

                  @Override
                  public byte[] getBody() {
                         return jsonBody.toString().getBytes();
                  }
        };
     // Request added to the RequestQueue
     VolleyController.getInstance(context).addToRequestQueue(postRequest);

MyCustomApplication.java

public class MyCustomApplication extends Application {

    private Activity mCurrentActivity = null;

    public void onCreate() {
        super.onCreate();

    }

    public Activity getCurrentActivity() {
        return mCurrentActivity;
    }

    public void setCurrentActivity(Activity mCurrentActivity) {
        this.mCurrentActivity = mCurrentActivity;
    }
}

MyBaseActivity.java

public class MyBaseActivity extends Activity {
    public static MyCustomApplication myCustomApplication;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        myCustomApplication = (MyCustomApplication)this.getApplicationContext();
    }
    protected void onResume() {
        super.onResume();
        myCustomApplication.setCurrentActivity(this);
    }
    protected void onPause() {
        clearReferences();
        super.onPause();
    }
    protected void onDestroy() {
        clearReferences();
        super.onDestroy();
    }

    private void clearReferences(){
        Activity currActivity = myCustomApplication.getCurrentActivity();
        if (this.equals(currActivity))
            myCustomApplication.setCurrentActivity(null);
    }
}

【问题讨论】:

    标签: java android android-studio android-asynctask android-volley


    【解决方案1】:

    在某些情况下,我发现使用带有 persistentState 参数的 onCreate 可能会导致问题:

        override fun onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) {
            super.onCreate(savedInstanceState, persistentState)
        }
    

    onCreate 更改为仅使用 savedInstanceState 参数可解决问题:

        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
        }
    

    【讨论】:

      【解决方案2】:

      这段代码似乎是正确的,但我唯一的怀疑是当你想在startActivity(intent) 中打开新活动时,会发生错误。
      所以检查下一个名为 SelfieCapture.class 的触发类,看看它是否也从 MyBaseActivity 扩展。
      还要考虑当你想得到currentActivity时,如果你把它放在onCreate中,你会得到null。更多信息请参考Understand the Activity Lifecycle

      【讨论】:

        【解决方案3】:

        就我而言,这发生在我试图通过 Intent Extras 传递一个非常长的 JSON 字符串时。 据我推断,通过额外的捆绑包传递一个非常大的值可能会在启动新意图时导致内存问题。 尝试使用共享首选项或可序列化对象在活动之间共享大型内容

        希望有所帮助!

        【讨论】:

          【解决方案4】:

          我不知道为什么和如何,但我所做的只是 build > Rebuilt Project 然后在重建后,我只是 File > Invalidate Caches / Restart

          然后我的活动运行良好......!

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2019-10-31
            • 1970-01-01
            • 2022-09-26
            • 2020-04-27
            • 1970-01-01
            • 1970-01-01
            • 2014-10-22
            • 1970-01-01
            相关资源
            最近更新 更多