here create 4 xml files main.xml
,test1.xml
,test2.xml
,test3.xml
.
they accessed in spinner
received exception. giving logcat also.
please me.
string[] lan = { "test1", "test2", "test3" }; spinner spn = (spinner) findviewbyid(r.id.spinner1); arrayadapter<string> adapter = new arrayadapter<string>(this, android.r.layout.simple_spinner_dropdown_item, lan); spn.setadapter(adapter); spn.setonitemselectedlistener(new onitemselectedlistener() { @override public void onitemselected(adapterview<?> parent, view v, int pos, long id) { if (pos == 0) { findviewbyid(r.layout.test1).setvisibility(view.visible); } else if (pos == 1) { findviewbyid(r.layout.test2).setvisibility(view.visible); } else if (pos == 2) { findviewbyid(r.layout.test3).setvisibility(view.visible); } } @override public void onnothingselected(adapterview<?> arg0) { } });
logcat:
07-11 18:09:39.480: e/androidruntime(21200): fatal exception: main 07-11 18:09:39.480: e/androidruntime(21200): java.lang.nullpointerexception 07-11 18:09:39.480: e/androidruntime(21200): @ com.venky.innverlayoutchangevisibility.mainactivity$1.onitemselected(mainactivity.java:30) 07-11 18:09:39.480: e/androidruntime(21200): @ android.widget.adapterview.fireonselected(adapterview.java:871) 07-11 18:09:39.480: e/androidruntime(21200): @ android.widget.adapterview.access$200(adapterview.java:42) 07-11 18:09:39.480: e/androidruntime(21200): @ android.widget.adapterview$selectionnotifier.run(adapterview.java:837) 07-11 18:09:39.480: e/androidruntime(21200): @ android.os.handler.handlecallback(handler.java:587) 07-11 18:09:39.480: e/androidruntime(21200): @ android.os.handler.dispatchmessage(handler.java:92) 07-11 18:09:39.480: e/androidruntime(21200): @ android.os.looper.loop(looper.java:130) 07-11 18:09:39.480: e/androidruntime(21200): @ android.app.activitythread.main(activitythread.java:3691) 07-11 18:09:39.480: e/androidruntime(21200): @ java.lang.reflect.method.invokenative(native method) 07-11 18:09:39.480: e/androidruntime(21200): @ java.lang.reflect.method.invoke(method.java:507) 07-11 18:09:39.480: e/androidruntime(21200): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:907) 07-11 18:09:39.480: e/androidruntime(21200): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:665) 07-11 18:09:39.480: e/androidruntime(21200): @ dalvik.system.nativestart.main(native method)
enter code hereyou cannot use components different layout like. need define layoutinflator
, work that. r.id.yourcomponent
can access components defined (mostly) in oncreate method like:
setcontentview(r.layout.main);
to need, create inflator each 1 of layouts (note: make work, give id each 1 of main linearlayouts in test1.xml, test2.xml, test3.xml , access that, instead of file itself):
string[] lan = { "test1", "test2", "test3" }; spinner spn = (spinner) findviewbyid(r.id.spinner1); arrayadapter<string> adapter = new arrayadapter<string>(this, android.r.layout.simple_spinner_dropdown_item, lan); //duplicate code every .xml layout inside onitemselected layoutinflater inflater = (layoutinflater) getsystemservice(context.layout_inflater_service); view firstview = inflater.inflate(r.layout.test1, null); //inflates test1.xml linearlayout test1layout = (linearlayout) firstview.findviewbyid(r.id.idofthelayout); //this finds component inside test1.xml file. mentioned, give highest layout holds components id. test1layout.setvisibility(view.visible); spn.setadapter(adapter); spn.setonitemselectedlistener(new onitemselectedlistener() { @override public void onitemselected(adapterview<?> parent, view v, int pos, long id) { if (pos == 0) { findviewbyid(r.layout.test1).setvisibility(view.visible); } else if (pos == 1) { findviewbyid(r.layout.test2).setvisibility(view.visible); } else if (pos == 2) { findviewbyid(r.layout.test3).setvisibility(view.visible); } }
Comments
Post a Comment