I want to show more than two relative layouts in a viewFlipper, but running the following code throws an exception
java.lang.IllegalStateException: The specified child already has a
parent. You must call removeView() on the child's parent first.
Code:
for (int i = 0; i < imageid.length; i++)
{
RelativeLayout image = new RelativeLayout(MainActivity.this);
// image.addView(rrl3);
// image.addView(rrl4);
image.setBackgroundResource(imageid[i]);
vflp.addView(rrl3);
vflp.addView(image, new LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
}
Related
I'm trying to add multiple views to RelativeLayout that I declared inside xml. Looping is the only way because I do not know how many elements needed to add there, since the size is dynamic. But I got this error.
java.lang.IllegalStateException: The specified child already has a
parent. You must call removeView() on the child's parent first.
Code:
View inf = getActivity().getLayoutInflater().inflate(R.layout.item_table_edit, null);
RelativeLayout.LayoutParams layoutParamss = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
for (int i = 0; i < putzones.size(); i++) {
inf.setX((float) putzones.get(i).getPosX());
inf.setY((float) putzones.get(i).getPosY());
addMap.addView(inf, layoutParamss);
}
What is wrong here, since when I'm trying to add views by clicking items it works with the same code, without any error. code is same when I am adding by clicking item without loop. Thanks in advance.
You have to create a new instance of item_table_edit at each loop's iteration.
RelativeLayout.LayoutParams layoutParamss = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
for (int i = 0; i < putzones.size(); i++) {
View inf = getActivity().getLayoutInflater().inflate(R.layout.item_table_edit, null);
inf.setX((float) putzones.get(i).getPosX());
inf.setY((float) putzones.get(i).getPosY());
addMap.addView(inf, layoutParamss);
}
I am trying to create nested linearlayouts dynamically and setting it to activity layout as
setContentView(createLayout()); in oncreate().
But I am getting nothing on the screen but a blank screen. Can someone help in pointing out if I am doing it in wrong way?
private LinearLayout createLayout() {
Log.d(TAG,"calling cretaelayout");
LinearLayout main = new LinearLayout(getApplicationContext());
main.setOrientation(LinearLayout.VERTICAL);
int k =0;
for(int i=0 ;i < MainActivity.height*10;i++) {
LinearLayout row = new LinearLayout(getApplicationContext());
row.setOrientation(LinearLayout.HORIZONTAL);
for(int j=0;j< MainActivity.width*10;j++)
{
Log.d(TAG,"creating layout element");
LinearLayout ll = new LinearLayout(getApplicationContext());
ll.setBackgroundColor(Color.BLACK);
ll.setId( k++);
ll.setOrientation(LinearLayout.VERTICAL);
ll.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT));
ll.setOnClickListener(myhandler);
row.addView(ll);
}
main.addView(row);
}
return main;
}
There's nothing anywhere that takes space. You have a LL inside an LL inside an LL, with the last LL set to wrap_content. But it has no content inside it, so its size will be 0 in both directions. Elements with no size don't appear. Try making the inner most LLs fixed size, and you should see something.
For creating a structure like the image in android, I tried the following code.
But showing this error. Please help
My code looks like
public void setValues(){
LinearLayout table=(LinearLayout)findViewById(R.id.mainWrap);
LinearLayout row=new LinearLayout(this);;
for(int i=0;i<5;i++){
if(i%2==0){
row= new LinearLayout(this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
row.setLayoutParams(lp);
}
FrameLayout layout = new FrameLayout(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams((width/2)-10,(height/2)-10);
params.setMargins(5, 5, 5, 5);
layout.setLayoutParams(params);
if(i%2==0){
layout.setBackgroundColor(Color.parseColor("#CACACA"));
}
else {
layout.setBackgroundColor(Color.parseColor("#333333"));
}
ProgressBar pr=new ProgressBar(this);
FrameLayout.LayoutParams params_p = new FrameLayout.LayoutParams((width/6),(width/6));
params_p.gravity=Gravity.CENTER;
pr.setLayoutParams(params_p);
layout.addView(pr);
ImageView im=new ImageView(this);
FrameLayout.LayoutParams params_im = new FrameLayout.LayoutParams((width/2),(width/2));
im.setImageResource(R.drawable.man);
im.setLayoutParams(params_im);
layout.addView(im);
row.addView(layout);
table.addView(row);
}
Error is
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
Thanks in advance
I have a feeling the problem is here:
LinearLayout row=new LinearLayout(this);;
for(int i=0;i<5;i++){
The first time it loops row will be replaced by your second assignment of it and held in the variable OUTSIDE of the loop, the second time it is not replaced and because it is outside of the loop, it is the same row you're trying to add to the table again. Just swap the order of the lines above.
I have a function that loops over a list and returns the row elements.
I want to add these rows to an empty RelativeLayout one by one, so that all the rows are displayed sequentially vertically.
But the code below is not working.
void setRows(RelativeLayout rowContainer, ViewHolder viewHolder,
List<RowCollection> rowCollection) {
for (int i = 0; i < rowCollection.size(); i++) {
MyRow row = new MyRow(rowCollection.get(i));
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, row.getId());
rowContainer.addView(row, params);
}
}
Only the last row element is displayed.
I guess but am not sure, that the rows are overlapping each other.
You are placing all your Views to the Bottom of the RelativeLayout, so they are always on top of the other. So there is always only the last one visible because the other ones are blow it.
Change your Code to this:
params.addRule(RelativeLayout.BELOW, iDFromPreviousView);
change to:
void setRows(RelativeLayout rowContainer, ViewHolder viewHolder,
List<RowCollection> rowCollection) {
MyRow lat_row=null;
for (int i = 0; i < rowCollection.size(); i++) {
MyRow row = new MyRow(rowCollection.get(i));
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
if (i == 0) {
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
}else{
//<---get the id of previous row not sure what RowCollection contains
params.addRule(RelativeLayout.ABOVE,lat_row.getId());
}
lat_row=row;
rowContainer.addView(row, params);
}
}
this will stack all the view from bottom to top.
I created button in activity programmaticaly, not in xml file. Then I wanted to set it LayoutParams like in this link: How to programmatically set the layout_align_parent_right attribute of a Button in Relative Layout?
But when I tried to launch, I had an exception.
Here is my code:
RelativeLayout ll2 = new RelativeLayout(this);
//ll2.setOrientation(LinearLayout.HORIZONTAL);
ImageButton go = new ImageButton(this);
go.setId(cursor.getInt(cursor.getColumnIndex("_id")));
go.setClickable(true);
go.setBackgroundResource(R.drawable.go);
RelativeLayout.LayoutParams params1 = (RelativeLayout.LayoutParams)go.getLayoutParams();
params1.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); // LogCat said I have Null Pointer Exception in this line
go.setLayoutParams(params1);
go.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent i3 = new Intent(RecipesActivity.this, MedicineActivity.class);
i3.putExtra(ShowRecipe, v.getId());
i3.putExtra("Activity", "RecipesActivity");
RecipesActivity.this.startActivity(i3);
}
});
ll2.addView(go);
Why my app throws an exception? Thanks.
Change this:
RelativeLayout.LayoutParams params1 = (RelativeLayout.LayoutParams)go.getLayoutParams();
params1.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
go.setLayoutParams(params1);
for:
RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
params1.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
go.setLayoutParams(params1);
When you create a View programatically it doesn't have any LayoutParams, that's why you are getting NullPointerException. If you inflate the view from XML, the view is coming now with LayoutParams.
From what I see, it looks like
go.getLayoutParams()
returns null in this line
RelativeLayout.LayoutParams params1 = (RelativeLayout.LayoutParams)go.getLayoutParams();
because you have no LayoutParams set for it. This will obviously make params1 null so you get NPE here
params1.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
when you try to run a method on it (addRule(RelativeLayout.ALIGN_PARENT_RIGHT))
getLayoutParams() is for a View that has params already set. So you will need to set them for go before trying to get them to use for another View
This method may return null if this View is not attached to a parent ViewGroup or {#link #setLayoutParams(android.view.ViewGroup.LayoutParams)} was not invoked successfully. When a View is attached to a parent ViewGroup, this method must not return null.
According to the annotation of getLayoutParams() in the View.java, your ImageButton is created programly and is not attached to a parent ViewGroup when you call the method, so it returns null.