Tuesday, 10 September 2013

Android crash activity when cannot access the url

Android crash activity when cannot access the url

I'm newbie in Android i will appreciate if anybody help me with this code:
I have got a button for CheckNetworkConnectivity if WiFi not available
show toast with text (Network not available) that is file, but when
network available but can't access to URL activity will crash...
Button btnEx = (Button) findViewById(R.id.button);
btnEx.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
checkNetworkConnectivity();
}
});
}
protected void checkNetworkConnectivity() {
// TODO Auto-generated method stub
ConnectivityManager connMgr = (ConnectivityManager)
this.getSystemService(Context.CONNECTIVITY_SERVICE);
final android.net.NetworkInfo wifi =
connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
final android.net.NetworkInfo mobile =
connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if( wifi.isAvailable() || mobile.isAvailable()){
Intent gogogo = new Intent(InfoActivity.this, exampleActivity.class);
String result = null;
gogogo.putExtra("result", result);
startActivity(gogogo);
}
else{
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.toastlayout01,
(ViewGroup) findViewById(R.id.toast_layout_root));
TextView tv = (TextView) layout.findViewById(R.id.text);
tv.setText("No Network Avilable");
Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER, 0, 0);
toast.setDuration(Toast.LENGTH_SHORT);
toast.setView(layout);
toast.show();
}
Thanks for any help in advance....

No comments:

Post a Comment