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

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.
 
Please post the stack trace from the Logcat output. This is required to advise on your problem. Thanks.
 
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)
 
Can you include all the code, as it looks like Frame1 variable is null, but unclear why from the code you've shown.
 
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!
 
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();

}
 
The fragment of stack trace you posted does not correspond to the above piece of code. Which line is producing the NullPointerException?
 
yes, I had some changes...The line Frame1.getLayoutParams().height = Newgridseperatorframehight;
because getLayoutParams returns NULL.
 
Back
Top Bottom