handleWindowVisibility: no activity for token android.os.BinderProxy

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

这就是我使用Volley调用AsyncTask的方式:
错误就像,当我调用时会来。
我知道这个错误是因为活动被杀死,而AsyncTask after Volley响应想要使用被杀死的上下文,但我不知道如何修复它。no activity for token android.os.BinderProxystartActivity(intent);

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

利用.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);

我的客户应用程序.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;
    }
}

我的基地活动.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);
    }
}

答案 1

在某些情况下,我发现将 onCreatepersistentState 参数一起使用可能会导致此问题:

    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.classMyBaseActivitycurrentActivityonCreatenull


推荐