I have trouble of tracing the cause of the crash in my app. I have a fragment that displays a ListView
, I also have SlidingMenu
for a search field. And when I search, the ListView
items are repopulated basing from the search results. Now, my app crashes when I try to open a ListView
item from the new populated result.
The exception is triggered here:
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
getSupportFragmentManager().putFragment(outState, "mContent", mContent); //Exception made here
}
Is this because of the support library? How do I resolve this?
EDIT:
Here's the stack trace:
02-05 12:51:53.941: E/AndroidRuntime(11638): FATAL EXCEPTION: main
02-05 12:51:53.941: E/AndroidRuntime(11638): java.lang.IllegalStateException: Fragment MyFragment{436749b0} is not currently in the FragmentManager
02-05 12:51:53.941: E/AndroidRuntime(11638): at android.support.v4.app.FragmentManagerImpl.putFragment(FragmentManager.java:546)
02-05 12:51:53.941: E/AndroidRuntime(11638): at com.sample.android.app.SearchListActivity.onSaveInstanceState(SearchListActivity.java:118)
02-05 12:51:53.941: E/AndroidRuntime(11638): at android.app.Activity.performSaveInstanceState(Activity.java:1137)
02-05 12:51:53.941: E/AndroidRuntime(11638): at android.app.Instrumentation.callActivityOnSaveInstanceState(Instrumentation.java:1215)
02-05 12:51:53.941: E/AndroidRuntime(11638): at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:2972)
02-05 12:51:53.941: E/AndroidRuntime(11638): at android.app.ActivityThread.handleStopActivity(ActivityThread.java:3031)
02-05 12:51:53.941: E/AndroidRuntime(11638): at android.app.ActivityThread.access$900(ActivityThread.java:138)
02-05 12:51:53.941: E/AndroidRuntime(11638): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
02-05 12:51:53.941: E/AndroidRuntime(11638): at android.os.Handler.dispatchMessage(Handler.java:99)
02-05 12:51:53.941: E/AndroidRuntime(11638): at android.os.Looper.loop(Looper.java:213)
02-05 12:51:53.941: E/AndroidRuntime(11638): at android.app.ActivityThread.main(ActivityThread.java:4787)
02-05 12:51:53.941: E/AndroidRuntime(11638): at java.lang.reflect.Method.invokeNative(Native Method)
02-05 12:51:53.941: E/AndroidRuntime(11638): at java.lang.reflect.Method.invoke(Method.java:511)
02-05 12:51:53.941: E/AndroidRuntime(11638): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
02-05 12:51:53.941: E/AndroidRuntime(11638): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
02-05 12:51:53.941: E/AndroidRuntime(11638): at dalvik.system.NativeStart.main(Native Method)
EDIT 2: Here's my Activity:
private Fragment mFragment;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
..........
params = new Bundle();
params.putInt("id", id);
// set the Above View Fragment
if (savedInstanceState != null)
mFragment = getSupportFragmentManager().getFragment(
savedInstanceState, "mFragment");
else {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
SampleListFragment fragment = new SampleListFragment();
fragment.setArguments(params);
fragmentTransaction.add(R.id.list_fragment, fragment);
fragmentTransaction.commit();
}
if (mFragment == null) {
mFragment = new SampleListFragment();
mFragment.setArguments(params);
}
getSupportFragmentManager().beginTransaction()
.replace(R.id.list_fragment, mFragment).commit();
// set the Behind View Fragment
Fragment searchFragment = new SampleSearchFragment();
searchFragment.setArguments(params);
getSupportFragmentManager().beginTransaction()
.replace(R.id.activity_search, searchFragment)
.commit();
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
getSupportFragmentManager().putFragment(outState, "mFragment",
mFragment);
}
And in my SampleListFragment
, I have this method:
public static boolean isNetworkAvailable(Activity activity) {
ConnectivityManager connectivity = (ConnectivityManager) activity
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity == null) {
return false;
} else {
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null) {
for (int i = 0; i < info.length; i++) {
if (info[i].getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
}
}
return false;
}
Inside of Asynctask
:
..........
@Override
protected void onPostExecute(String result) {
super.onPostExecute(null);
if (null != pDialog && pDialog.isShowing()) {
pDialog.dismiss();
}
if (isNetworkAvailable(getActivity())) { //Triggered NPE
From here, how do I know if getActivity()
is now attached to the parent activity before calling it?
Before saving the fragment in onSaveInstanceState
make sure to check if fragment is added to fragment manager
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
if (mFragment.isAdded()){
getSupportFragmentManager().putFragment(outState, "mFragment", mFragment);
}
}