gpt4 book ai didi

android - Fragment 未正确展开布局

转载 作者:行者123 更新时间:2023-11-29 21:58:10 25 4
gpt4 key购买 nike

我有一个使用 TableLayout 创建的网格。我正在将 TableRows 动态添加到 TableLayout。我已将布局创建为 fragment ,但是当我将该 fragment 添加到我的 FragmentActivity 时,出现以下错误:

Binary XML file line #25: Error inflating class fragment

这是我的代码:

    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;
}


}

这是我的 layout.xml 文件

    <?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>

编辑:这是解决问题的更新代码。

    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;
}


}

Here is the screen which Im trying to develop using this. Is there any other memory efficient way to develop this than this?

最佳答案

在 logcat 中的 Binary XML file line #25: Error inflating class fragment 下方应该是对真正问题的解释。问题是您错误地使用了膨胀的布局,并且您遇到了这样一种情况,即您将具有父级的 View 添加到同一父级,这将抛出 IllegalStateException(作为 View 中的 View 必须先从该层次结构中删除层次结构,然后再添加)。

onCreateView 中,您扩充布局文件,然后搜索 View 。现在由于某些未知原因,您尝试再次将这些 View (它们已经在布局中作为 TableRow 的子级)添加到它们在布局中的同一父级,这是错误的。与在 onCreateView 末尾对 LinearLayout 执行的操作相同,您已经将 ScrollView 添加到 LinearLayout 但随后再次尝试将相同的 View 添加到 LinearLayout,这是错误的。

如果您在布局中有这些 View ,则无需再次添加它们,您只需添加您在该for 循环中构造的新行。

同时添加 100 个 TableRows 和 6 个 child 将不起作用,您将因内存不足异常终止应用程序,将有许多 View 要呈现。如果您的布局中需要那么多行,请改用 ListView

关于android - Fragment 未正确展开布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12677547/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com