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

  • Home
  • SEARCH
  • 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 8840053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:22:39+00:00 2026-06-14T10:22:39+00:00

I have a RelativeLayout completely created within java, not using xml layout. I have

  • 0

I have a RelativeLayout completely created within java, not using xml layout.
I have a few buttons on the left if my screen. I want to click on one button and show a default image from my res/drawable next to the button and make it disappear again on second click.
What I have tried was toggling the visibility but my onClick() raises a FATAL EXCEPTION, NullPointer Exception.

This is my code so far. Hardcoded the image shows right when I set picview.setVisibility(View.INVISIBLE); by hand. What I am doing wrong in the onClick() ?

    private ImageView picview;

        //*snip* loads of other code

    //Show Image Button
    ImageButton show_pic = new ImageButton(this);
    show_pic.setBackgroundColor(Color.WHITE);
    show_pic.setOnClickListener(new OnClickListener()
    {

        @Override
        public void onClick(View v) {
            if(picview.getVisibility() == View.INVISIBLE)
             {
             picview.setVisibility(View.VISIBLE);
             } 
            else if (picview.getVisibility() == View.VISIBLE)
             {
             picview.setVisibility(View.INVISIBLE);
             }          
            }

    });

    params = new RelativeLayout.LayoutParams(40, 40);
    params.topMargin = 10;
    params.leftMargin = 10;
    params.addRule(RelativeLayout.BELOW, button2_id);
    rl.addView(show_pic, params);

    //Imageview loaded from drawable
    ImageView picview = new ImageView(this);
    params = new RelativeLayout.LayoutParams(200, 400);
    params.topMargin = 0;
    params.leftMargin = 30;
    params.addRule(RelativeLayout.RIGHT_OF, button2_id);
    picview.setImageResource(R.drawable.my_image);
    picview.setVisibility(View.INVISIBLE);
    rl.addView(picview, params);

    this.setContentView(rl);
  • 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-14T10:22:40+00:00Added an answer on June 14, 2026 at 10:22 am

    You are accidentally creating two copies of picview. Shorten this line:

    ImageView picview = new ImageView(this);
    

    To:

    picview = new ImageView(this);
    

    (Your field variable private ImageView picview; never changed from null, so when you clicked your Button you see the NullPointerException…)

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

Sidebar

Related Questions

I have a layout with a listView <?xml version=1.0 encoding=utf-8?> <RelativeLayout xmlns:android=http://schemas.android.com/apk/res/android android:id=@+id/testMain android:layout_width=fill_parent
I have a RelativeLayout defined in an xml layout file named 'myview.xml' which contains
I want to hide a relative layout completely. i have a view with several
I have a RelativeLayout containing a pair of side-by-side buttons, which I want to
I have a relativelayout within a scrollview with 12 buttons. On each button clicked
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 a relativelayout with 30 buttons within a scrollview. What I would like
I have a RelativeLayout with one child as <include another.xml> like this root.xml :
In my foo_layout.xml file I have a subclassed RelativeLayout: <?xml version=1.0 encoding=utf-8?> <FrameLayout xmlns:android=http://schemas.android.com/apk/res/android
I have an ImageButton that I want to fill its parent RelativeLayout container for

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.