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 [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-beginners?hl=en
-~----------~----~----~----~------~----~------~--~---