• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

NullPointerException when using getLayoutParams

Woman In Love

Lurker
Apr 17, 2017
7
0
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.
 
I only have Frame 1 declaration in my layout xml file:
Code:
<RelativeLayout
    android:id="@+id/Frame1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    >


    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/lefti"
        android:background="@drawable/lefti"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="4dp"
        android:id="@+id/seperateline"
        android:background="@color/color_primary"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:layout_gravity="center">
    </RelativeLayout>

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/righti"
        android:background="@drawable/righti"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

</RelativeLayout>

I need to assign height and width programmatically to Frame1 Layout.

Thanks!
 
Upvote 0
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();

}
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones