Android Studio WindowVisibility:no activity for token android.os.BinderProxy

2uluyalo  于 11个月前  发布在  Android
关注(0)|答案(6)|浏览(155)

我有一个登录屏幕,成功登录后,它完成并显示另一个页面,有关于用户的信息。
我读了this postthis 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) {

          }
});

字符串
我有如下接口:

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

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

wgxvkvu9

wgxvkvu91#

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

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

字符串
onCreate 更改为仅使用 savedInstanceState 参数修复了该问题:

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

3j86kqsm

3j86kqsm2#

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

jk9hmnmh

jk9hmnmh3#

我不知道为什么和如何,但我所做的是我只是建立>重建项目,然后在它重建后,我只是文件>无效缓存/重新启动
然后我的活动完美地工作了...!

ybzsozfc

ybzsozfc4#

在我的例子中,当我试图通过Intent extra传递一个非常长的JSON字符串时,就会发生这种情况。从我可以推断的是,通过额外的bundle传递一个非常大的值可能会在启动新Intent时导致内存问题。尝试使用共享首选项或可序列化对象在Activity之间共享大的内容
希望有帮助!

iaqfqrcu

iaqfqrcu5#

我在我的登录活动中得到了这个错误,因为我忘记了打开模拟器互联网!我打开它,它解决了。
我的意思是这个错误:没有活动令牌android.os.BinderProxy

vhmi4jdf

vhmi4jdf6#

对于我的flutter应用程序,firebase需要SHA-1指纹.我以前添加了它,但不知何故,它已经改变了,我只是添加了新的SHA-1指纹我的应用程序到firebase android应用程序,然后它的工作.希望它能帮助.

相关问题