handleWindowVisibility:令牌android.os.BinderProxy没有活动 [英] handleWindowVisibility: no activity for token android.os.BinderProxy

查看:183
本文介绍了handleWindowVisibility:令牌android.os.BinderProxy没有活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个登录屏幕,成功登录后,它完成并显示另一个包含有关用户信息的页面.
我阅读了有关这篇文章以及这篇文章.
我还阅读了很多有关如何扩展Application类的信息,但仍然无法运行此代码.
在下面,您可以找到我的代码,我还将解释错误.

I have a Login screen and upon success login, it finishes and shows another page that has information about the user.
I read about this post and also this post.
I also read a lot about how we extend Application class but still it is not possible for me to run this code.
Below you can find my code and I will explain the error also.

这是我使用Volley调用AsyncTask的方式:
该错误类似于令牌android.os.BinderProxy没有活动,并且在我调用 startActivity(intent); 时会出现.
我知道此错误是因为活动被杀死,Volley响应后的AsyncTask想使用被杀死的上下文,但我不知道如何解决它.

This is how I user Volley to call an AsyncTask:
The error is like no activity for token android.os.BinderProxy and it will come when I call startActivity(intent);.
I know this error is because the activity is killed and the AsyncTask after Volley response wants to use a killed context but I don't know how to fix it.

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);
    }
}

推荐答案

此代码似乎正确,但是我唯一的怀疑是,当您想在 startActivity(intent)中打开新活动时,该错误发生.
因此,请检查下一个触发的名为 SelfieCapture.class 的类,以查看它是否也从 MyBaseActivity 扩展.
还要考虑一下,当您想要获取 currentActivity 时,如果将其放在 onCreate 中,则会得到 null .有关更多信息,请参阅 了解活动生命周期 .

This code Seems correct but my only suspicion is that when you want to open the new activity in startActivity(intent), the error occurs.
So check the next fired class named SelfieCapture.class to see whether it extends from MyBaseActivity also.
Also consider that when you want to get the currentActivity, if you put it in onCreate, you will get null. For more information please refer to Understand the Activity Lifecycle.

这篇关于handleWindowVisibility:令牌android.os.BinderProxy没有活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