我有一个FragmentActivity
应该容纳2 Fragments
。每个片段都有自己的布局,里面有一个表格。我需要从FragmentActivity
中的片段布局访问表格,以放置我从XML文件中读取的一些数据。我的问题是,当我使用
tableOrders = (TableLayout)findViewById(R.id.tabel1);
它返回null
。现在,我知道布局中的项目只能在它们被夸大之前被访问,并且我在尝试调用findViewById()
之前在FragmentActivity中完成了这些操作。这是我尝试过的代码:
FragmentHolder.java
的{{1}}的onCreate:
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;
}
}
片段的.xml文件:
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;
}
}
有了这个说希望有人有一个解决方案(可能这是我错过的一些简单的事情)。
<小时/> 编辑:我用于将片段放在FragmentActivity上的代码:
<?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>