2

I have a layout which is designed by XML. Then, I want to add a button by Java programming. No matter how I played with the marginparams, layoutparams, I always get an error when I do the addview.

What am I doing wrong? What is missing?

Also, I am getting a warning that RelativeLayout.LayoutParams.FILL_PARENT within MarginLayoutParams is deprecated. So what is the other way to do this? I am trying to put the new button in a specific position on a relative layout.

Thanks, AJ

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Button btn = new Button(this);
    btn.setText("Go!");
    btn.setOnClickListener(new Button.OnClickListener() {
        public void onClick(View v) {
            number_of_cards=0;
        }
    });       


    RelativeLayout ll = (RelativeLayout) findViewById(R.layout.activity_main);

    //ll.addView(btn);
    //setContentView(ll);

    float density = getApplicationContext().getResources().getDisplayMetrics().density;
    MarginLayoutParams marginParams;
    RelativeLayout.LayoutParams layoutParams;

    int px_left = Math.round((float) (30+(4-1)*75) * density);
    int px_top = Math.round((float) (30+(1-1)*91) * density);
    marginParams = new MarginLayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,RelativeLayout.LayoutParams.FILL_PARENT);
    marginParams.setMargins(px_left, px_top, 0, 0);
    layoutParams = new RelativeLayout.LayoutParams(marginParams);
    btn.setLayoutParams(layoutParams);              
    ll.addView(btn, layoutParams); // getting error here
    setContentView(R.layout.activity_main);

The LogCat is:

12-07 10:57:03.534: E/AndroidRuntime(850): FATAL EXCEPTION: main

12-07 10:57:03.534: E/AndroidRuntime(850): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.first/com.example.first.First}: java.lang.NullPointerException

12-07 10:57:03.534: E/AndroidRuntime(850):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)

12-07 10:57:03.534: E/AndroidRuntime(850):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)

12-07 10:57:03.534: E/AndroidRuntime(850):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)

12-07 10:57:03.534: E/AndroidRuntime(850):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)

12-07 10:57:03.534: E/AndroidRuntime(850):  at android.os.Handler.dispatchMessage(Handler.java:99)

12-07 10:57:03.534: E/AndroidRuntime(850):  at android.os.Looper.loop(Looper.java:123)

12-07 10:57:03.534: E/AndroidRuntime(850):  at android.app.ActivityThread.main(ActivityThread.java:3683)

12-07 10:57:03.534: E/AndroidRuntime(850):  at java.lang.reflect.Method.invokeNative(Native Method)

12-07 10:57:03.534: E/AndroidRuntime(850):  at java.lang.reflect.Method.invoke(Method.java:507)

12-07 10:57:03.534: E/AndroidRuntime(850):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)

12-07 10:57:03.534: E/AndroidRuntime(850):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)

12-07 10:57:03.534: E/AndroidRuntime(850):  at dalvik.system.NativeStart.main(Native Method)

12-07 10:57:03.534: E/AndroidRuntime(850): Caused by: java.lang.NullPointerException

12-07 10:57:03.534: E/AndroidRuntime(850):  at com.example.first.First.onCreate(First.java:127)

12-07 10:57:03.534: E/AndroidRuntime(850):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)

12-07 10:57:03.534: E/AndroidRuntime(850):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)

12-07 10:57:03.534: E/AndroidRuntime(850):  ... 11 more

My activity_main xml is below:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/textScore"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="30dp"
    android:layout_marginTop="5dp"
    android:text="@string/text_score" />

</RelativeLayout>
3
  • 2
    Please show some logcat for the error you're getting Commented Dec 7, 2012 at 10:30
  • Hi Stupidus. It gives: NullPointerException. Commented Dec 7, 2012 at 10:52
  • Edit my question with the LogCat Commented Dec 7, 2012 at 11:05

3 Answers 3

3

It gives you NullPointerException

Solution is:- Write below line

setContentView(R.layout.activity_main);

after

super.onCreate(savedInstanceState);

And write below line

RelativeLayout ll = (RelativeLayout) findViewById(R.id.mRlayout1);

instead of

RelativeLayout ll = (RelativeLayout) findViewById(R.layout.activity_main);

And Update your xml using below code.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/mRlayout1">

<TextView
    android:id="@+id/textScore"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="30dp"
    android:layout_marginTop="5dp"
    android:text="@string/text_score" />

</RelativeLayout>

and remove from last, it will solve your problem.

Sign up to request clarification or add additional context in comments.

3 Comments

Dipak Keshariya, it doesn't solve the problem. :-( I still get NullPointerException...
Dipak, how do I get the layout id? it doesn't appear in the R.id. autocompletion list...
@AJGottes See my updated answer and also update this line RelativeLayout ll = (RelativeLayout) findViewById(R.id.mRlayout1);
1

You need to call setContentView before calling findViewById.. and to set the margin check below..

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

Button btn = new Button(this);
btn.setText("Go!");
btn.setOnClickListener(new Button.OnClickListener() {
    public void onClick(View v) {
        number_of_cards=0;
    }
});       


RelativeLayout ll = (RelativeLayout) findViewById(R.layout.activity_main);

//ll.addView(btn);
//setContentView(ll);

float density = getApplicationContext().getResources().getDisplayMetrics().density;
MarginLayoutParams marginParams;
RelativeLayout.LayoutParams layoutParams;

int px_left = Math.round((float) (30+(4-1)*75) * density);
int px_top = Math.round((float) (30+(1-1)*91) * density);

layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,RelativeLayout.LayoutParams.FILL_PARENT);
layoutParams.setMargins(px_left, px_top, 0, 0);
btn.setLayoutParams(layoutParams);              
ll.addView(btn, layoutParams); // getting error here

Comments

0

Try removing this line... btn.setLayoutParams(layoutParams);

As you are providing the layout parameters in ll.addView(btn, layoutParams);

Regards,\n Suman

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.