Note that there are some explanatory texts on larger screens.

plurals
  1. POA very strange error with regards to Android's ViewFlipper and the first call to showNext() or showPrevious
    primarykey
    data
    text
    <p>I am currently developing an application and have run into a small error that I can hack to fix but I would prefer to know why my error occurs. I have a tabbed interface that uses the TabWidget to create three tabs. My error lies in the first tab where I have placed a ViewFlipper. The ViewFlipper is meant to flip between two different views (using two ViewStub's to connect to two previously designed layouts). Basically the first view has a button that when pressed should change the ViewFlipper in the current Tab to the next (or specified) view\layout. Now my error comes in when the application is launched for the first time. The very first request to the ViewFlipper (showNext or showPrevious or setDisplayChild) isn't registered. The next press (and presses of the button thereafter) correctly change the display. I have checked using LogCat whether the button actually calls the registered onClick function (defined in the xml layout) and the button does register the click. Once again the problem is specifically the very first call to the ViewFlipper view changing and every call thereafter works flawlessly. </p> <p>Any help would be greatly appreciated. I am willing to post the code if necessary but it is quite lengthy. </p> <p>EDIT: Added code.</p> <p><strong>Main XML</strong></p> <pre><code>&lt;TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/tabhost" android:layout_width="fill_parent" android:layout_height="fill_parent"&gt; &lt;LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"&gt; &lt;TabWidget android:id="@android:id/tabs" android:layout_width="fill_parent" android:layout_height="wrap_content" /&gt; &lt;FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="fill_parent"&gt; &lt;ViewFlipper android:id="@+id/tab1" android:layout_width="fill_parent" android:layout_height="fill_parent"&gt; &lt;!-- View 1 --&gt; &lt;ViewStub android:id="@+id/stub1" android:inflatedId="@+id/subTree1" android:layout="@layout/layout_tab1" android:layout_width="fill_parent" android:layout_height="fill_parent" /&gt; &lt;!-- View 2 --&gt; &lt;ViewStub android:id="@+id/stub2" android:inflatedId="@+id/subTree2" android:layout="@layout/layout_tab12" android:layout_width="fill_parent" android:layout_height="fill_parent"/&gt; &lt;/ViewFlipper&gt; &lt;ViewFlipper android:id="@+id/tab2" android:layout_width="fill_parent" android:layout_height="fill_parent"&gt; &lt;!-- View 1 --&gt; &lt;ViewStub android:id="@+id/stub3" android:inflatedId="@+id/subTree3" android:layout="@layout/layout_tab2" android:layout_width="fill_parent" android:layout_height="fill_parent" /&gt; &lt;/ViewFlipper&gt; &lt;ViewFlipper android:id="@+id/tab3" android:layout_width="fill_parent" android:layout_height="fill_parent"&gt; &lt;!-- View 1 --&gt; &lt;ViewStub android:id="@+id/stub4" android:inflatedId="@+id/subTree4" android:layout="@layout/layout_tab3" android:layout_width="fill_parent" android:layout_height="fill_parent" /&gt; &lt;!-- View 2 --&gt; &lt;/ViewFlipper&gt; &lt;/FrameLayout&gt; &lt;/LinearLayout&gt; &lt;/TabHost&gt; </code></pre> <p><strong>Tab 1 XML</strong></p> <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/tabone" android:layout_width="fill_parent" android:layout_height="fill_parent"&gt; &lt;Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" android:onClick="onButtonClick"/&gt; &lt;TextView android:id="@+id/textview1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="Tab 1" /&gt; &lt;/LinearLayout&gt; </code></pre> <p><em>Application Code</em></p> <pre><code> TabHost mTabHost; TextView textCurrent; ViewFlipper viewFlipper; String tag = "Tag"; int value = 0; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mTabHost = getTabHost(); mTabHost.addTab(mTabHost.newTabSpec("tab_test1").setIndicator("TAB 1").setContent(R.id.tab1)); mTabHost.addTab(mTabHost.newTabSpec("tab_test2").setIndicator("TAB 2").setContent(R.id.tab2)); mTabHost.addTab(mTabHost.newTabSpec("tab_test3").setIndicator("TAB 3").setContent(R.id.tab3)); mTabHost.setCurrentTab(0); mTabHost.setOnTabChangedListener(this); } public void onButtonClick(View v) { if(v.getId() == R.id.button1) { Log.d(tag, "Button Pressed"); viewFlipper = (ViewFlipper) findViewById(R.id.tab1); viewFlipper.showNext(); /* //HACK TO GET NEXT BUTTON WORKING value++; if(value == 1) viewFlipper.showNext(); */ } } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if ((keyCode == KeyEvent.KEYCODE_BACK)) { //Back key pressed mTabHost = getTabHost(); if(mTabHost.getCurrentTab() == 0) { //Things to Do viewFlipper = (ViewFlipper) findViewById(R.id.tab1); viewFlipper.showPrevious(); } else { finish(); } return true; } return super.onKeyDown(keyCode, event); } public void onTabChanged(String arg0) { // TODO Auto-generated method stub if(arg0.equals("tab_test1")) { Log.d(tag, "Tab 1 Accessed"); viewFlipper = (ViewFlipper) findViewById(R.id.tab1); int index = viewFlipper.indexOfChild(findViewById(R.id.subTree1)); viewFlipper.setDisplayedChild(index); } } </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.
 

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