android crash when using setVisibility(View.VISIBLE) -
i seem getting application crashes at:
parent.findviewbyid(r.id.a_1).setvisibility(view.visible);
not sure im doing wrong
code below
layouttext.java
public class layouttest extends activity { private spinner solvefor; private arrayadapter<charsequence> featuresadapter; private list<charsequence> featureslist; /** called when activity first created. */ @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); featureslist = new arraylist<charsequence>(); featuresadapter = new arrayadapter<charsequence>(this, android.r.layout.simple_spinner_item, featureslist); featuresadapter.setdropdownviewresource(android.r.layout.simple_spinner_dropdown_item); solvefor = ((spinner) layouttest.this.findviewbyid(r.id.solvefor)); solvefor.setadapter(featuresadapter); featuresadapter.add("velocity"); featuresadapter.add("time"); featuresadapter.add("distance"); solvefor.setonitemselectedlistener(new myonitemselectedlistener()); } }
myonitemselectedlistener.java package android.example.layouttest;
import android.view.view; import android.widget.adapterview; import android.widget.adapterview.onitemselectedlistener; import android.widget.toast; public class myonitemselectedlistener implements onitemselectedlistener { private static final int spinner_option_first = 0; private static final int spinner_option_second = 1; public void onitemselected(adapterview<?> parent, view view, int pos, long id) { switch(pos) { case spinner_option_first: { toast.maketext(parent.getcontext(), "0", toast.length_long).show(); break; } case spinner_option_second: { toast.maketext(parent.getcontext(), "1", toast.length_long).show(); parent.findviewbyid(r.id.a_1).setvisibility(view.visible); parent.findviewbyid(r.id.a_2).setvisibility(view.visible); break; } } } public void onnothingselected(adapterview<?> parent) { // nothing. } }
remember view#findviewbyid
starts search view call on , descends children. if view id you're looking not either view or descendant of view, search fail , findviewbyid
return null
.
as falmarri suggests, may not want call findviewbyid
on parent
in context. (you might not want call on view
there either.)
where in view hierarchy expecting find r.id.a_1
?
Comments
Post a Comment