1

我有一个使用TableLayout. 我正在动态地TableRowsTableLayout. 我已经将布局创建为片段,但是当我将该片段添加到我的片段时,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;
    }


}

这是我尝试使用它开发的屏幕。 除了这个,还有其他内存有效的方法来开发它吗?

4

1 回答 1

2

Bellow the Binary XML file line #25: Error inflating class fragment in logcat 应该是真正问题的解释。问题是您错误地使用了膨胀的布局,并且您遇到了一种情况,即您将具有父级的视图添加到将抛出一个父级的同一父级IllegalStateException(因为必须首先从该层次结构中删除位于视图层次结构中的视图然后添加)。

onCreateView你膨胀布局文件,然后搜索视图。现在由于某些未知的原因,您尝试再次将这些视图(它们已经在布局中作为 a 的子级TableRow)添加到它们在布局中具有的同一个父级,这是错误的。LinearLayout你在 的末尾做同样的事情onCreateView,你已经将它ScrollView添加到了,LinearLayout但然后再次尝试将相同的视图添加到LinearLayout,这是错误的。

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

同样添加 100TableRows和 6 个孩子也不起作用,你会在内存不足的情况下杀死应用程序,会有很多视图要渲染。ListView如果您在布局中需要那么多行,请改用 a 。

于 2012-10-02T06:10:36.770 回答