无法在连接更改时将ReceiverRestrictedContext强制转换为android.app.Activity

时间:2019-02-26 14:31:16

标签: android

我目前正在尝试在建立网络连接后将一些信息上载到服务器。 但是一旦到达这一点,应用程序的流程就中断了,并停止了上传过程,并且出现了标题上显示的异常。

会发生什么:启动线程,上传文件,然后继续执行而不会中断流程。

正在发生的事情:该过程尝试启动,中断并且不上传任何文件。

public static void responseJson(JSONObject response) throws JSONException {
    try {
        JSONObject jsonResponse = response.getJSONObject("quote");
        int idServer = jsonResponse.getInt("id");

        dbAgenda.updateAgenda(idServer, id_local);

        ((Activity)context).runOnUiThread(new Runnable() {
            @Override
            public void run() {
                submitAgenda();
            }
        });

    } catch (Exception e) {
        e.printStackTrace();
    }
}

这是我得到的错误:

02-26 09:20:38.588 13250-13959/www.newapp.com.co E/AsyncHttpResponseHandler: User-space exception detected!
java.lang.ClassCastException: android.app.ReceiverRestrictedContext cannot be cast to android.app.Activity
    at www.newapp.com.co.utils.SYNC.SyncCalendar.responseJson(SyncCalendar.java:131)
    at www.newapp.com.co.json.JSONServices.onSuccess(JSONServices.java:178)
    at www.newapp.com.co.services.HttpClient$1.onSuccess(HttpClient.java:218)
    at com.loopj.android.http.JsonHttpResponseHandler$1$1.run(JsonHttpResponseHandler.java:153)
    at com.loopj.android.http.AsyncHttpResponseHandler.postRunnable(AsyncHttpResponseHandler.java:426)
    at com.loopj.android.http.JsonHttpResponseHandler$1.run(JsonHttpResponseHandler.java:146)
    at com.loopj.android.http.JsonHttpResponseHandler.onSuccess(JsonHttpResponseHandler.java:182)
    at com.loopj.android.http.AsyncHttpResponseHandler.handleMessage(AsyncHttpResponseHandler.java:360)
    at com.loopj.android.http.AsyncHttpResponseHandler.sendMessage(AsyncHttpResponseHandler.java:410)
    at com.loopj.android.http.AsyncHttpResponseHandler.sendSuccessMessage(AsyncHttpResponseHandler.java:323)
    at com.loopj.android.http.AsyncHttpResponseHandler.sendResponseMessage(AsyncHttpResponseHandler.java:457)
    at com.loopj.android.http.AsyncHttpRequest.makeRequest(AsyncHttpRequest.java:162)
    at com.loopj.android.http.AsyncHttpRequest.makeRequestWithRetries(AsyncHttpRequest.java:179)
    at com.loopj.android.http.AsyncHttpRequest.run(AsyncHttpRequest.java:108)
    at com.loopj.android.http.SyncHttpClient.sendRequest(SyncHttpClient.java:95)
    at com.loopj.android.http.AsyncHttpClient.post(AsyncHttpClient.java:978)
    at www.newapp.com.co.services.HttpClient.httpRequestJson(HttpClient.java:133)
    at www.newapp.com.co.json.JSONServices.submitInsert(JSONServices.java:85)
    at www.newapp.com.co.json.JSONServices.access$000(JSONServices.java:30)
    at www.newapp.com.co.json.JSONServices$1.run(JSONServices.java:56)
    at java.lang.Thread.run(Thread.java:841)

