繁体   English   中英

如何对两个不同类型的视图使用相同的ID

[英]How to use the same id for 2 views of different types

我有2个滚动视图,一个是纵向布局,另一个是水平布局(布局-土地),在onCreate中,我使用带有共享ID的findViewById,问题是,其中一个是ScrollView,另一个是Horizo​​ntalScrollView旋转手机时会导致以下异常:

Caused by: java.lang.IllegalArgumentException: Wrong state class, expecting View State but received class android.widget.ScrollView$SavedState instead. This usually happens when two views of different type have the same id in the same hierarchy. This view's id is id/mainscreen_scrollview. Make sure other views do not use the same id.

我为两个ScrollView赋予相同ID的原因背后的代码,以下代码位于onCreate(....)中

    mHomeScrollView = (FrameLayout) findViewById(R.id.mainscreen_scrollview);

    mHomeScrollView.setOnTouchListener(new View.OnTouchListener(){
        @Override
        public boolean onTouch(View v, MotionEvent event){
            return mGestureDetector.onTouchEvent(event);
        }
    });

编辑1:

根据与尊敬的成员的讨论,这似乎是最合适的解决方案,我希望它可以对其他人有所帮助:

    int scrollViewId = R.id.mainscreen_scrollview;
    if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE){
        scrollViewId = R.id.mainscreen_horizontalscrollview;
    }

您已经为两个Scrollview设置了相同的ScrollView id

错误:

确保其他视图不使用相同的ID。

id是homescreen_scrollview

注意:两个scrollView ID应该不同。

通过创建两个具有相同名称但位于不同目录(例如layout&layout_land)中的布局文件,您可以为同一对象分配两个相同的ID。 只需将id设置为android:id="@+id/my_id"即可。 注意:两者都必须是同一对象的一部分(例如,两个ScrollViews)。

我对此不确定,但是您可以尝试一下并告诉我们是否可行。

scrollView var1= (scrollview) var.findViewByID(R.id.first_scroll_view);

HorizontalScrollView  var2 = (HorizontalScrollView) var2.findViewByID(R.id.second_scroll_view)!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM