Note that there are some explanatory texts on larger screens.

plurals
  1. POAccess fragment controls from FragmentActivity
    primarykey
    data
    text
    <p>I have a <code>FragmentActivity</code> that should hold 2 <code>Fragments</code>. Each fragment has it's own layout with a table in it. I need to access the tables from the fragments' layouts from within the <code>FragmentActivity</code> to put some data that I read from an XML file. My problem is that when I use </p> <p><code>tableOrders = (TableLayout)findViewById(R.id.tabel1);</code></p> <p>it returns <code>null</code>. Now, I know that items from layouts can only be accessed before they have been inflated, and I've done that in the FragmentActivity before trying to call <code>findViewById()</code>. Here is the code for what I've tried:</p> <hr> <p>The onCreate of the <code>FragmentHolder.java</code> that is derived from <code>FragmentActivity</code>:</p> <pre><code> 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); } </code></pre> <hr> <p>The fragment class (don't know if it helps):</p> <pre><code> 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; } } </code></pre> <p><hr> The fragment class (don't know if it helps):</p> <pre><code> 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; } } </code></pre> <hr> <p>And the .xml file for the fragment:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" &gt; &lt;TableLayout android:id="@+id/tabel1header" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@color/tablebgcolor" &gt; &lt;TableRow &gt; &lt;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" /&gt; &lt;/TableRow&gt; &lt;TableRow android:id="@+id/ordersTable" style="@style/HeaderRow" &gt; &lt;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" /&gt; &lt;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" /&gt; &lt;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" /&gt; &lt;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"/&gt; &lt;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" /&gt; &lt;/TableRow&gt; &lt;/TableLayout&gt; &lt;ScrollView android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="1" &gt; &lt;TableLayout android:id="@+id/tabel1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@color/tablebgcolor"&gt; &lt;/TableLayout&gt; &lt;/ScrollView&gt; &lt;/LinearLayout&gt; </code></pre> <p>With that said hope someone has a solution for this (probably it's something easy that I missed).</p> <p><hr> Edit: The code I use to put the Fragments on the FragmentActivity:</p> <pre><code>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); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload