片段不会正确地扩大布局

时间:2012-10-01 16:48:18

标签: android layout android-fragments inflate android-tablelayout

我有一个使用TableLayout创建的网格。我正在动态地将TableRows添加到TableLayout。我已将布局创建为片段,但当我将该片段添加到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?

1 个答案:

答案 0 :(得分:2)

bellow 二进制XML文件行#25:在logcat中输出类片段时出错应该是对实际问题的解释。问题是你错误地使用了膨胀的布局,并且你会遇到这样一种情况,即你将同一个父项的父视图添加到一个IllegalStateException的视图中(作为视图层次结构中的视图必须首先从该层次结构中删除然后添加)。

onCreateView中,您为布局文件充气,然后搜索视图。现在,出于一些不明原因,您尝试再次将这些视图(已在布局中作为TableRow的子项)添加到它们在布局中的相同父级,这是错误的。与LinearLayout末尾的onCreateView相同,您已将ScrollView添加到LinearLayout,但之后又尝试将相同的视图添加到LinearLayout for,这是错误的。

如果您在布局中有这些视图,则无需再添加视图,您只需添加在TableRows循环中构建的新行。

同时添加100个ListView和6个孩子将无法正常工作,您将使用内存不足异常终止应用程序,将会有许多视图要呈现。如果您的布局中需要很多行,请使用{{1}}。