Hartley
Hartley

Reputation: 69

Dynamically add imageView to layout

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

Upvotes: 0

Views: 10363

Answers (2)

Sam
Sam

Reputation: 86948

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.

Upvotes: 3

Darry Morales
Darry Morales

Reputation: 171

to check if there is a problem with your XML layout you can try to define the layout programmatically

        LinearLayout tables = new LinearLayout(getApplicationContext());

        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); 
        }

And adds this view like a ContentView

        setContentView(tables);

Upvotes: 3

Related Questions