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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T06:03:06+00:00 2026-06-07T06:03:06+00:00

Every time I go to add an Image view to my Linear Layout dynamically

  • 0

Every time I go to add an Image view to my Linear Layout dynamically I get a null pointer exception.

LinearLayout tables = (LinearLayout) findViewById(R.id.table);

        for(int i = 0; i < data.length; i++){
            ImageView image = new ImageView(getApplicationContext());
            try{
                int imgID = getResources().getIdentifier(data[i], "drawable", "package");
                image.setImageResource(imgID);

            }catch(Exception e){
                int imgID = getResources().getIdentifier("nia", "drawable", "package");
                image.setImageResource(imgID);
            }               
            tables.addView(image); //NULL POINTER THROWN HERE
        }

When I debug, the imgID has a value, so I know its working. I just don’t understand why its null if

  • 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-07T06:03:08+00:00Added an answer on June 7, 2026 at 6:03 am

    If this is the line that causes the Null Pointer Exception:

    tables.addView(image);
    

    Then tables is null, simply findViewById() did not find any View with the id R.id.table in the current layout being displayed.

    (If you want help figuring out why tables is null, please post the layout which you pass to setContentView())

    Added from comments

    Here is a general way to create a PopupWindow. This uses a LayoutInflator to inflate the layout so that we can access it to dynamically add elements to the layout. (Notice that we scope find to popupLayout.findViewById(...)):

    public class Example extends Activity {
        private PopupWindow popupWindow;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            TextView text = (TextView) findViewById(R.id.text);
            text.setOnClickListener(new OnClickListener() {
                public void onClick(View view) {
                    createPopup(view);
                }
            });
        }
    
        public void createPopup(View view) {
            LayoutInflater layoutInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);  
            View popupLayout = layoutInflater.inflate(R.layout.popup, null);  
            // Customize popup's layout here
    
            Button dismissButton = (Button) popupLayout.findViewById(R.id.dismiss);
            dismissButton.setOnClickListener(new OnClickListener() {
                public void onClick(View view) {
                    popupWindow.dismiss();
                }
            });
    
            popupWindow = new PopupWindow(popupLayout, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            popupWindow.showAtLocation(view, Gravity.CENTER, 0, 0);
        }
    }
    

    Understand that the root element in popup.xml should have a background attribute defined otherwise the window is transparent by default.

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

Sidebar

Related Questions

I am trying to add multiple images to Html5 canvas but every time I
It's a weird problem but every time I add a new document to Lucene.net
I need to set the color of the font every time I add text
I thought every time you do a flash[:notice]=Message it would add it to the
I am trying to add a new column into an SQLiteDatabase but every time
Currently after successful login I add md5($username) md5($password) in cookies and then every time
I am trying to make a button to add another input box every time
I have a UIImageView property in my view controller set to assign. Every time
I'm with some really weird problem here. Every time when I add a second
Every time i reset & seed my database it wipes out the standard admin@example.com

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.