-2

我正在做项目。我需要使用编程代码的 Activity 的 LinearLayout 的宽度和高度。此线性布局具有固定的宽度和高度。但是当我使用以下..我得到 Nullpointer Exception

LinearLayout viewGroup = (LinearLayout) context.findViewById(R.id.popup);
Log.e("getWidth",""+viewGroup.getWidth());
Log.e("getHeight",""+viewGroup.getHeight());

我需要活动中该布局的宽度和高度。

<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>

这是Java代码文件

公共类 MainActivity 扩展 Activity {

//The "x" and "y" position of the "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);
         }
       });
}



// Get the x and y position after the button is draw on screen
// (It's important to note that we can't get the position in the onCreate(),
// because at that stage most probably the view isn't drawn yet, so it will return (0, 0))
@Override
public void onWindowFocusChanged(boolean hasFocus) {

   int[] location = new int[2];
   Button button = (Button) findViewById(R.id.show_popup);

   // Get the x, y location and store it in the location[] array
   // location[0] = x, location[1] = y.
   button.getLocationOnScreen(location);

   //Initialize the Point with x, and y positions
   p = new Point();
   p.x = location[0];
   p.y = location[1];
}

// The method that displays the popup.
private void showPopup( Point p) {
   int popupWidth = 200;
   int popupHeight = 380;

   // Inflate the 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 the PopupWindow
   final PopupWindow popup = new PopupWindow(getApplicationContext());
   popup.setContentView(layout);
   popup.setWidth(viewGroup.getWidth());
   popup.setHeight(viewGroup.getHeight());
   popup.setFocusable(true);

   // Some offset to align the popup a bit to the right, and a bit down, relative to button's position.
   int OFFSET_X = 30;
   int OFFSET_Y = 30;

   // Clear the default translucent background
   popup.setBackgroundDrawable(new BitmapDrawable());

   // Displaying the popup at the specified location, + offsets.
   popup.showAtLocation(layout, Gravity.NO_GRAVITY, p.x + OFFSET_X, p.y + OFFSET_Y);

   // Getting a reference to Close button, and close the popup when clicked.
  // Button close = (Button) layout.findViewById(R.id.close);
  /* close.setOnClickListener(new OnClickListener() {

     @Override
     public void onClick(View v) {
       popup.dismiss();
     }
   });*/
}
}
4

4 回答 4

0

要等待视图被附加并放置在布局中,您应该使用ViewTreeObserver.OnGlobalLayoutListener

你可以像这样注册它:

v.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

 public void onGlobalLayout() {

          //TODO: do your stuff here

          //if you change something in the layout you have to add this
          //line to avoid loops
          v.getViewTreeObserver().removeGlobalOnLayoutListener(this);
  }

});

在该回调中,您可以确保视图具有一致的位置和尺寸。

于 2013-08-28T12:17:42.243 回答
0

只有第一次调用View.onSizeChanged ()方法后,您才能可靠地使用and方法。这意味着您必须更改应用程序的逻辑才能考虑到这一事实。getHeight()getWidth()

于 2013-08-28T11:32:45.110 回答
0

这可能ViewGroup还没有创建。

在创建显示对象(例如视图等)后,检查您试图获取此对象的宽度和高度的位置。您可以通过在不同的加载方法中放置断点来调试它,例如onCreateonResume或者在其中放置NSLog's。

于 2013-08-28T11:18:25.880 回答
0

您只是为了获得宽度和高度而膨胀布局,不是吗?如果是这样,您不需要 viewGroup。假设 R.layout.popup_layout 指向具有固定尺寸的 LinearLayout:

LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout layout = layoutInflater.inflate(R.layout.popup_layout, null);
LayoutParams params = layout.getLayoutParams();
Log.e("getWidth",""+params.width);
Log.e("getHeight",""+params.height);

之后,您可以设置弹出窗口:

final PopupWindow popup = new PopupWindow(getApplicationContext());
popup.setContentView(layout);
popup.setWidth(params.width);
popup.setHeight(params.height);
popup.setFocusable(true);
于 2013-08-28T12:03:10.120 回答