views:

54

answers:

0

Hi,

I am trying to embed an xml layout resource onto a custom view created in code.

So for instance I have an xml file called about.xml

i was trying the inflate method, but i am getting a NULL POINTER EXCEPTION CALL and kills my app.

in my onCreate method, i am calling

    linearOut = new LinearLayout(this);
    linear = new LinearLayout(this);
    linear.setOrientation(LinearLayout.VERTICAL);           

    scroll = new ScrollView(this);
    linearOut.addView(scroll);
    scroll.addView(linear);

    text=new TextView(this);
    text.setHeight(250);
    text.setWidth(320);
    text.setBackgroundColor(Color.argb(255,255,255,255));
    text.setTextColor(-16777216); // BLACK
    text.setText("Lorem ipsum dolor sit amet, consectetur adipiscing elit. " +
            "Nulla eu magna lorem, at posuere elit. Cum sociis natoque " +
            "penatibus et magnis dis parturient montes, nascetur ridiculus " +
            "mus. Donec accumsan, tortor dictum sodales euismod, erat felis " +
            "molestie dui, tincidunt sagittis diam est quis quam. Maecenas " +
            "pulvinar risus at turpis vestibulum vitae interdum ante facilisis. " +
            "Duis sed justo sodales elit consequat varius pharetra vel purus. " +
            "Phasellus sem ipsum, placerat a cursus a, pellentesque eu turpis. " +
            "In posuere fermentum dui, quis sagittis tortor congue sed. " +
            "Vestibulum accumsan purus et nulla aliquet tristique. " +
            "arcu ac suscipit ullamcorper, ipsum nunc congue massa.");

    // lets add more Object View

    View itemView = inflater.inflate(R.layout.about, linear); // this is the layout
    linear.addView(itemView);
    linear.addView(text);

    setContentView(linearOut);

Stack Trace:

   09-27 14:23:53.947: ERROR/AndroidRuntime(1672): FATAL EXCEPTION: main
   09-27 14:23:53.947: ERROR/AndroidRuntime(1672): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.adunit.android/com.adunit.android.BannerIDs}: java.lang.NullPointerException
   09-27 14:23:53.947: ERROR/AndroidRuntime(1672):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
   09-27 14:23:53.947: ERROR/AndroidRuntime(1672):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
   09-27 14:23:53.947: ERROR/AndroidRuntime(1672):     at 

I know i am doing this all wrong, but is there a way by example can fix my problem.

Thanks again in advance

  • Rob