java - handleWindowVisibility : no activity for token android. os.BinderProxy

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

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

这就是我使用 Volley 调用 AsyncTask 的方式:
错误类似于 no activity for token android.os.BinderProxy 并且当我调用 startActivity(intent); 时它会出现。
我知道这个错误是因为 Activity 被终止并且 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) {

          }
});

我有如下接口(interface):
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);
    }
}

最佳答案

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

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

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

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

关于java - handleWindowVisibility : no activity for token android. os.BinderProxy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57848280/

相关文章:

android - 将 Activity 的实例传递给另一个对象安全吗?

Android Studio 未处理的异常警告

java - 如果 URL 参数值包含未解码的 '&' 字符

java - 通过相机捕获显示图像时 ImageView 错误

java - 更改形状 XML Android 的颜色

android - 无法退出 Android 应用程序

java - 使用java和UTF-16LE到UTF-8转换打开xls文件并将其保存为tsv文件

android - OpenCv4Android : frame masking by an image

Android Studio 3.1.2 gradle 构建错误

android-studio - Android Studio - 用于发布崩溃的签名 APK