我的应用程序在大多数设备上成功运行。然而,今天它在一些设备中崩溃了。经询问,我得到以下例外:
java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1842)
at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1860)
at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:650)
at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:609)
at com.xyz.fieldquote.Activities.RegistrationActivity$2.onErrorResponse(RegistrationActivity.java:190)
at com.android.volley.Request.deliverError(Request.java:598)
at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:101)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
下面我发布了我的代码,我收到错误。这里如果我得到200响应,我正在调用一个活动,否则如果出现错误响应,我会调用一个片段。
public void tryLogIn( String str_uuid) {
if (isNetworkAvailable()) {
volleyQueue = CustomVolleyRequestQueue.getInstance(getApplicationContext())
.getRequestQueue();
JsonObjectRequest request = new JsonObjectRequest(apiClass.QUOTE_LOG_IN + str_uuid, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d("recommendationresponse",response+"");
String str_fsrID = "", str_userName = "", str_fsrName;
int int_notificationNumber;
boolean bool_whitelisted;
try {
JSONObject data = response.getJSONObject("data");
int int_response_code = response.getInt("OK");
Log.d("response",int_response_code+"");
if(int_response_code==200)
{
bool_whitelisted = data.getBoolean("IsWhitelisted");
Log.d("response",bool_whitelisted+"");
if(bool_whitelisted == true)
{
str_fsrID = data.getString("VendorId");
str_userName = data.getString("UserName");
str_fsrName = data.getString("FsrName");
JSONArray notificationArray = data.optJSONArray("MessageNotification");
if(notificationArray==null)
{
Log.d("arrayresponse","null");
int_notificationNumber = -1;
}
else
{
Log.d("arrayresponse",notificationArray.length()+"");
int_notificationNumber = notificationArray.length();
for(int i = 0;i< notificationArray.length();i++)
{
alst_notificationList.add((String) notificationArray.get(i));
}
}
Log.d("login", response + "" + str_fsrID);
shrp_sharedpreferences = getSharedPreferences("pdfPreferences", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = shrp_sharedpreferences.edit();
editor.putString("fsrID", str_fsrID);
editor.putString("username", str_userName);
editor.putString("fsrname", str_fsrName);
editor.putInt("notificationNumber", int_notificationNumber);
editor.commit();
startActivity(new Intent(RegistrationActivity.this, MainActivity.class));
RegistrationActivity.this.finish();
}
else
{
}
}
else
{
if (!isFinishing()) {
blankFragment = new RegistrationFragment();
android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().add(R.id.registrationContainer, blankFragment).commit();
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
if (!isFinishing()) {
blankFragment = new RegistrationFragment();
android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().add(R.id.registrationContainer, blankFragment).commit();
}
}
活动的OnCreate():
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_registration);
alst_notificationList = new ArrayList<String>();
shrp_sharedpreferences = getSharedPreferences(pdfPreferences, Context.MODE_PRIVATE);
if(isTablet(this))
{
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
else
{
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
apiClass = new webAPIs();
str_uuid = Settings.Secure.getString(getContentResolver(), Settings.Secure.ANDROID_ID);
Log.d("uuid",str_uuid);
tryLogIn(str_uuid);
}
错误发生在onErrorResponse()fragmenttransaction行。
我甚至做了一些更改,比如调用commitAllowingStateLoss(),这给了我一个Activity is Destroyed exception。对于整个场景,我实际上非常无能为力。请帮忙。提前谢谢。