Java – An error occurred while creating a relative layout using Java code

An error occurred while creating a relative layout using Java code… here is a solution to the problem.

An error occurred while creating a relative layout using Java code

I’m new to android development and am trying to create a RelativeLayout using Java code.
Here’s my MainActivty .java

package com.rt.droid.rellayoutjava;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.RelativeLayout.LayoutParams;

public class MainActivity extends Activity {
RelativeLayout r;
public static LayoutParams msgDim;
EditText userNameVal, passwordVal;
TextView message, userName, password;
Button login;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    init();
    createMessage();
    r.addView(message,msgDim);
    setContentView(r);
}

public void init() {
    r = new RelativeLayout(this);
    LayoutParams dimensions = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
    r.setLayoutParams(dimensions);
    userNameVal = new EditText(this);
    passwordVal = new EditText(this);
    message=new TextView(this);
    userName = new TextView(this);
    password = new TextView(this);
    login = new Button(this);
}
public void createMessage(){
    LayoutParams msgDim = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    msgDim.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
    message.setLayoutParams(msgDim);
    message.setText("Please login first");
    message.setTextColor(Color.rgb(0,0,0));

}

}

But I get a null pointer exception (from logcat below).

 Process: com.rt.droid.rellayoutjava, PID: 2471
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.rt.droid.rellayoutjava/com.rt.droid.rellayoutjava.MainActivity}: java.lang.NullPointerException: Attempt to read from field 'int android.view.ViewGroup$LayoutParams.width' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
        at android.app.ActivityThread.access$800(ActivityThread.java:144)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5221)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
 Caused by: java.lang.NullPointerException: Attempt to read from field 'int android.view.ViewGroup$LayoutParams.width' on a null object reference
        at android.view.ViewGroup$LayoutParams.<init>(ViewGroup.java:6403)
        at android.view.ViewGroup$MarginLayoutParams.<init>(ViewGroup.java:6685)
        at android.widget.RelativeLayout$LayoutParams.<init>(RelativeLayout.java:1345)
        at android.widget.RelativeLayout.generateLayoutParams(RelativeLayout.java:1104)
        at android.view.ViewGroup.addViewInner(ViewGroup.java:3889)
        at android.view.ViewGroup.addView(ViewGroup.java:3733)
        at android.view.ViewGroup.addView(ViewGroup.java:3709)
        at com.rt.droid.rellayoutjava.MainActivity.onCreate(MainActivity.java:24)
        at android.app.Activity.performCreate(Activity.java:5933)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

Any guidance would be appreciated.

Solution

First, I recommend that you use XML files to define your layouts instead of writing them programmatically (you can search for any type of sample and tutorial).

Anyway, your specific problem is related to the uninitialized variable msgDim. Change your onCreate() to:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    init();
    createMessage();
    msgDim = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    r.addView(message,msgDim);
    setContentView(r);
}

Related Problems and Solutions