App crashes(unfortunately app has stopped) when click homebutton on phone? <p>Using fragment app crashed when am click phone home button..i have 7 fragments in navigation.Apart from 2 fragment other not crashed when am clicked homebutton.i can not find issue. what the issue ? how can i solve it?</p> <p><strong>Source code</strong></p> <pre><code>package com.system.soft.proj; import android.net.ParseException; import android.os.AsyncTask; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.ListView; import android.widget.ProgressBar; import android.widget.TextView; import android.widget.Toast; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.util.EntityUtils; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; public class Followupshistory extends android.support.v4.app.Fragment { ArrayList&lt;Actors&gt; actorsList; TextView tv9; Followuphistoryadapter adapter; ListView listview; String msg = "default"; String crmadminid=""; String adminname=""; String admintype=""; String userprivilege=""; String crmcmpid=""; String username=""; String corporate=""; String password=""; String id = "",fid="",distitle="",city=""; SessionManager session; private ProgressBar bar; Button b5; String message = "", ccompany = "", cassign = "", ccategtory = ""; ArrayList&lt;Actors&gt; temarr; String position = ""; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View rootView = inflater.inflate(R.layout.fragment_followupshistory, null); MainActivity.mTitle = " Followup "; ((MainActivity) getActivity()).restoreActionBar(); bar = (ProgressBar) rootView.findViewById(R.id.progressBar); tv9 = (TextView) rootView.findViewById(R.id.textView9); session = new SessionManager(getActivity()); b5 = (Button) rootView.findViewById(R.id.button5); HashMap&lt;String, String&gt; user = session.getUserDetails(); username = user.get(SessionManager.USERNAME); password = user.get(SessionManager.PASSWORD); corporate = user.get(SessionManager.CORPORATE); crmadminid = user.get(SessionManager.CRMADMINID); crmcmpid = user.get(SessionManager.CRMCMPID); adminname = user.get(SessionManager.ADMINNAME); admintype = user.get(SessionManager.ADMINTYPE); userprivilege = user.get(SessionManager.USERPRIVILEGE); Bundle bundle = this.getArguments(); id = bundle.getString("cid"); fid = bundle.getString("fid"); distitle = bundle.getString("distitle"); city= bundle.getString("city"); temarr = (ArrayList&lt;Actors&gt;) bundle.getSerializable("temparr"); ccategtory = bundle.getString("ccategtory"); ccompany = bundle.getString("ccompany"); cassign = bundle.getString("cassign"); city = bundle.getString("city"); position = bundle.getString("position"); tv9.setText(" Followup for "+ distitle+" Service ,Location "+city); actorsList = new ArrayList&lt;Actors&gt;(); new JSONAsyncTask().execute("http://example.com/datacapture.php?userprivilege="+userprivilege+"&amp;admin_name="+adminname+"&amp;admintype="+admintype+"&amp;crm_admin_id="+crmadminid+"&amp;corporate="+corporate+"&amp;crm_cmpid="+crmcmpid+"&amp;s="+id+"&amp;user_name="+username+"&amp;corporate="+corporate+"&amp;password="+password+"&amp;fid="+fid); listview = (ListView) rootView.findViewById(R.id.list); adapter = new Followuphistoryadapter(getActivity(), R.layout.followuphistoryrow, actorsList); listview.setAdapter(adapter); b5.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { try { // Toast.makeText(mContext, "followupid"+fid, Toast.LENGTH_LONG).show(); Fragment fragment = null; fragment = new Reassign(); Bundle bundle = new Bundle(); bundle.putString("cid", id); bundle.putString("fid", fid); bundle.putString("distitle", distitle); bundle.putString("city", city); bundle.putString("ccategtory", ccategtory); bundle.putString("ccompany", ccompany); bundle.putString("cassign", cassign); String pos=String.valueOf(position); bundle.putString("position", pos); bundle.putSerializable("temparr",temarr); fragment.setArguments(bundle); FragmentManager fragmentManager = (getActivity()).getSupportFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); fragmentTransaction.replace(R.id.container, fragment).addToBackStack(null).commit(); } catch (Exception e) { Toast.makeText(getActivity(), String.valueOf(e.getMessage()), Toast.LENGTH_LONG).show(); } } }); return rootView; } class JSONAsyncTask extends AsyncTask&lt;String, Void, Boolean&gt; { @Override protected void onPreExecute() { bar.setVisibility(View.VISIBLE); super.onPreExecute(); } @Override protected Boolean doInBackground(String... urls) { try { //------------------&gt;&gt; HttpGet httppost = new HttpGet(urls[0]); HttpClient httpclient = new DefaultHttpClient(); HttpResponse response = httpclient.execute(httppost); // StatusLine stat = response.getStatusLine(); int status = response.getStatusLine().getStatusCode(); if (status == 200) { HttpEntity entity = response.getEntity(); String data = EntityUtils.toString(entity); JSONObject jsono = new JSONObject(data); JSONArray jarray = jsono.getJSONArray("followshistory"); msg=String.valueOf(jarray.length()); for (int i = 0; i &lt; jarray.length(); i++) { JSONObject object = jarray.getJSONObject(i); Actors actor = new Actors(); actor.setcompanyname(object.getString("followedon")); actor.setcontactperson(object.getString("creadtedby")); actor.setproduct(object.getString("remarks")); actor.setAssign(object.getString("nextdueon")); actor.setnextdue(object.getString("status")); actor.setlastaction(object.getString("smsstatus")); actor.setmobile(object.getString("message")); actorsList.add(actor); } return true; } //------------------&gt;&gt; } catch (ParseException e1) { e1.printStackTrace(); msg=String.valueOf(e1.getMessage().toString()); } catch (IOException e) { e.printStackTrace(); msg=String.valueOf(e.getMessage().toString()); } catch (JSONException e) { e.printStackTrace(); msg=String.valueOf(e.getMessage().toString()); } return false; } protected void onPostExecute(Boolean result) { bar.setVisibility(View.GONE); adapter.notifyDataSetChanged(); if(actorsList.size()==0) { Toast.makeText(getActivity().getApplicationContext(), "No Follow up Hostory", Toast.LENGTH_LONG).show(); } } } } </code></pre> <p>Xml file</p> <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.system.soft.proj.Followupshistory"&gt; &lt;TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceSmall" android:text="Followup History" android:id="@+id/textView9" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:gravity="center_vertical|center_horizontal" android:textColor="#ffff3d15" android:textStyle="bold" android:layout_marginLeft="5dp" android:layout_marginTop="5dp" android:layout_toStartOf="@+id/button5" android:layout_toLeftOf="@+id/button5" android:padding="5dp" /&gt; &lt;ListView android:id="@+id/list" android:layout_width="wrap_content" android:layout_height="match_parent" tools:listitem="@layout/followuphistoryrow" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_gravity="center_horizontal" android:layout_marginBottom="5dp" android:layout_below="@+id/button5"&gt; &lt;/ListView&gt; &lt;ProgressBar android:id="@+id/progressBar" style="?android:attr/progressBarStyleLarge" android:layout_width="wrap_content" android:layout_height="wrap_content" android:indeterminateDrawable="@drawable/progress" android:layout_gravity="center" android:layout_centerVertical="true" android:layout_centerHorizontal="true"&gt;&lt;/ProgressBar&gt; &lt;Button style="?android:attr/buttonStyleSmall" android:layout_width="40dp" android:layout_height="40dp" android:id="@+id/button5" android:layout_gravity="right|top" android:gravity="center_vertical|center_horizontal" android:background="@drawable/edit" android:layout_alignTop="@+id/textView9" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" android:layout_margin="5dp" /&gt; &lt;/RelativeLayout&gt; </code></pre>