android - The opening of the layouts as conditional status in the activity -


i have 2 layouts. 1 show in main activity if have internet connection. , other 1 show in same activity if have not internet connection.

i want when launch program , there internet connection see activity_main.xml layout. , want when launch program , there not internet connection see error.xml layout.

but when run program can not top description well.please not me not know concepts android , please me solve problem.

public class mainactivity extends activity {     webview webview;     imageview imageview1,imageview2,imageview3;     drawable drawable0,drawable1,drawable2;     edittext edittext;     @override     protected void oncreate(bundle s){          if(isconnected()==false){             super.oncreate(s);             edittext = (edittext) findviewbyid(r.id.textview);             setcontentview(r.layout.error);         }         else if(isconnected()==true){ super.oncreate(s);             webview = (webview) findviewbyid(r.id.webview);             webview.setbackgroundcolor(color.rgb(88,255,114));             imageview1 = (imageview) findviewbyid(r.id.imageview);             imageview2 = (imageview) findviewbyid(r.id.imageview2);             imageview3 = (imageview) findviewbyid(r.id.imageview3);             drawable0 = loadimagefromweboperations("http://127.0.0.1:8080/apps/image.jpg");             drawable1 = loadimagefromweboperations("http://127.0.0.1:8080/apps/image2.jpg");             drawable2 = loadimagefromweboperations("http://127.0.0.1:8080/apps/image4.jpg");             imageview1.setimagedrawable(drawable0);             imageview2.setimagedrawable(drawable1);             imageview3.setimagedrawable(drawable2);             setcontentview(r.layout.activity_main);         }     }      public boolean isconnected(){         connectivitymanager connmgr = (connectivitymanager) getsystemservice(this.connectivity_service);         networkinfo networkinfo = connmgr.getactivenetworkinfo();         if (networkinfo != null && networkinfo.isconnected())             return true;         else             return false;     }      private drawable loadimagefromweboperations(string url)     {         try{             inputstream = (inputstream) new url(url).getcontent();             drawable d = drawable.createfromstream(is,"");             return d;         }catch (exception e) {              return null;         }     }} 

your code fail because call findviewbyid before calling setcontentview. findviewbyid return null , imageview1.setimagedrawable(drawable0); lead nullpointerexception.

and doing networking operation on main thread (in loadimagefromweboperations(string url)). reason code fail.


Comments