3

i am new to android development and was trying to create RelativeLayout using Java code. Below is 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));

}

}

However i get the null pointer exception(below from Logcat)

 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 pointers would be appreciated.

rapidclock
  • 1,677
  • 2
  • 17
  • 32

2 Answers2

5

First of all, I suggest you to use XML files to define your layout, instead of writing them programmatically (you can search for any kind of example and tutorial).

Anyway, your specific problem is related to the variable msgDim which is not initialized. Change your onCreate() to this:

@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);
}
bonnyz
  • 13,458
  • 5
  • 46
  • 70
  • Thanks for the reply. First off i know xml is a better way to define layout but this was just my attempt at learning how to do the same programatically. Your reply pointed me to the answer, which is that i was trying to create another msgDim variable inside CreateMessage() even though i had already declared it at the beginning of my program(global var). Thanks a lot! These type of errors are frustrating for beginners :) – rapidclock Jan 08 '15 at 18:22
0

Try using this:

r = new RelativeLayout(this);
RelativeLayout.LayoutParams dimensions = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, 
                                                                         RelativeLayout.LayoutParams.MATCH_PARENT);
r.setLayoutParams(dimensions);

The first answer under this question has more complete code: Programatically creating a RelativeLayout in Android

Community
  • 1
  • 1
Ero
  • 491
  • 1
  • 3
  • 15