02-26 09:20:38.648 13250-13959/www.newapp.com.co E/AsyncHttpRequest: Unhandled exception origin cause
java.lang.RuntimeException: java.lang.ClassCastException: android.app.ReceiverRestrictedContext cannot be cast to android.app.Activity
    at com.loopj.android.http.AsyncHttpResponseHandler.onUserException(AsyncHttpResponseHandler.java:313)
    at com.loopj.android.http.AsyncHttpResponseHandler.handleMessage(AsyncHttpResponseHandler.java:404)
    at com.loopj.android.http.AsyncHttpResponseHandler.sendMessage(AsyncHttpResponseHandler.java:410)
    at com.loopj.android.http.AsyncHttpResponseHandler.sendSuccessMessage(AsyncHttpResponseHandler.java:323)
    at com.loopj.android.http.AsyncHttpResponseHandler.sendResponseMessage(AsyncHttpResponseHandler.java:457)
    at com.loopj.android.http.AsyncHttpRequest.makeRequest(AsyncHttpRequest.java:162)
    at com.loopj.android.http.AsyncHttpRequest.makeRequestWithRetries(AsyncHttpRequest.java:179)
    at com.loopj.android.http.AsyncHttpRequest.run(AsyncHttpRequest.java:108)
    at com.loopj.android.http.SyncHttpClient.sendRequest(SyncHttpClient.java:95)
    at com.loopj.android.http.AsyncHttpClient.post(AsyncHttpClient.java:978)
    at www.newapp.com.co.services.HttpClient.httpRequestJson(HttpClient.java:133)
    at www.newapp.com.co.json.JSONServices.submitInsert(JSONServices.java:85)
    at www.newapp.com.co.json.JSONServices.access$000(JSONServices.java:30)
    at www.newapp.com.co.json.JSONServices$1.run(JSONServices.java:56)
    at java.lang.Thread.run(Thread.java:841)

 Caused by: java.lang.ClassCastException: android.app.ReceiverRestrictedContext cannot be cast to android.app.Activity
    at www.newapp.com.co.utils.SYNC.SyncCalendar.responseJson(SyncCalendar.java:131)
    at www.newapp.com.co.json.JSONServices.onSuccess(JSONServices.java:178)
    at www.newapp.com.co.services.HttpClient$1.onSuccess(HttpClient.java:218)
    at com.loopj.android.http.JsonHttpResponseHandler$1$1.run(JsonHttpResponseHandler.java:153)
    at com.loopj.android.http.AsyncHttpResponseHandler.postRunnable(AsyncHttpResponseHandler.java:426)
    at com.loopj.android.http.JsonHttpResponseHandler$1.run(JsonHttpResponseHandler.java:146)
    at com.loopj.android.http.JsonHttpResponseHandler.onSuccess(JsonHttpResponseHandler.java:182)
    at com.loopj.android.http.AsyncHttpResponseHandler.handleMessage(AsyncHttpResponseHandler.java:360)
    at com.loopj.android.http.AsyncHttpResponseHandler.sendMessage(AsyncHttpResponseHandler.java:410) 
    at com.loopj.android.http.AsyncHttpResponseHandler.sendSuccessMessage(AsyncHttpResponseHandler.java:323) 
    at com.loopj.android.http.AsyncHttpResponseHandler.sendResponseMessage(AsyncHttpResponseHandler.java:457) 
    at com.loopj.android.http.AsyncHttpRequest.makeRequest(AsyncHttpRequest.java:162) 
    at com.loopj.android.http.AsyncHttpRequest.makeRequestWithRetries(AsyncHttpRequest.java:179) 
    at com.loopj.android.http.AsyncHttpRequest.run(AsyncHttpRequest.java:108) 
    at com.loopj.android.http.SyncHttpClient.sendRequest(SyncHttpClient.java:95) 
    at com.loopj.android.http.AsyncHttpClient.post(AsyncHttpClient.java:978) 
    at www.newapp.com.co.services.HttpClient.httpRequestJson(HttpClient.java:133) 
    at www.newapp.com.co.json.JSONServices.submitInsert(JSONServices.java:85) 
    at www.newapp.com.co.json.JSONServices.access$000(JSONServices.java:30) 
    at www.newapp.com.co.json.JSONServices$1.run(JSONServices.java:56) 
    at java.lang.Thread.run(Thread.java:841) 

我相信最后一部分是罪魁祸首,但我不知道如何解决。

1 个答案:

答案 0 :(得分:0)

BroadcastReceiver吗?您无法在此行-> ReceiverRestrictedContext上将Activity投射到((Activity)context)。可能是this question here的副本。