Initializing value to views in ActionBar Tab framents in android while onCreate of Tab Activity -
my app uses actionbar tab fragments create/modify device details. device properties of categorized different fragments. want initialize each fragments in oncreate of tab activity. in each tab fragment has setvalues method initializes values view in fragment. noticed fragment created when clicked on particular tab.
tab activity
public class cameradetails extends activity { private cameradata camera = new cameradata(); fragment network = new networkfragment(); fragment remoteuser = new remoteuserfragment(); private g.interface mnetworkinterface = null; private g.interface mremoteuserinterface = null; /** called when activity first created. */ @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.cameradetails); actionbar actionbar = getactionbar(); actionbar.setdisplayshowhomeenabled(false); actionbar.setdisplayshowtitleenabled(false); actionbar.setnavigationmode(actionbar.navigation_mode_tabs); // network tab actionbar.tab tab = actionbar.newtab(); tab.settext("network"); tab.seticon(r.drawable.ic_launcher); tab.settablistener(new tablistener(network)); actionbar.addtab(tab); // remote user tab tab = actionbar.newtab(); tab.settext("remote user"); tab.seticon(r.drawable.ic_launcher); tab.settablistener(new tablistener(remoteuser)); actionbar.addtab(tab); final handler handler = new handler(); handler.postdelayed(new runnable() { @override public void run() { init(); } }, 1000); } public void init(){ try{ byte[] = new byte[512]; string extstoragedirectory = environment.getexternalstoragedirectory().tostring(); file file = new file(extstoragedirectory, "dump.hex"); bufferedinputstream in = new bufferedinputstream( new fileinputstream(file)); in.read(by); in.close(); camera.set( ); mnetworkinterface.setvalues(); mremoteuserinterface.setvalues(); } catch(exception e){ e.printstacktrace(); } } public cameradata getcameradata(){ return camera; } public void setnetworklistener( g.interface interface ){ this.mnetworkinterface = interface; } public void setremoteuserlistener( g.interface interface ){ this.mremoteuserinterface = interface; } }
one of fragment tab
public class networkfragment extends fragment implements g.interface { view rootview; activity mactivity; cameradata camera; @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { rootview = inflater.inflate(r.layout.network_layout, container, false); log.i("networkfragment", "createview"); return rootview; } @override public void onattach(activity activity) { mactivity = activity; ((cameradetails)mactivity).setnetworklistener(this); camera = ((cameradetails)mactivity).getcameradata(); log.i("networkfragment", "activity attached"); super.onattach(activity); } @override public void setvalues() { try { // ip address edittext et = (edittext)rootview.findviewbyid(r.id.cd_ip_address); et.settext(camera.mnetwork.ipaddress); // subnet mask et = (edittext)rootview.findviewbyid(r.id.cd_subnetmask); et.settext(camera.mnetwork.subnetmask); // gateway et = (edittext)rootview.findviewbyid(r.id.cd_gateway); et.settext(camera.mnetwork.gateway); // com port et = (edittext)rootview.findviewbyid(r.id.cd_comm_port); et.settext(""+camera.mnetwork.commport); // mimg port et = (edittext)rootview.findviewbyid(r.id.cd_mimg_port); et.settext(""+camera.mnetwork.mimgport); // webserver port et = (edittext)rootview.findviewbyid(r.id.cd_web_server_port); et.settext(""+camera.mnetwork.webserverport); // wifi id et = (edittext)rootview.findviewbyid(r.id.cd_wifi_id); et.settext(camera.mnetwork.wifiid); // wifi password et = (edittext)rootview.findviewbyid(r.id.cd_wifi_pwd); et.settext(camera.mnetwork.wifipassword); // wifi mode spinner sp = (spinner)rootview.findviewbyid(r.id.cd_wifi_mode); sp.setselection(camera.mnetwork.wifimode, true); log.i("networkfragment", "setvalues ok"); } catch( exception e ){ } } }
init method of tab activity initializes values fragment networkfragment , remoteuserfragment. values initialized networkfragment. nullpointerexception remoteuserfragment.
logcat
06-11 12:06:38.161: w/system.err(677): java.lang.nullpointerexception 06-11 12:06:38.201: w/system.err(677): @ com.mtxlabs.cameraviewer.cameradetails.init(cameradetails.java:114) 06-11 12:06:38.201: w/system.err(677): @ com.mtxlabs.cameraviewer.cameradetails$1.run(cameradetails.java:98) 06-11 12:06:38.201: w/system.err(677): @ android.os.handler.handlecallback(handler.java:615) 06-11 12:06:38.211: w/system.err(677): @ android.os.handler.dispatchmessage(handler.java:92) 06-11 12:06:38.211: w/system.err(677): @ android.os.looper.loop(looper.java:137) 06-11 12:06:38.211: w/system.err(677): @ android.app.activitythread.main(activitythread.java:4745) 06-11 12:06:38.211: w/system.err(677): @ java.lang.reflect.method.invokenative(native method) 06-11 12:06:38.211: w/system.err(677): @ java.lang.reflect.method.invoke(method.java:511) 06-11 12:06:38.211: w/system.err(677): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:786) 06-11 12:06:38.211: w/system.err(677): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:553) 06-11 12:06:38.211: w/system.err(677): @ dalvik.system.nativestart.main(native method)
how can solve problem or other technique used it.
advance thanks
i called setvalues method in oncreateview of fragment. solved problem
main activity
public class cameradetails extends activity { private cameradata camera = new cameradata(); fragment network = new networkfragment(); fragment remoteuser = new remoteuserfragment(); fragment rtsp = new rtspfragmenttab(); fragment video = new videofragmenttab(); fragment record = new recordfragmenttab(); fragment alarm = new alarmfragmenttab(); fragment productinfo = new productinfofragmenttab(); private g.interface mnetworkinterface = null; private g.interface mremoteuserinterface = null; /** called when activity first created. */ @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.cameradetails); initcameradata(); actionbar actionbar = getactionbar(); actionbar.setdisplayshowhomeenabled(false); actionbar.setdisplayshowtitleenabled(false); actionbar.setnavigationmode(actionbar.navigation_mode_tabs); // network tab actionbar.tab tab = actionbar.newtab(); tab.settext("network"); tab.seticon(r.drawable.ic_launcher); tab.settablistener(new tablistener(network)); actionbar.addtab(tab); // remote user tab tab = actionbar.newtab(); tab.settext("remote user"); tab.seticon(r.drawable.ic_launcher); tab.settablistener(new tablistener(remoteuser)); actionbar.addtab(tab); } public void initcameradata(){ try{ byte[] = new byte[512]; string extstoragedirectory = environment.getexternalstoragedirectory().tostring(); file file = new file(extstoragedirectory, "dump.hex"); bufferedinputstream in = new bufferedinputstream( new fileinputstream(file)); in.read(by); in.close(); camera.set( ); } catch(exception e){ e.printstacktrace(); } } public void onclick(view v){ getactionbar().setselectednavigationitem(0); if(!mnetworkinterface.validate()) return; getactionbar().setselectednavigationitem(1); if(!mremoteuserinterface.validate()) return; try{ byte[] bytes = camera.get(); string extstoragedirectory = environment.getexternalstoragedirectory().tostring(); file file = new file(extstoragedirectory, "dump.hex"); bufferedoutputstream out = new bufferedoutputstream( new fileoutputstream(file, false)); out.write(bytes); out.close(); log.v("data", "setup data updated"); } catch (exception e) { e.printstacktrace(); return ; } } public cameradata getcameradata(){ return camera; } public void setnetworklistener( g.interface interface ){ this.mnetworkinterface = interface; } public void setremoteuserlistener( g.interface interface ){ this.mremoteuserinterface = interface; } }
network fragment
public class networkfragment extends fragment implements g.interface { view rootview; activity mactivity; cameradata camera; @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { rootview = inflater.inflate(r.layout.network_layout, container, false); log.i("networkfragment", "createview"); setvalues(); return rootview; } @override public void onattach(activity activity) { mactivity = activity; ((cameradetails)mactivity).setnetworklistener(this); camera = ((cameradetails)mactivity).getcameradata(); log.i("networkfragment", "activity attached"); super.onattach(activity); } @override public boolean validate() { try { string s; int i; // ip address camera.mnetwork.ipaddress = g.getiptext(rootview, r.id.cd_ip_address); if( camera.mnetwork.ipaddress.isempty()){ toast.maketext(mactivity, "please enter valid ip address", toast.length_short).show(); return false; } // subnet mask camera.mnetwork.subnetmask = g.getiptext(rootview, r.id.cd_subnetmask); if( camera.mnetwork.subnetmask.isempty() ){ toast.maketext(mactivity, "please enter valid subnetmask", toast.length_short).show(); return false; } // gateway camera.mnetwork.gateway = g.getiptext(rootview, r.id.cd_gateway); if( camera.mnetwork.gateway.isempty()){ toast.maketext(mactivity, "please enter valid gateway", toast.length_short).show(); return false; } // com port camera.mnetwork.commport = g.getinteger(rootview, r.id.cd_comm_port); if( camera.mnetwork.commport < 0 ){ toast.maketext(mactivity, "please enter valid communication port", toast.length_short).show(); return false; } // mimg port camera.mnetwork.mimgport = g.getinteger(rootview, r.id.cd_mimg_port); if( camera.mnetwork.mimgport < 0 ){ toast.maketext(mactivity, "please enter valid mimg port", toast.length_short).show(); return false; } // webserver port camera.mnetwork.webserverport = g.getinteger(rootview, r.id.cd_web_server_port); if( camera.mnetwork.webserverport < 0 ){ toast.maketext(mactivity, "please enter valid webserver port", toast.length_short).show(); return false; } // wifi id camera.mnetwork.wifiid = g.gettext(rootview, r.id.cd_wifi_id); if( camera.mnetwork.wifiid.isempty() ){ toast.maketext(mactivity, "please enter valid wifi id", toast.length_short).show(); return false; } // wifi password camera.mnetwork.wifipassword = g.gettext(rootview, r.id.cd_wifi_pwd); if( camera.mnetwork.wifipassword.isempty() ){ toast.maketext(mactivity, "please enter valid wifi password", toast.length_short).show(); return false; } // wifi mode camera.mnetwork.wifimode = (byte)g.getspinneroptions(rootview, r.id.cd_wifi_mode); if( camera.mnetwork.wifimode == -1 ){ toast.maketext(mactivity, "please select valid wifi mode list", toast.length_short).show(); return false; } log.i("networkfragment", "validation ok"); } catch( exception e ){ e.printstacktrace(); } return true; } public void setvalues() { try { // ip address edittext et = (edittext)rootview.findviewbyid(r.id.cd_ip_address); et.settext(camera.mnetwork.ipaddress); // subnet mask et = (edittext)rootview.findviewbyid(r.id.cd_subnetmask); et.settext(camera.mnetwork.subnetmask); // gateway et = (edittext)rootview.findviewbyid(r.id.cd_gateway); et.settext(camera.mnetwork.gateway); // com port et = (edittext)rootview.findviewbyid(r.id.cd_comm_port); et.settext(""+camera.mnetwork.commport); // mimg port et = (edittext)rootview.findviewbyid(r.id.cd_mimg_port); et.settext(""+camera.mnetwork.mimgport); // webserver port et = (edittext)rootview.findviewbyid(r.id.cd_web_server_port); et.settext(""+camera.mnetwork.webserverport); // wifi id et = (edittext)rootview.findviewbyid(r.id.cd_wifi_id); et.settext(camera.mnetwork.wifiid); // wifi password et = (edittext)rootview.findviewbyid(r.id.cd_wifi_pwd); et.settext(camera.mnetwork.wifipassword); // wifi mode spinner sp = (spinner)rootview.findviewbyid(r.id.cd_wifi_mode); sp.setselection(camera.mnetwork.wifimode, true); log.i("networkfragment", "setvalues ok"); } catch( exception e ){ e.printstacktrace(); } } }
anyway @cricket_007 helping me understand fragment tab operation
Comments
Post a Comment