错误java.lang.IllegalStateException:活动已被销毁(Error java.lang.IllegalStateException: Activity has been destroyed)
下面我一直在尝试与Facebook SDK合作,当我遇到这个问题时,每当运行下面的代码时,我似乎得到错误“java.lang.IllegalStateException:Activity has been destroyed”,我认为它有一些东西处理碎片,有什么想法?
11-02 15:24:29.212: E/AndroidRuntime(17034): FATAL EXCEPTION: main 11-02 15:24:29.212: E/AndroidRuntime(17034): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.facebooktest/com.example.facebooktest.FacebookTutorial}: java.lang.IllegalStateException: Activity has been destroyed
以上是错误,下面是代码。
package com.example.facebooktest; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.view.Menu; import android.view.ViewGroup.LayoutParams; import android.widget.LinearLayout; import android.widget.TextView; import com.facebook.android.AsyncFacebookRunner; import com.facebook.android.Facebook; public class FacebookTutorial extends FragmentActivity { // --------- Facebook References ---------// private Fragment mainFragment; // application id from facebook.com/developers public static final String APP_ID = "434865303240706"; // log tag for any log.x statements public static final String TAG = "FACEBOOK CONNECT"; // permissions array private static final String[] PERMS = new String[] { "user_events" }; // facebook vars private Facebook mFacebook; private AsyncFacebookRunner mAsyncRunner; // id text view private TextView mText; public static final int LOGIN = Menu.FIRST; public static final int GET_EVENTS = Menu.FIRST + 1; public static final int GET_ID = Menu.FIRST + 2; protected void initLayout() { LinearLayout rootView = new LinearLayout(this.getApplicationContext()); rootView.setOrientation(LinearLayout.VERTICAL); this.mText = new TextView(this.getApplicationContext()); this.mText.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); rootView.addView(this.mText); this.setContentView(rootView); } // --------- Method called on create. ---------// public void onCreate(Bundle savedInstanceState) { if (savedInstanceState == null) { // Add the fragment on initial activity setup mainFragment = new Fragment(); getSupportFragmentManager().beginTransaction() .add(android.R.id.content, mainFragment).commit(); } else { // Or set the fragment from restored state info mainFragment = (Fragment) getSupportFragmentManager() .findFragmentById(android.R.id.content); } } }
Below I have been trying to work with the Facebook SDK while i came upon this problem, every time that a run the below code i seem to get the error "java.lang.IllegalStateException: Activity has been destroyed", I think it has something to do with the fragments, any thoughts?
11-02 15:24:29.212: E/AndroidRuntime(17034): FATAL EXCEPTION: main 11-02 15:24:29.212: E/AndroidRuntime(17034): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.facebooktest/com.example.facebooktest.FacebookTutorial}: java.lang.IllegalStateException: Activity has been destroyed
Above is the error, and below is the code.
package com.example.facebooktest; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.view.Menu; import android.view.ViewGroup.LayoutParams; import android.widget.LinearLayout; import android.widget.TextView; import com.facebook.android.AsyncFacebookRunner; import com.facebook.android.Facebook; public class FacebookTutorial extends FragmentActivity { // --------- Facebook References ---------// private Fragment mainFragment; // application id from facebook.com/developers public static final String APP_ID = "434865303240706"; // log tag for any log.x statements public static final String TAG = "FACEBOOK CONNECT"; // permissions array private static final String[] PERMS = new String[] { "user_events" }; // facebook vars private Facebook mFacebook; private AsyncFacebookRunner mAsyncRunner; // id text view private TextView mText; public static final int LOGIN = Menu.FIRST; public static final int GET_EVENTS = Menu.FIRST + 1; public static final int GET_ID = Menu.FIRST + 2; protected void initLayout() { LinearLayout rootView = new LinearLayout(this.getApplicationContext()); rootView.setOrientation(LinearLayout.VERTICAL); this.mText = new TextView(this.getApplicationContext()); this.mText.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); rootView.addView(this.mText); this.setContentView(rootView); } // --------- Method called on create. ---------// public void onCreate(Bundle savedInstanceState) { if (savedInstanceState == null) { // Add the fragment on initial activity setup mainFragment = new Fragment(); getSupportFragmentManager().beginTransaction() .add(android.R.id.content, mainFragment).commit(); } else { // Or set the fragment from restored state info mainFragment = (Fragment) getSupportFragmentManager() .findFragmentById(android.R.id.content); } } }
原文:https://stackoverflow.com/questions/13198702
最满意答案
你应该像这样在你的自定义请求中扩展JsonArrayRequest:
public class MyJsonArrayRequest extends JsonArrayRequest { private Map<String, String> mPostParams; @Override protected Map<String, String> getParams() throws AuthFailureError { return mPostParams; } public MyJsonArrayRequest(String url, Map<String, String> postParams, Response.Listener<JSONArray> listener, Response.ErrorListener errorListener) { super(url, listener, errorListener); this.mPostParams = postParams; } }
使用
getParams()
方法,您将能够传递POST参数。Map<String, String> params = new HashMap<>(); params.put("ID", id); params.put("USER_ID", userId); MyJsonArrayRequest jsonArrayRequest = new MyJsonArrayRequest(GET_JSON_DATA_HTTP_URL, params, new Response.Listener<JSONArray>() { @Override public void onResponse(JSONArray response) { progressBar.setVisibility(View.GONE); JSON_PARSE_DATA_AFTER_WEBCALL(response); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { } });
另一种方法是通过URL中的GET请求传递参数。
You should extends JsonArrayRequest in your custom Request like this:
public class MyJsonArrayRequest extends JsonArrayRequest { private Map<String, String> mPostParams; @Override protected Map<String, String> getParams() throws AuthFailureError { return mPostParams; } public MyJsonArrayRequest(String url, Map<String, String> postParams, Response.Listener<JSONArray> listener, Response.ErrorListener errorListener) { super(url, listener, errorListener); this.mPostParams = postParams; } }
with
getParams()
method you will able to pass POST parameters.Map<String, String> params = new HashMap<>(); params.put("ID", id); params.put("USER_ID", userId); MyJsonArrayRequest jsonArrayRequest = new MyJsonArrayRequest(GET_JSON_DATA_HTTP_URL, params, new Response.Listener<JSONArray>() { @Override public void onResponse(JSONArray response) { progressBar.setVisibility(View.GONE); JSON_PARSE_DATA_AFTER_WEBCALL(response); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { } });
Another way is to pass parameters via GET request in URL.
相关问答
更多-
从JsonArrayRequest发送邮件到服务器不工作(send post from JsonArrayRequest volley to server not working)[2023-03-11]
用这个: public class BaseRequest extends Request{ private static final String TAG = BaseRequest.class.getSimpleName(); private final Gson gson = new Gson(); private final Response.Listener listener; public BaseRequest(String url ... -
对于JSON解析,你应该有头指定内容为json。 因为错误暗示你只是在回显html中的json数据。 当收到的数据在html文件中时,你会得到这样的结果。 尝试在标题中将内容类型设置为json put header('Content-Type: application/json'); 在回显之前在你的php文件中 For JSON parsing u should have header specifying the content to be json. As the error suggests you ...
-
你应该像这样在你的自定义请求中扩展JsonArrayRequest: public class MyJsonArrayRequest extends JsonArrayRequest { private Map
mPostParams; @Override protected Map getParams() throws AuthFailureError { return mPostParams; ... -
你在响应中得到一个jsonObject,但是你使用JsonArrayRequest在代码中处理JsonArray,所以你必须得到如下的JsonObject Request JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.GET,url,null,new Response.Listener(){ @Override public void onResponse(JSONObject response) { mT ...
-
可能是空格的问题 - 尝试使用%20而不是空格 String url2 = "http://www.xxxxxx.com/ParserUpdateUserAction.do?test=[{\"Mail\":\"xxxxxx@hotmail.fr\",\"Nationality\":\"Spain\",\"City\":\"nimes\",\"Quote\":\"bla%20bla%20bla\"}]"; 编辑: 实际上GET参数有一个字符限制,但它大约是512,所以这不应该是一个问题 - 但你应该明确考虑 ...
-
你尝试过使用StringRequest吗? 出于某种原因,当我使用JsonArrayRequest我没有从服务器得到任何回复。 如果您决定创建StringRequest ,则可以将响应转换为JSONArray如下所示: StringRequest get = new StringRequest(Method.POST, URL, new Listener
() { @Override public void onResponse(String ... -
我创建了一个定制的抽象请求,接受一个JSONObject作为参数。 CustomJsonArrayRequest.java public class CustomJsonArrayRequest extends JsonRequest
{ /** * Creates a new request. * @param method the HTTP method to use * @param url URL to ... -
好吧,伙计们,抱歉所有的麻烦......一切都发生了,因为我没有把“http://”放在网址中,现在正在运作 Okay guys, sorry for all the troubles... everything happened because I did not put "http://" in the url, it's working now
-
php中正常的$_POST方法在排球中不起作用。 你需要在你的php文件中做这个。 $post = json_decode(file_get_contents("php://input"), true); $my_value = $post['param1']; param1是你放入截击的值。 用这个 : final Hashmap
param1 = new Hashmap (); param1.put("param1",your value); ... -
Android-Volley:为JsonArrayRequest设置HTTP标头(Android-Volley : set HTTP Header for JsonArrayRequest)[2023-08-30]
您可以在请求结束时调用这些方法并进行调用; JsonArrayRequest localJReq = new JsonArrayRequest(localURL, new Response.Listener() { @Override public void onResponse(JSONArray response) { } }, ...