Domanda

In my app,I should find width and height of a frame layout at the time of activity is loading.When i tried this,i am getting null pointer exception displaying width and height must be greater than zero.Please help me how to find width and height of frame layout in my xml.Thanks in advance.

my main Activity is

  import android.app.Activity;
  import android.graphics.Bitmap;
  import android.graphics.BitmapFactory;
   import android.os.Bundle;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.view.ViewTreeObserver.OnGlobalLayoutListener;
   import android.widget.Button;
   import android.widget.FrameLayout;
  import android.widget.ImageView;

  public class MainActivity extends Activity {
ImageView  image;
FrameLayout frame;
  int width,height;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    image=(ImageView)findViewById(R.id.imageView2);
    frame=(FrameLayout)findViewById(R.id.frame);


    frame.getViewTreeObserver().addOnGlobalLayoutListener( 
            new OnGlobalLayoutListener(){
                @Override
                public void onGlobalLayout() {                   
                    width=frame.getWidth();
                    height = frame.getHeight(); 

                    frame.getViewTreeObserver().removeGlobalOnLayoutListener( this );                               
                }

        });



    Bitmap bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
      Bitmap scaled=Bitmap.createScaledBitmap(bitmap, width, height, true);
    image.setImageBitmap(scaled);
    image.setOnTouchListener(new myTouchListener());

       }
       }

and my main xml is

   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:background="@android:color/black"
   android:orientation="vertical" 
   android:weightSum="3"
   >  
   <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:orientation="vertical" 
    android:id="@+id/top_linear"
    android:layout_weight="1"
   >
  </LinearLayout>

   <FrameLayout
    android:layout_width="match_parent"
    android:layout_height="380dp"
    android:id="@+id/frame" 
     >
     <ImageView
    android:id="@+id/imageView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:scaleType="matrix"
     />    
  </FrameLayout>    
  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="36dp"
    android:orientation="horizontal" 
    android:background="@android:color/black"
     android:id="@+id/bottom_linear">

   </LinearLayout>


   </LinearLayout>
È stato utile?

Soluzione

Move your code for the Bitmap handling into the block of the ViewTreeObeserver. Before this code block will be executed the width and height are 0.

frame.getViewTreeObserver().addOnGlobalLayoutListener( 
        new OnGlobalLayoutListener(){
            @Override
            public void onGlobalLayout() {                   
                width=frame.getWidth();
                height = frame.getHeight(); 

                Bitmap bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
                Bitmap scaled=Bitmap.createScaledBitmap(bitmap, width, height, true);
                image.setImageBitmap(scaled);
                image.setOnTouchListener(new myTouchListener());

                frame.getViewTreeObserver().removeGlobalOnLayoutListener( this );                               
            }

    });

Altri suggerimenti

getWidth() and getHeight() methods return 0 when layouts width and height are set like match_parent and wrap_content. dimensions are not measured.

The right methods to access to get measured dimensions are getMeasuredWidth() and getMeasuredHeight().

The Correct way:

FrameLayout frmlayout = (FrameLayout)findViewById(R.id.svtest);
ViewTreeObserver vto = frmlayout.getViewTreeObserver(); 
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 
    @Override 
    public void onGlobalLayout() { 
        scrollView.getViewTreeObserver().removeGlobalOnLayoutListener(this); 
        int width  = frmlayout.getMeasuredWidth();
        int height = frmlayout.getMeasuredHeight(); 

    } 
});
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top