Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8949839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:18:43+00:00 2026-06-15T13:18:43+00:00

I have a layout which is designed by XML. Then, I want to add

  • 0

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>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T13:18:44+00:00Added an answer on June 15, 2026 at 1:18 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a layout which I am inflating in order to dynamically add TableRows
I have an xml layout which I use for each row in my listview:
I have a layout like this in which I want to display a label
I have designed an alertdialog of which when the user press the save button,
Just recently, my Android XML files which define my layout and etc have stopped
I have layout which has a button. In the graphical layout editor, the button
I have a XML layout which has two edit text fields, one for title
I have a layout which looks like this: <LinearLayout> <ImageView> android:width=wrap_content android:height:wrap_content ... </ImageView>
I have a layout setup which can be view here: http://jsfiddle.net/Pn3ts/ It all works
Help needed.. I have a organization layout which have rectangular shapes not in an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.