What does the XML for your res/layout/main.xml look like? Thanks, Justin
---------------------------------------------------------------------- There are only 10 types of people in the world... Those who know binary and those who don't. ---------------------------------------------------------------------- On Mon, Oct 12, 2009 at 9:28 AM, Sven <koff...@gmail.com> wrote: > > Hi, > > > I want to add an ImageView to my layout when a button has been > pressed. Sadly, I cannot get a handle to the main application's > linearlayout, I always get a null pointer error when trying to > retrieve the layout using findViewByID. > > Here's the code that ought to display the image when button b has been > pressed, but I think I have missed something obious: > > package com.foo.bar; > > import android.app.*; > import android.os.Bundle; > import android.view.*; > import android.view.ViewGroup.LayoutParams; > import android.widget.*; > import android.content.*; > import android.util.Log; > > > public class AndroidTest extends Activity { > /** Called when the activity is first created. */ > @Override > public void onCreate(Bundle savedInstanceState) { > super.onCreate(savedInstanceState); > setContentView(R.layout.main); > > // assign flag.png to the button, loading correct flag image for > current locale > Button b; > b = (Button)findViewById > (R.id.flag_button) ;//.setBackgroundDrawable(this.getResources > ().getDrawable(R.drawable.developers)); > > // build dialog box to display when user clicks the flag > AlertDialog.Builder builder = new AlertDialog.Builder(this); > builder.setMessage(R.string.dialog_text) > .setCancelable(false) > .setTitle(R.string.dialog_title) > .setPositiveButton("Done", new > DialogInterface.OnClickListener() { > public void onClick(DialogInterface dialog, int id) { > dialog.dismiss(); > } > }); > final AlertDialog alert = builder.create(); > > // set click listener on the flag to show the dialog box > b.setOnClickListener(new View.OnClickListener() { > public void onClick(View v) { > // alert.show(); > ImageView image; > image = new ImageView(v.getContext()); > image.setImageDrawable(v.getResources().getDrawable > (R.drawable.developers)); > > > LinearLayout main = (LinearLayout) > AndroidTest.this.findViewById(R.layout.main); > // some debug code > if (main == null) > { > Log.v("abc", "Main is null"); > } > else > { > main.addView(image, 200, 200); > } > } > }); > > } > } > > > > --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Android Beginners" group. To post to this group, send email to android-beginners@googlegroups.com To unsubscribe from this group, send email to android-beginners-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-beginners?hl=en -~----------~----~----~----~------~----~------~--~---