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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:25:49+00:00 2026-05-23T13:25:49+00:00

I have a relative layout with three buttons in it and I am trying

  • 0

I have a relative layout with three buttons in it and I am trying to change the background of the relative layout dynamically. Here’s the xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="@drawable/mbi2"
            android:id="@+id/rlMain"
            android:layout_centerHorizontal="true">
    <Button android:text="Compose" 
            android:id="@+id/btnCompose" 
            android:layout_height="wrap_content"  
            android:layout_width="wrap_content"/>
    <Button android:text="Messages" 
            android:id="@+id/btnMessages" 
            android:layout_height="wrap_content" 
            android:layout_width="wrap_content" 
            android:layout_toRightOf="@id/btnCompose"/>
    <Button android:text="More &gt;&gt;" 
            android:id="@+id/btnMore" 
            android:layout_height="wrap_content" 
            android:layout_width="wrap_content" 
            android:layout_toRightOf="@id/btnMessages"/>                    
            </RelativeLayout>

Here’s the java code which is giving me an error

            RelativeLayout llMain=null;
    try {
        llMain = (RelativeLayout) findViewById(R.id.rlMain);
        Resources res = getResources();
        Drawable drawMbi = res.getDrawable(drawBgId);
        llMain.setBackgroundDrawable(drawMbi);          
    } catch (Exception e) {
        e.printStackTrace();
    }

The error is

06-29 23:56:41.099: WARN/System.err(8487): java.lang.NullPointerException
06-29 23:56:41.099: WARN/System.err(8487):     at com.me2youmob.cwrap.ChickenWrapActivity.loadMBIIntoView(ChickenWrapActivity.java:65)
06-29 23:56:41.099: WARN/System.err(8487):     at com.me2youmob.cwrap.ChickenWrapActivity.onCreate(ChickenWrapActivity.java:23)
06-29 23:56:41.099: WARN/System.err(8487):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1066)
06-29 23:56:41.099: WARN/System.err(8487):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2797)
06-29 23:56:41.108: WARN/System.err(8487):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2854)
06-29 23:56:41.108: WARN/System.err(8487):     at android.app.ActivityThread.access$2300(ActivityThread.java:136)
06-29 23:56:41.118: WARN/System.err(8487):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2179)
06-29 23:56:41.118: WARN/System.err(8487):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-29 23:56:41.118: WARN/System.err(8487):     at android.os.Looper.loop(Looper.java:143)
06-29 23:56:41.118: WARN/System.err(8487):     at android.app.ActivityThread.main(ActivityThread.java:5068)
06-29 23:56:41.118: WARN/System.err(8487):     at java.lang.reflect.Method.invokeNative(Native Method)
06-29 23:56:41.118: WARN/System.err(8487):     at java.lang.reflect.Method.invoke(Method.java:521)
06-29 23:56:41.118: WARN/System.err(8487):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
06-29 23:56:41.118: WARN/System.err(8487):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
06-29 23:56:41.118: WARN/System.err(8487):     at dalvik.system.NativeStart.main(Native Method)

Another thing in this issue is that earlier I had the buttons within a linear layout which was then within a Relative Layout. I thought it was not able to reach the relative layout and remove the linear layout section. It still gives me the same error.

Any idea what is going on here ?

NEW UPDATE:

    @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    RelativeLayout rlM = (RelativeLayout) findViewById(R.id.rlMain);
    if (rlM == null)
    {
        Log.d("NULL CHECK","Layout is null");
    }
    else
    {
        Log.d("NULL CHECK","Layout is not null");
    }
    //spPreferences = getSharedPreferences(PREFS_NAME, 0);
    //loadMBIIntoView();
    //handleButtonClicks();
}
  • 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-23T13:25:49+00:00Added an answer on May 23, 2026 at 1:25 pm

    The View.findViewById() function will only find views that are children of the current view you are in.

    In this case, you’re trying to obtain the main view either within one of the children views, or you have not used setContentView() within the Activity class yet.

    If you are trying to set your background, I would highly suggest doing so from the Activity.

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

Sidebar

Related Questions

I have designed the screen using this relative layout. <?xml version=1.0 encoding=utf-8?> <RelativeLayout xmlns:android=http://schemas.android.com/apk/res/android
Why does my relative layout occupy full screen width <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
i have used the following xml code to create the layout. <?xml version=1.0 encoding=utf-8?>
I have a relative layout defined in a xml file(say realtive.xml). In another layout
I have a Relative Layout. Which has 2 buttons, side by side and it
i have a relative layout and i have two buttons in it with texts
In my App I have 7 buttons aligned using relative layout. The order I
I have imageView inside a relative layout. And when i create the activity i
I have this layout that works correctly, a relative layout with a text view
I have a ImageView in my relative layout. How can I get all the

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.