Jeeter
Jeeter

Reputation: 6085

Android - LinearLayout not adding custom view

NOTICE: THIS QUESTION HAS BEEN RESOLVED DUE TO ME CATCHING MY OWN STUPIDITY

I want to add a custom class to a LinearLayout, but for some reason I keep getting a NullPointerException.

Here is the method that deals with the addition:

protected void onPostExecute(String results) {
        System.out.println("ON POST EXECUTE : " + results);
        try {
            if(!results.equals(((MessageBlurb)container.getChildAt(0)).getMessage())){
                try {
                    container.removeViewAt(30);
                    for (int i = 29; i > 0; i--) {
                        container.addView(container.getChildAt(i-1), i);
                        container.removeViewAt(i-1);
                    }
                    container.addView(new MessageBlurb(getApplicationContext(), results, Color.BLACK), 0);
                } catch (NullPointerException e) {
                    // TODO: handle exception
                }
            }
        }
        catch (Exception e) {
            MessageBlurb mb = new MessageBlurb(getApplicationContext(), results, Color.BLACK);
            mb.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
            System.out.println(mb);
            container.addView(mb, 0);
        }


    }

where MessageBlurb extends ViewGroup, because I have a TextView inside the MessageBlurb.

The MessageBlurb class looks like this:

public MessageBlurb(Context context, String message, int color){
    super(context);
    myTV = new TextView(context);
    this.addView(myTV);
    myTV.setText(message);
    System.out.println("THE BLURB IS CREATED");
    this.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            System.out.println("YOU CLIKED THE BLURB");

        }
    });

}

I printed out the description of mb, and it gives me a memory location. As well as that, the logcat error points to this line:

container.addView(mb, 0);

The container itself is a LinearLayout defined in the activity_main.xml file. It is initialized through the line of code:

container = (LinearLayout)findViewById(R.id.container);

The id of the Layout in the xml file is also called container

Can someone see what I'm doing wrong? Thanks!

Upvotes: 0

Views: 671

Answers (2)

Jeeter
Jeeter

Reputation: 6085

It turns out that I was being a complete idiot. I looked through my onCreate() and found that I had commented out the line: setContentView(...)

Even so, I want to thank everyone who replied to this thread. Sorry for all the trouble! :)

Upvotes: 0

Alex MDC
Alex MDC

Reputation: 2456

I'm not 100% sure if this is where the problem is, but the following code looks like it's just asking for trouble:

                container.removeViewAt(30);
                for (int i = 29; i > 0; i--) {
                    container.addView(container.getChildAt(i-1), i);
                    container.removeViewAt(i-1);
                }
                container.addView(new MessageBlurb(getApplicationContext(), results, Color.BLACK), 0);

It looks like you are trying to remove the last view in the container and add a new MessageBlurb at the top. But you don't need to shift all the views down manually like that, simply adding a new view at position 0 will do all that for you. Try replacing it with this:

                container.removeViewAt(30);
                container.addView(new MessageBlurb(getApplicationContext(), results, Color.BLACK), 0);

Also, do you have a good reason for using getApplicationContext() when creating the message blurb? Your code looks like its part of an AsyncTask class nested inside an Activity class, so you can and should pass the activity itself as the context (i.e. new MessageBlurb(Activity.this, results, Color.BLACK) or something similar).

Hope that helps!

Upvotes: 2

Related Questions