getting null pointer exception when mobile switching from portrait to landscape and reversal

157 Views Asked by At

can u explain where did i done a mistake..

My fragment class contains the async task

class FormDetails extends AsyncTask<String, String, FormFull> {

NetworkConnection nc;
NetworkConnectivity ncty;
FormFull fList=null;
ProgressDialog pd;

@Override
 protected void onPreExecute() {
// TODO Auto-generated method stub
pd=new ProgressDialog(getActivity());
pd.show();
super.onPreExecute();
 }

@Override
protected void onProgressUpdate(String... values) {
// TODO Auto-generated method stub
pd.setMessage(values[0]);
super.onProgressUpdate(values);
}

@Override
 protected FormFull doInBackground(String... params) {
nc=new NetworkConnection();
ncty=new NetworkConnectivity(getActivity());
if(ncty.haveNetworkConnection()){
publishProgress("Please Wait..");
fList=nc.getFormDetails(params[0], params[1], params[2]);
}else{
Toast.makeText(getActivity(), "No Network Found", Toast.LENGTH_LONG).show();
}
return fList;
 }
@Override
protected void onPostExecute(FormFull result) {
// TODO Auto-generated method stub
pd.dismiss();
if(result!=null){
afaqd.clear();
afard.clear();
if(result.getFqndata()!=null){
afaqd.addAll(result.getFqndata());
}
if(result.getFresdata()!=null){
afard.addAll(result.getFresdata());
}
fad.notifyDataSetChanged();
}
super.onPostExecute(result);
}
}


@Override
public void onPause() {
super.onPause();
if(pd != null){
pd.dismiss();
}
}
}

i am getting same problem for progress dialogue but when i added onPause() method it resolves but how about the remaining like toast and network connectivity class

and my network connectivity class

public class NetworkConnectivity {

Context ct;
ConnectivityManager cm; 
NetworkInfo networkInfo;
public NetworkConnectivity(Context ct){
this.ct=ct;
}

public boolean haveNetworkConnection() {
boolean haveConnectedWifi = false;
boolean haveConnectedMobile = false;

  cm  = (ConnectivityManager) ct.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo[] netInfo = cm.getAllNetworkInfo();
for (NetworkInfo ni : netInfo) {
if (ni.getTypeName().equalsIgnoreCase("WIFI"))
if (ni.isConnected())
haveConnectedWifi = true;
if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
if (ni.isConnected())
haveConnectedMobile = true;
}
return haveConnectedWifi || haveConnectedMobile;
}

public String networkName(){
 cm  = (ConnectivityManager) ct.getSystemService(Context.CONNECTIVITY_SERVICE);
 networkInfo = cm.getActiveNetworkInfo();

 if (networkInfo != null && networkInfo.isConnected()) {
 if(networkInfo.getType() == ConnectivityManager.TYPE_WIFI)
 return "wifi";
 else 
 return "mobile";
}else
return "not";
}

}

getting null pointer exception for both Toast and also network connectivity class..

Thanks in advance

2

There are 2 best solutions below

0
On

add this to the activity in your manifest:

android:configChanges="orientation|screenSize"
0
On

Test if getActivity is null.

@Override
 protected FormFull doInBackground(String... params) {
  nc=new NetworkConnection();
  if (getActivity()!=null) {
  ncty=new NetworkConnectivity(getActivity());
  if(ncty.haveNetworkConnection()){
  publishProgress("Please Wait..");
  fList=nc.getFormDetails(params[0], params[1], params[2]);
  }else{
  Toast.makeText(getActivity(), "No Network Found", Toast.LENGTH_LONG).show();
  }
                           }
  return fList;
 }