Manish Gupta Manish Gupta - 3 months ago 12
Android Question

Fragment crashing when rotate screen or minimise theapplication

I am going from 1st fragment to 2nd. The application crashes when i rotate or minimise the screen on the 2nd fragment. Earlier when i used to minimise the app 1st fragment was being called (don't know why), now its crashing here is the code :

2nd Fragment :
public class SubPlaceFragment extends Fragment {

public static final String LOG_TAG = SubPlaceFragment.class.getSimpleName();

private OnFragmentInteractionListener mListener;

public SubPlaceFragment() {
// Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_sub_place, container, false);
}

@Override
public void onAttach(Activity activity) {
super.onAttach(activity);

}

@Override
public void onDetach() {
super.onDetach();
mListener = null;
}

@Override
public void onPause() {
super.onPause();
Log.i(LOG_TAG,"onPause");
}

@Override
public void onResume() {
super.onResume();
Log.i(LOG_TAG, "onResume");
}

@Override
public void onStop() {
super.onStop();
Log.i(LOG_TAG, "onStop");
}

@Override
public void onStart() {
super.onStart();
Log.i(LOG_TAG, "onStart");
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.i(LOG_TAG, "onActivityCreated");
}

public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
}


this is my logcat :

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.manish.planetdelhi, PID: 16516
java.lang.RuntimeException: Unable to resume activity {com.example.manish.planetdelhi/com.example.manish.planetdelhi.MainActivity}: java.lang.ClassCastException: com.example.manish.planetdelhi.SubPlaceFragment cannot be cast to com.example.manish.planetdelhi.PlaceListFragment
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2812)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2845)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1252)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5086)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassCastException: com.example.manish.planetdelhi.SubPlaceFragment cannot be cast to com.example.manish.planetdelhi.PlaceListFragment
at com.example.manish.planetdelhi.MainActivity.AddPlaceFragment(MainActivity.java:74)
at com.example.manish.planetdelhi.MainActivity.onStart(MainActivity.java:80)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1194)
at android.app.Activity.performStart(Activity.java:5258)
at android.app.Activity.performRestart(Activity.java:5314)
at android.app.Activity.performResume(Activity.java:5319)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2797)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2845) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1252) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:136) 
at android.app.ActivityThread.main(ActivityThread.java:5086) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:515) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 
at dalvik.system.NativeStart.main(Native Method) 


This is my full main activity code :

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
AddPlaceFragment(savedInstanceState);
}

@Override
protected void onPause() {
super.onPause();
}

@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
}

private void AddPlaceFragment(Bundle loadingFirstTime)
{
// calling placeListFragment for the first time
if (loadingFirstTime == null) {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.fragment_container, placeListFragment).addToBackStack(placeListFragment.getTag());
fragmentTransaction.commit();
}
}

// calling placeListFragment when screen is rotated
private void AddPlaceFragment()
{
placeListFragment = (PlaceListFragment) getFragmentManager().findFragmentById(R.id.fragment_container);
}

@Override
protected void onStart() {
super.onStart();
AddPlaceFragment();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}

@Override
public void onBackPressed() {
int fragmentCount = getFragmentManager().getBackStackEntryCount();
if(fragmentCount > 1)
{
getFragmentManager().popBackStack();
}
else {
finish();
}
}

@Override
public void DelhiPlaceClicked(int paramInt, String paramString) // meathod invoked from placelistfragment
{
Bundle localBundle = new Bundle();
localBundle.putInt("_id", paramInt);
localBundle.putString("sub_place_name", paramString);
SubPlaceFragment subfragment = new SubPlaceFragment();
subfragment.setArguments(localBundle);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, subfragment).addToBackStack(subfragment.getTag());
fragmentTransaction.commit();
}


Thanks for help :)

Answer

You are adding your subfragment by:

fragmentTransaction.replace(R.id.fragment_container, subfragment)

Now, a call to:

getFragmentManager().findFragmentById(R.id.fragment_container);

will return your subfragment only. Hence, you are getting the classcast exception