Android runtime error when adding imageview to view-Collection of common programming errors
I’m having a runtime error when I’m adding an image to my main view/layout ( I tried with both ).
Code starts from an activity and I create the game board no problem, when it’s done the game start, all this goes well. The issue occurs when I press a button to add an image to the screen, I get a run time error at the moment the view is added into the main view. In my code I’m keeping everything Java only, I’m avoiding XML files, I’m wondering if that might be a part of the problem. I have been reading some solution to similar problem where people suggested to use the layout inflater method however I cannot find reference to a use of this method without having an XML file.
Here is my code
(PS sorry I see in the preview window how wierd it breaks at some moments… the error log is at the end
RelativeLayout GameView = new RelativeLayout(this);
GameView.setLayoutParams(new LayoutParams(
screenHei,
screenLen));
//Start Game sequence
ZoomView layout3 = new ZoomView(this);
layout3.setLayoutParams(new LayoutParams(
screenHei,
screenLen));
//Relative layout... aka base view that will be inserted in the zoom view
RelativeLayout baseBoard = new RelativeLayout(this);
//LinearLayOut Setup
baseBoard.setLayoutParams(new LayoutParams(
screenHei,
screenLen));
LinearLayout linearLayout= new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setLayoutParams(new LayoutParams(
screenHei,
screenLen));
//ImageView Setup
ImageView imageView = new ImageView(this);
//setting image resource
imageView.setImageResource(R.drawable.bluepage);
//setting image position
imageView.setLayoutParams(new LayoutParams(
screenHei,
screenLen));
//adding view to layout
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
linearLayout.addView(imageView);
//make visible to program
linearLayout.setX(0);
linearLayout.setY(0);
// RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
// RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.FILL_PARENT);
// lp.addRule(RelativeLayout.BELOW, v.getId());
baseBoard.addView(linearLayout);
layout3.setX(0);
layout3.setY(0);
final RelativeLayout menuMobile = new RelativeLayout(this);
menuMobile.setLayoutParams(new LayoutParams(
200,
200));
menuMobile.setBackgroundColor(0xFF000000 );
Button ButtonMobile1 = new Button(this);
ButtonMobile1.setBackgroundColor(0xFFCCFFCC);
ButtonMobile1.setText("test1");
ButtonMobile1.setTextSize(6);
ButtonMobile1.setLayoutParams(new LayoutParams(155,66));
ButtonMobile1.setX(100);
ButtonMobile1.setY(40);
menuMobile.addView(ButtonMobile1);
ButtonMobile1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.v("prepare to add barrack! "," testing Barrack ");
menuMobile.setVisibility(View.INVISIBLE);
@SuppressWarnings("rawtypes")
ArrayList aList = (ArrayList) GroundWork.get(selectedX);
GameTile axs = (GameTile) aList.get(selectedY);
axs.selected=1;
axs.createBuilding(1,1,1);
addPicture(axs.imgSizeIds);
Log.v("tile selected value and information "," axs.selected :" + axs.selected);
}
});
valueX=21;
valueY=12;
//Base map builder
for(int l=0; l