android - NullPointer Exception in getWidth(), getHeight() -
i working on project . need width & height of linearlayout activity using programming code. linear layout has fixed width , height . when use following ..i getting nullpointer exception
linearlayout viewgroup = (linearlayout) context.findviewbyid(r.id.popup); log.e("getwidth",""+viewgroup.getwidth()); log.e("getheight",""+viewgroup.getheight());
i need width , height of layout activity.
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/popup" android:layout_width="150dp" android:layout_height="252dp" android:background="#303030" android:orientation="vertical" > </linearlayout>
here java code file
public class mainactivity extends activity {
//the "x" , "y" position of "show button" on screen. point p; button btn_show; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); btn_show = (button) findviewbyid(r.id.show_popup); } @override protected void onresume() { super.onresume(); btn_show.setonclicklistener(new onclicklistener() { @override public void onclick(view arg0) { //open popup window if (p != null) showpopup( p); } }); } // x , y position after button draw on screen // (it's important note can't position in oncreate(), // because @ stage view isn't drawn yet, return (0, 0)) @override public void onwindowfocuschanged(boolean hasfocus) { int[] location = new int[2]; button button = (button) findviewbyid(r.id.show_popup); // x, y location , store in location[] array // location[0] = x, location[1] = y. button.getlocationonscreen(location); //initialize point x, , y positions p = new point(); p.x = location[0]; p.y = location[1]; } // method displays popup. private void showpopup( point p) { int popupwidth = 200; int popupheight = 380; // inflate popup_layout.xml linearlayout viewgroup = (linearlayout) findviewbyid(r.id.popup); layoutinflater layoutinflater = (layoutinflater) getsystemservice(context.layout_inflater_service); view layout = layoutinflater.inflate(r.layout.a, viewgroup); log.e("getwidth",""+viewgroup.getwidth()); log.e("getheight",""+viewgroup.getheight()); // creating popupwindow final popupwindow popup = new popupwindow(getapplicationcontext()); popup.setcontentview(layout); popup.setwidth(viewgroup.getwidth()); popup.setheight(viewgroup.getheight()); popup.setfocusable(true); // offset align popup bit right, , bit down, relative button's position. int offset_x = 30; int offset_y = 30; // clear default translucent background popup.setbackgrounddrawable(new bitmapdrawable()); // displaying popup @ specified location, + offsets. popup.showatlocation(layout, gravity.no_gravity, p.x + offset_x, p.y + offset_y); // getting reference close button, , close popup when clicked. // button close = (button) layout.findviewbyid(r.id.close); /* close.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { popup.dismiss(); } });*/ } }
it may viewgroup
hasn't been created yet.
check you're trying width , height of object being called after display objects such views etc being created. can debug either placing breakpoints in different loading methods such oncreate
, onresume
or placing nslog
's in them instead.
Comments
Post a Comment