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 6979391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:52:53+00:00 2026-05-27T17:52:53+00:00

import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.ViewGroup.LayoutParams; import android.widget.Button; import android.widget.RelativeLayout; public class

  • 0
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.RelativeLayout;

public class register_activity extends Activity {


@Override
public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register);

        RelativeLayout RLayout = (RelativeLayout) View.inflate(this, R.layout.register, null);

        RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.FILL_PARENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT
        );

        Button btnCreateNew = new Button(this);
        btnCreateNew.setText("Create New User");
        btnCreateNew.offsetTopAndBottom(10);
        btnCreateNew.offsetLeftAndRight(10);
        RLayout.addView(btnCreateNew, p);

    }
}

So that code works and runs just fine, only I can not see the button being displayed. The layout is simply blank with nothing inside it.

What is wrong?

  • 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-05-27T17:52:53+00:00Added an answer on May 27, 2026 at 5:52 pm

    You got a NullPointerException, right? (Check your LogCat!)

    The reason:

    1. You create a Button as a member variable with this as a parameter. That might cause some trouble as this might not be defined when new Button(this) is called. Move the initialization into onCreate

    2. Your RLayout will be null, and here I am very sure. The reason is, that you can’t call findViewById() before you called setContentView(). If you call it before, Android has no idea where to look at and returns null.

    update

    As you changed your question quite a bit, here is my updated answer:

    You set your content to R.layout.register and after that you inflate it again.

    My solution for you: just use setContentView(R.layout.register), than use findViewById(R.id.layout_id) and finally create and add your button:

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register);
    
        RelativeLayout RLayout = (RelativeLayout) findViewById(R.id.layout_id);
    
        RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.FILL_PARENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT
        );
    
        Button btnCreateNew = new Button(this);
        btnCreateNew.setText("Create New User");
        btnCreateNew.offsetTopAndBottom(10);
        btnCreateNew.offsetLeftAndRight(10);
        RLayout.addView(btnCreateNew, p);
    }
    

    Of course you can also add the button in the xml layout directly. I would prefer this way, because you have a better separation between layout and code.

    Basic XML structure (style it to as you want):

    <RelativeLayout android:id="@+id/layout_id"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
        <Button android:id="@+id/button_id"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:text="@string/button_text"/>
    </RelativeLayout>
    

    The reason for you blank screen was, that you added the button to the new created RLayout but this layout was never part of your screen (never added by setContentView())

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

Sidebar

Related Questions

package com.example.android.home; import android.app.Activity; import android.os.Bundle; import android.widget.*; import android.view.*; public class HomeActivity extends
package com.example.helloandroid; import android.app.Activity; import android.os.Bundle; import android.widget.Button; import android.view.View; import android.view.View.OnClickListener; //Create an
package one.two; import android.app.Activity; import android.database.Cursor; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.SimpleCursorAdapter;
package woot.wat.wen; import android.app.Activity; import android.os.Bundle; import android.text.Layout; import android.util.Log; import android.view.KeyEvent; import android.view.View;
Here is my code import org.ksoap2.*; import org.ksoap2.serialization.*; import org.ksoap2.transport.*; import android.app.Activity; import android.os.Bundle;
import java.util.ArrayList; import android.app.Activity; import android.content.Context; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.Path; import android.os.Bundle;
I've modified my previous code for login. package log1.log2; import android.app.Activity; import android.content.Intent; import
package com.VRG; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import android.app.Activity; import android.content.Context; import android.media.MediaPlayer;
The following code works fine on Android 2.1update1 - package com.troubadorian.android.teststreaming; import android.app.Activity; import
import java.lang.Math; public class NewtonIteration { public static void main(String[] args) { System.out.print(rootNofX(2,9)); }

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.