NullPointerException when using getLayoutParams

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.
 

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

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.
 

Woman In Love

Lurker
Thread starter
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!
 

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();

}
 
D

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?
 

Woman In Love

Lurker
Thread starter
yes, I had some changes...The line Frame1.getLayoutParams().height = Newgridseperatorframehight;
because getLayoutParams returns NULL.
 
Top