Fragment does not inflate the layout properly
-
12-12-2019 - |
Question
I have a grid which is created using a TableLayout
. I'm dynamically adding the TableRows
to the TableLayout
. I have created the layout as a fragment but when I'm adding that fragment to my FragmentActivity
it gives me the following error:
Binary XML file line #25: Error inflating class fragment
Here is my code:
public class TodayPriceList extends Fragment {
LinearLayout llTop = null;
LinearLayout llPriceList = null;
RelativeLayout rlPriceTableHeading = null;
TableLayout priceTableHedaer = null;
TableLayout tlPriceList = null;
ScrollView svGridScroll = null;
TableRow row;
TextView tvSymbol = null;
TextView tvSharevolume = null;
TextView tvOpen = null;
TextView tvHigh = null;
TextView tvLow = null;
TextView tvLastTrade = null;
TextView symbol,shareVol,open,high,low,lastTrade;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.todaypricelist, container, false);
llTop = (LinearLayout)view.findViewById(R.id.ll_top);
llPriceList = (LinearLayout)view.findViewById(R.id.ll_priceList);
rlPriceTableHeading = (RelativeLayout)view.findViewById(R.id.rl_priceTable_heading);
llPriceList = (LinearLayout)view.findViewById(R.id.ll_priceList);
tlPriceList = (TableLayout)view.findViewById(R.id.priceTable);
priceTableHedaer = (TableLayout)view.findViewById(R.id.priceTableHedaer);
svGridScroll = (ScrollView)view.findViewById(R.id.ScrollView11);
tvSymbol = (TextView)view.findViewById(R.id.tvSymbol);
tvSharevolume = (TextView)view.findViewById(R.id.tvSharevolume);
tvOpen = (TextView)view.findViewById(R.id.tvOpen);
tvHigh = (TextView)view.findViewById(R.id.tvHigh);
tvLow = (TextView)view.findViewById(R.id.tvLow);
tvLastTrade = (TextView)view.findViewById(R.id.tvLastTrade);
//fillPriceTable(container);
priceTableHedaer.addView(tvSymbol);
priceTableHedaer.addView(tvSharevolume);
priceTableHedaer.addView(tvOpen);
priceTableHedaer.addView(tvHigh);
priceTableHedaer.addView(tvLow);
priceTableHedaer.addView(tvLastTrade);
//define array data
String[] dummy=new String[]{"AEL.N000","48.56","16.00","15.00","16.00","18.00"};
//Converting to dip unit
int dip = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX,
(float) 1, getResources().getDisplayMetrics());
for (int i = 0; i < 100; i++) {
row=new TableRow(container.getContext());
int rowcount=i%2;
if(rowcount>0){
row.setBackgroundColor(Color.parseColor("#E0F8F7"));
}
symbol = new TextView(container.getContext());
//t1.setTextColor(getResources().getColor(R.color.yellow));
shareVol = new TextView(container.getContext());
//t2.setTextColor(getResources().getColor(R.color.dark_red));
open=new TextView(container.getContext());
high=new TextView(container.getContext());
low=new TextView(container.getContext());
lastTrade=new TextView(container.getContext());
symbol.setText(dummy[0]);
shareVol.setText(dummy[1]);
open.setText(dummy[2]);
high.setText(dummy[3]);
low.setText(dummy[4]);
lastTrade.setText(dummy[5]);
symbol.setWidth(55*dip);
shareVol.setWidth(30*dip);
open.setWidth(30*dip);
high.setWidth(30*dip);
low.setWidth(30*dip);
lastTrade.setWidth(30*dip);
symbol.setGravity(android.view.Gravity.CENTER_VERTICAL);
shareVol.setGravity(android.view.Gravity.CENTER_VERTICAL);
open.setGravity(android.view.Gravity.CENTER_VERTICAL);
high.setGravity(android.view.Gravity.CENTER_VERTICAL);
low.setGravity(android.view.Gravity.CENTER_VERTICAL);
lastTrade.setGravity(android.view.Gravity.CENTER_VERTICAL);
symbol.setTextSize(12);
shareVol.setTextSize(12);
open.setTextSize(12);
high.setTextSize(12);
low.setTextSize(12);
lastTrade.setTextSize(12);
symbol.setHeight(20*dip);
symbol.setPadding(5, 0, 0, 0);
row.addView(symbol);
row.addView(shareVol);
row.addView(open);
row.addView(high);
row.addView(low);
row.addView(lastTrade);
tlPriceList.addView(row, new TableLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
}
rlPriceTableHeading.addView(priceTableHedaer);
svGridScroll.addView(tlPriceList);
llPriceList.addView(svGridScroll);
llTop.addView(rlPriceTableHeading);
llTop.addView(llPriceList);
return view;
}
}
Here is my layout.xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/ll_top"
>
<RelativeLayout
android:id="@+id/rl_priceTable_heading"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#E6E6E6" >
<TableLayout
android:id="@+id/priceTableHedaer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:stretchColumns="*"
android:shrinkColumns="*"
android:weightSum="50">
<TableRow >
<TextView android:id="@+id/tvSymbol"
android:layout_width="70dip"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:gravity="center|left"
android:text="@string/symbol"
android:textStyle="normal"
android:textColor="#2E2EFE"
android:textSize="11dip"
android:paddingLeft="5dip"
>
</TextView>
<TextView
android:id="@+id/tvSharevolume"
android:layout_width="45dip"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/tvSymbol"
android:gravity="center|left"
android:text="@string/sharevolume"
android:textColor="#2E2EFE"
android:textSize="11dip"
android:textStyle="normal"
>
</TextView>
<TextView
android:id="@+id/tvOpen"
android:layout_width="35dip"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/tvSharevolume"
android:gravity="center|left"
android:text="@string/open"
android:textColor="#2E2EFE"
android:textSize="11dip"
android:textStyle="normal" />
<TextView
android:id="@+id/tvHigh"
android:layout_width="45dip"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/tvOpen"
android:gravity="center|left"
android:text="@string/high"
android:textColor="#2E2EFE"
android:textSize="11dip"
android:textStyle="normal" />
<TextView
android:id="@+id/tvLow"
android:layout_width="45dip"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/tvHigh"
android:gravity="center|left"
android:text="@string/low"
android:textColor="#2E2EFE"
android:textSize="11dip"
android:textStyle="normal" />
<TextView
android:id="@+id/tvLastTrade"
android:layout_width="45dip"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/tvLow"
android:gravity="center|left"
android:text="@string/lastTrad"
android:textColor="#2E2EFE"
android:textSize="11dip"
android:textStyle="normal" />
</TableRow>
</TableLayout>
</RelativeLayout>
<LinearLayout android:id="@+id/ll_priceList"
android:layout_height="wrap_content" android:layout_width="wrap_content">
<ScrollView android:id="@+id/ScrollView11"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:fillViewport="true">
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/priceTable"
android:layout_width="wrap_content"
android:background="#FFFFFF"
android:stretchColumns="*"
android:shrinkColumns="*"
android:layout_height="50dip">
</TableLayout>
</ScrollView>
</LinearLayout>
</LinearLayout>
Edit: Here is the updated code which fixed the issue.
public class TodayPriceList extends Fragment {
TableLayout tlPriceList = null;
TableRow row;
TextView tvSymbol = null;
TextView tvSharevolume = null;
TextView tvOpen = null;
TextView tvHigh = null;
TextView tvLow = null;
TextView tvLastTrade = null;
TextView symbol,shareVol,open,high,low,lastTrade;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.todaypricelist, container, false);
tlPriceList = (TableLayout)view.findViewById(R.id.priceTable);
tvSymbol = (TextView)view.findViewById(R.id.tvSymbol);
tvSharevolume = (TextView)view.findViewById(R.id.tvSharevolume);
tvOpen = (TextView)view.findViewById(R.id.tvOpen);
tvHigh = (TextView)view.findViewById(R.id.tvHigh);
tvLow = (TextView)view.findViewById(R.id.tvLow);
tvLastTrade = (TextView)view.findViewById(R.id.tvLastTrade);
//define array data
String[] dummy=new String[]{"AEL.N000","48.56","16.00","15.00","16.00","18.00"};
//Converting to dip unit
int dip = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX,
(float) 1, getResources().getDisplayMetrics());
for (int i = 0; i < 100; i++) {
row = new TableRow(tlPriceList.getContext());
int rowcount=i%2;
if(rowcount>0){
row.setBackgroundColor(Color.parseColor("#E0F8F7"));
}
symbol = new TextView(tlPriceList.getContext());
//t1.setTextColor(getResources().getColor(R.color.yellow));
shareVol = new TextView(tlPriceList.getContext());
//t2.setTextColor(getResources().getColor(R.color.dark_red));
open=new TextView(tlPriceList.getContext());
high=new TextView(tlPriceList.getContext());
low=new TextView(tlPriceList.getContext());
lastTrade=new TextView(tlPriceList.getContext());
symbol.setText(dummy[0]);
shareVol.setText(dummy[1]);
open.setText(dummy[2]);
high.setText(dummy[3]);
low.setText(dummy[4]);
lastTrade.setText(dummy[5]);
symbol.setWidth(55*dip);
shareVol.setWidth(30*dip);
open.setWidth(30*dip);
high.setWidth(30*dip);
low.setWidth(30*dip);
lastTrade.setWidth(30*dip);
symbol.setGravity(android.view.Gravity.CENTER_VERTICAL);
shareVol.setGravity(android.view.Gravity.CENTER_VERTICAL);
open.setGravity(android.view.Gravity.CENTER_VERTICAL);
high.setGravity(android.view.Gravity.CENTER_VERTICAL);
low.setGravity(android.view.Gravity.CENTER_VERTICAL);
lastTrade.setGravity(android.view.Gravity.CENTER_VERTICAL);
symbol.setTextSize(12);
shareVol.setTextSize(12);
open.setTextSize(12);
high.setTextSize(12);
low.setTextSize(12);
lastTrade.setTextSize(12);
symbol.setHeight(20*dip);
symbol.setPadding(5, 0, 0, 0);
row.addView(symbol);
row.addView(shareVol);
row.addView(open);
row.addView(high);
row.addView(low);
row.addView(lastTrade);
tlPriceList.addView(row, new TableLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
}
return view;
}
}
Solution
Bellow the Binary XML file line #25: Error inflating class fragment in the logcat should be the explanation of the real problem. The problem is that you use the inflated layout incorrectly and you get in a situation where you add a view that has a parent to the same parent which will throw an IllegalStateException
(as a view which is in a view hierarchy must first be removed from that hierarchy and then added).
In the onCreateView
you inflate the layout file and then search for the views. Now for some unknow reason you try to add those views again(which are already in the layout as children of a TableRow
) to the same parent that they have in the layout, which is wrong. The same thing you do with the LinearLayout
at the end of the onCreateView
, you already have the ScrollView
added to the LinearLayout
but then again try to add the same view to the LinearLayout
, which is wrong.
There is no need to add those view again if you have them in the layout, you just have to add the new rows that you construct in that for
loop.
Also adding 100 TableRows
with 6 children will not work, you'll kill the app with out of memory exceptions, there will be to many views to render. Use a ListView
instead if you need that many rows in your layout.