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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:36:57+00:00 2026-05-27T23:36:57+00:00

I want this relative layout to eventually load an ad at the bottom. public

  • 0

I want this relative layout to eventually load an ad at the bottom.

public class Main extends Activity {
RelativeLayout mLayout;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);        
    setContentView(R.layout.main);
    mLayout = (RelativeLayout) findViewById(R.layout.main);  

    ImageView View = new ImageView(this, null);      //example view

    mLayout.addView(View);

Then the Null Pointer occurs below when mLayout is called

 RelativeLayout.LayoutParams rparams = (LayoutParams) mLayout.getLayoutParams();

E/AndroidRuntime(4066): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.name.project/com.name.project.Main}: java.lang.NullPointerException

xml for the layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/lib/com.google.ads"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center" >

Any idea why this is happening here?

  • 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-27T23:36:58+00:00Added an answer on May 27, 2026 at 11:36 pm

    The error occurs in this line:

    mLayout = (RelativeLayout) findViewById(R.layout.main);
    

    It’s pretty obvious by the name of the findViewById method – it’s used to find a view by its ID, but you’re trying to find a view using a layout reference, not ID reference. Try with the following:

    mLayout = (RelativeLayout) findViewById(R.id.main);
    

    Also, your RelativeLayout should include the android:id tag. Try using the following XML for your RelativeLayout:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/lib/com.google.ads"
    android:id="@+id/main"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center" >
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Linear layout below. This layout is aligned parent bottom in a Relative Layout. Problem
I have this div: <div class=inauguration-image> I do not want this text to display,
i have one main relative layout in that i have buttons and in middle
I want to make my xml layout more modular because this one is going
I've got relative layout in my view hierarchy and I want it to have
I want to center a text inside a TextView relative to the parent layout
I want to hide a relative layout completely. i have a view with several
I have a ViewFlipper in which I want to add Views (Relative Layout with
I want to draw ImageView with text over a relative layout with 9patch background.
I have requirement for my project. I want the my Relative layout to slide

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.