Multiple layout files Android -
i have 2 xml files layout - main.xml , test.xml. here code in test.xml:
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <listview xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/menu_objoptions" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="10dp" > </listview> </linearlayout> when seek instance of listview
findviewbyid( r.id.menu_objoptions )
it returns null. why that?
edit: here relevant java code:
public void objclick(string objid, long x, long y) { final string objid = objid; final long x = x; final long y = y; seek { mhandle.runonuithread(new runnable() { @override public void run() { popupwindow popup; linearlayout layout; listview mainlistview; arrayadapter<string> listadapter; string[] planets = new string[] { "mercury", "venus", "earth", "mars", "jupiter", "saturn", "uranus", "neptune"}; arraylist<string> planetlist = new arraylist<string>(); planetlist.addall( arrays.aslist(planets) ); listadapter = new arrayadapter<string>(mhandle, r.layout.menu_objoptions_row, planetlist); listadapter.add( "ceres" ); listadapter.add( "pluto" ); listadapter.add( "haumea" ); listadapter.add( "makemake" ); listadapter.add( "eris" ); popup = new popupwindow(mhandle); layout = new linearlayout(mhandle); layout.setorientation(linearlayout.vertical); mainlistview = (listview) mhandle.findviewbyid( r.id.menu_objoptions ); mainlistview.setadapter( listadapter ); layout.addview(mainlistview); popup.setcontentview(layout); popup.setoutsidetouchable(true); popup.showatlocation(rlmain, gravity.no_gravity, 10, 10); popup.update((int)x,(int)y, 300, 80); } }); } grab (exception e) { e.printstacktrace(); } } nullpointerexception thrown @ line:
mainlistview.setadapter( listadapter );
it returns null. why that?
probably calling early. need phone call after
setcontentview(r.layout.somelayout); is called. setcontentview method should called before start initialising widgets, after phone call parent's constructor.
so in case need set linearlayout contentview before want initialise widget in case listview.
my suggestion initialise linearlayout in oncreate() method.
public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.yourlinearlayout); // must called first listview list = findviewbyid(r.id.menu_objoptions); // should works. } android android-layout
No comments:
Post a Comment