0

我有一个FragmentActivity应该持有 2 Fragments。每个片段都有自己的布局,其中包含一个表格。我需要从片段的布局中访问表,FragmentActivity以放置我从 XML 文件中读取的一些数据。我的问题是当我使用

tableOrders = (TableLayout)findViewById(R.id.tabel1);

它返回null。现在,我知道布局中的项目只能在它们被膨胀之前被访问,并且我在尝试调用之前在 FragmentActivity 中做到了这一点findViewById()。这是我尝试过的代码:


的 onCreateFragmentHolder.java派生自FragmentActivity

    private TableLayout tableOrders = null;
   private TableLayout tableExecutions = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_fragment_holder);
    initialisePaging();

    //View orderFragment = (View)findViewById(R.layout.fragment_orders);        
    tableOrders = (TableLayout)/*orderFragment.*/findViewById(R.id.tabel1);
    //View execFragment = (View)findViewById(R.layout.executions_fragment);
    tableExecutions = (TableLayout)/*execFragment.*/findViewById(R.id.tabel2);

    Intent intent = getIntent();
    Bundle bundle = intent.getExtras();

    if(bundle != null)
        logedUser = (User) bundle.getSerializable("user");

    AssetManager am = getApplicationContext().getAssets();

    String xmlContentOrders = XMLfunctions.getXml(am, "ordersData.xml");
    populateOrdersTable(xmlContentOrders);

    String xmlContentExecutions = XMLfunctions.getXml(am, "executionsData.xml");
    populateExecutionsTable(xmlContentExecutions);
}

片段类(不知道是否有帮助):

    public class OrdersFragment extends Fragment {
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
     super.onActivityCreated(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
     View view = inflater.inflate(R.layout.fragment_orders, container, false);
     return view;
    }
    }


片段类(不知道是否有帮助):

   public class OrdersFragment extends Fragment {
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
     super.onActivityCreated(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
     View view = inflater.inflate(R.layout.fragment_orders, container, false);
     return view;
    }
    }

以及片段的 .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="match_parent"
android:orientation="vertical" >


<TableLayout
    android:id="@+id/tabel1header"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@color/tablebgcolor" >

    <TableRow  >

        <TextView
            android:id="@+id/ordersHeader"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:layout_weight="1"
            android:text="@string/orders"
            android:textStyle="bold"                />
    </TableRow>

    <TableRow
        android:id="@+id/ordersTable"
        style="@style/HeaderRow" >

        <TextView
            android:id="@+id/textSymbol"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/symbol"
            style="@style/HeaderText" />

        <TextView
            android:id="@+id/textSide"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/side"
            style="@style/HeaderText" />

        <TextView
            android:id="@+id/textPrice"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/price"
            style="@style/HeaderText" />

        <TextView
            android:id="@+id/textQuantity"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/quantity" 
            style="@style/HeaderText"/>

        <TextView
            android:id="@+id/textRemaining"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/remanent"
            style="@style/HeaderText" />
    </TableRow>
</TableLayout>

<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="1" >

    <TableLayout
        android:id="@+id/tabel1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:background="@color/tablebgcolor">
    </TableLayout>
</ScrollView>

</LinearLayout>

话虽如此,希望有人对此有解决方案(可能很容易我错过了)。


编辑:我用来将片段放在 FragmentActivity 上的代码:

private void initialisePaging() {

    OrdersFragment fragmentOne = new OrdersFragment();
    ExecutionsFragment fragmentTwo= new ExecutionsFragment();

    PagerAdapter pagerAdapter = new PagerAdapter(getSupportFragmentManager());
    pagerAdapter.addFragment(fragmentOne);
    pagerAdapter.addFragment(fragmentTwo);

    ViewPager viewPager = (ViewPager) super.findViewById(R.id.viewPager);
    viewPager.setAdapter(pagerAdapter);
    viewPager.setOffscreenPageLimit(2);
    viewPager.setCurrentItem(0);
}
4

0 回答 0