移动从纵向切换到横向和反转时获取空指针异常

时间:2014-11-22 12:13:25

标签: android android-fragmentactivity android-toast

你可以解释我在哪里犯了错误。

我的片段类包含异步任务

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();
}
}
}

我在进度对话方面遇到了同样的问题但是当我添加onPause()方法时它会解析但是剩下的就像toast和网络连接类一样

和我的网络连接类

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";
}

}

获取Toast和网络连接类的空指针异常。

提前致谢

2 个答案:

答案 0 :(得分:0)

将此添加到清单中的活动:

android:configChanges="orientation|screenSize"

答案 1 :(得分:0)

测试getActivity是否为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;
 }