1. Are you ready for the Galaxy S20? Here is everything we know so far!

NullPointerException when using getLayoutParams

Discussion in 'Android Development' started by Woman In Love, Jul 27, 2017.

  1. Woman In Love

    Woman In Love Lurker
    Thread Starter

    Hi,

    I'm trying to set height and width to Relative Layout Programmaticaly:

    final RelativeLayout Frame1 = (RelativeLayout) findViewById(R.id.Frame1);
    Frame1.getLayoutParams().width = X;
    Frame1.getLayoutParams().height = Y;

    Works on smartphone but crashes on Ipad.

    I tried also:
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams (RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

    Frame1.setLayoutParams(params);

    and again SetLayoutParams return null.

    Why is that?
    Thank you.
     



    1. Download the Forums for Android™ app!


      Download

       
  2. Deleted User

    Deleted User Guest

    Please post the stack trace from the Logcat output. This is required to advise on your problem. Thanks.
     
    scary alien likes this.
  3. Woman In Love

    Woman In Love Lurker
    Thread Starter

    Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.RelativeLayout.setLayoutParams(android.view.ViewGroup$LayoutParams)' on a null object reference
    at myapp.com.gridview.Main2Activity.onCreate(Main2Activity.java:237)
     
  4. Deleted User

    Deleted User Guest

    Can you include all the code, as it looks like Frame1 variable is null, but unclear why from the code you've shown.
     
    scary alien likes this.
  5. Woman In Love

    Woman In Love Lurker
    Thread Starter

    I only have Frame 1 declaration in my layout xml file:
    Code (Text):
    1.  
    2. <RelativeLayout
    3.     android:id="@+id/Frame1"
    4.     android:layout_width="wrap_content"
    5.     android:layout_height="wrap_content"
    6.     android:layout_gravity="center"
    7.     >
    8.  
    9.  
    10.     <ImageButton
    11.         android:layout_width="wrap_content"
    12.         android:layout_height="wrap_content"
    13.         android:id="@+id/lefti"
    14.         android:background="@drawable/lefti"
    15.         android:layout_alignParentLeft="true"
    16.         android:layout_alignParentStart="true" />
    17.  
    18.     <RelativeLayout
    19.         android:layout_width="wrap_content"
    20.         android:layout_height="4dp"
    21.         android:id="@+id/seperateline"
    22.         android:background="@color/color_primary"
    23.         android:layout_centerHorizontal="true"
    24.         android:layout_centerVertical="true"
    25.         android:layout_gravity="center">
    26.     </RelativeLayout>
    27.  
    28.     <ImageButton
    29.         android:layout_width="wrap_content"
    30.         android:layout_height="wrap_content"
    31.         android:id="@+id/righti"
    32.         android:background="@drawable/righti"
    33.         android:layout_alignParentRight="true"
    34.         android:layout_alignParentEnd="true" />
    35.  
    36. </RelativeLayout>
    37.  
    I need to assign height and width programmatically to Frame1 Layout.

    Thanks!
     
  6. Deleted User

    Deleted User Guest

    Need to see the Java code for the Activity which is trying to set the layout parameters.
     
    scary alien likes this.
  7. Woman In Love

    Woman In Love Lurker
    Thread Starter

    public class Main2Activity extends Activity {

    int Newgridseperatorframehight = 40;
    int Newgridseperatorframewidth = 580;

    @override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);

    final RelativeLayout Frame1 = (RelativeLayout) findViewById(R.id.Frame1);
    Frame1.getLayoutParams().height = Newgridseperatorframehight;
    Frame1.getLayoutParams().width = Newgridseperatorframewidth;
    Frame1.requestLayout();

    }
     
  8. Deleted User

    Deleted User Guest

    The fragment of stack trace you posted does not correspond to the above piece of code. Which line is producing the NullPointerException?
     
    scary alien likes this.
  9. Woman In Love

    Woman In Love Lurker
    Thread Starter

    yes, I had some changes...The line Frame1.getLayoutParams().height = Newgridseperatorframehight;
    because getLayoutParams returns NULL.
     
Loading...
Similar Threads - NullPointerException using getLayoutParams
  1. Marvin P Droid
    Replies:
    19
    Views:
    424
  2. Gobshyte
    Replies:
    1
    Views:
    239
  3. DridriLaBastos
    Replies:
    0
    Views:
    249
  4. npuleio
    Replies:
    0
    Views:
    466
  5. BabdevPro
    Replies:
    0
    Views:
    310
  6. Drchiefd113
    Replies:
    5
    Views:
    488
  7. Hada de la Luna
    Replies:
    1
    Views:
    471
  8. acenspades
    Replies:
    0
    Views:
    490
  9. mysorian
    Replies:
    0
    Views:
    434
  10. Hornetzero
    Replies:
    5
    Views:
    576

Share This Page

Loading...