【问题标题】:A very strange error with regards to Android's ViewFlipper and the first call to showNext() or showPrevious关于 Android 的 ViewFlipper 和第一次调用 showNext() 或 showPrevious 的一个非常奇怪的错误
【发布时间】:2011-12-16 20:02:39
【问题描述】:

我目前正在开发一个应用程序,遇到了一个小错误,我可以破解它来修复,但我想知道我的错误发生的原因。我有一个选项卡式界面,它使用 TabWidget 创建三个选项卡。我的错误在于我放置 ViewFlipper 的第一个选项卡。 ViewFlipper 旨在在两个不同的视图之间翻转(使用两个 ViewStub 连接到两个先前设计的布局)。基本上,第一个视图有一个按钮,按下该按钮应将当前选项卡中的 ViewFlipper 更改为下一个(或指定的)视图\布局。现在我的错误出现在第一次启动应用程序时。对 ViewFlipper 的第一个请求(showNext 或 showPrevious 或 setDisplayChild)未注册。下一次按下(以及之后按下按钮)正确地改变了显示。我已经使用 LogCat 检查了按钮是否实际调用了注册的 onClick 函数(在 xml 布局中定义)并且按钮确实注册了点击。再一次,问题特别是对 ViewFlipper 视图的第一次调用发生了变化,此后的每个调用都完美无缺。

任何帮助将不胜感激。如有必要,我愿意发布代码,但它很长。

编辑:添加代码。

主要 XML

<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">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">
            <ViewFlipper android:id="@+id/tab1"
                android:layout_width="fill_parent" 
                android:layout_height="fill_parent">

                <!-- View 1 -->
                <ViewStub android:id="@+id/stub1"
                   android:inflatedId="@+id/subTree1"
                   android:layout="@layout/layout_tab1"
                   android:layout_width="fill_parent"
                   android:layout_height="fill_parent" />

                <!-- View 2 -->
                <ViewStub android:id="@+id/stub2"
                   android:inflatedId="@+id/subTree2"
                   android:layout="@layout/layout_tab12" 
                   android:layout_width="fill_parent"
                   android:layout_height="fill_parent"/>
            </ViewFlipper>

            <ViewFlipper android:id="@+id/tab2"
                android:layout_width="fill_parent" android:layout_height="fill_parent">

                <!-- View 1 -->
                <ViewStub android:id="@+id/stub3"
                   android:inflatedId="@+id/subTree3"
                   android:layout="@layout/layout_tab2"
                   android:layout_width="fill_parent"
                   android:layout_height="fill_parent" />

            </ViewFlipper>    
            <ViewFlipper android:id="@+id/tab3"
                android:layout_width="fill_parent" android:layout_height="fill_parent">

                <!-- View 1 -->
                <ViewStub android:id="@+id/stub4"
                   android:inflatedId="@+id/subTree4"
                   android:layout="@layout/layout_tab3"
                   android:layout_width="fill_parent"
                   android:layout_height="fill_parent" />

                <!-- View 2 -->
            </ViewFlipper>         
        </FrameLayout>
    </LinearLayout>
</TabHost>

标签 1 XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tabone"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" 
        android:onClick="onButtonClick"/>

            <TextView 
                android:id="@+id/textview1"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" 
                android:text="Tab 1" />
</LinearLayout>

应用代码

    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);   
        }
    }

【问题讨论】:

  • 我想知道这是否与使用 ViewStubs 有关。您可以尝试使用实际布局替换存根的相同设置吗?
  • Yip 现在似乎可以工作了.... 很奇怪。不过,我需要以某种方式使用其他布局 - ViewFlipper 似乎在我在 viewFlipper 中使用 RelativeLayout 时遇到了问题。
  • 我想知道是否有某种方法可以修复该错误。 ViewStubs 导致错误(即在使用它们时不存在)这一切都很好,但我真的很想使用它们(大大简化了我的布局系统)。
  • 你能不能使用include标签得到一些相同的简化?

标签: android viewflipper


【解决方案1】:

听起来对我来说是一个空指针...在方法第一次运行之前检查所有变量是否已正确初始化。但是,如果没有您的代码,很难说出实际出了什么问题。

【讨论】:

  • viewFlipper 在按下按钮之前不会初始化。你应该把它移到你的 onCreate 方法。编辑:实际上没关系,因为您每次使用它时都对其进行了初始化
猜你喜欢
  • 2016-04-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多