AppCompatImageView无法强制转换为ViewGroup

时间:2017-05-15 15:22:34

标签: java android

我试图创建包含内部文本的指示符的片段滑块。

我在线发现了一些包含图片滑块的教程,并且我已将其编辑为文本滑块,但我收到此错误消息:

  

android.support.v7.widget.AppCompatImageView无法强制转换为android.view.ViewGroup

现在我检查了我的代码,我在MainFragment.java中只使用过一次ViewGroup

public class MainFragment extends Fragment {

    private ViewPager _mViewPager;
    private CalcsPagerAdapter _adapter;
    private ImageView _btn1, _btn2, _btn3;
    public MainFragment() {
    }
    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        setUpView();
        setTab();
        onCircleButtonClick();
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_main, container, false);
    }
    private void onCircleButtonClick() {
        _btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                _btn1.setImageResource(R.drawable.fill_circle);
                _mViewPager.setCurrentItem(0);
            }
        });
        _btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                _btn2.setImageResource(R.drawable.fill_circle);
                _mViewPager.setCurrentItem(1);
            }
        });
        _btn3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                _btn3.setImageResource(R.drawable.fill_circle);
                _mViewPager.setCurrentItem(2);
            }
        });
    }
    private void setUpView() {
        _mViewPager = (ViewPager) getView().findViewById(R.id.textviewPager);
        _adapter = new CalcsPagerAdapter(getActivity(), getFragmentManager());
        _mViewPager.setAdapter(_adapter);
        _mViewPager.setCurrentItem(0);
        initButton();
    }
    private void setTab() {
        _mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrollStateChanged(int position) {
            }
            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }
            @Override
            public void onPageSelected(int position) {
                _btn1.setImageResource(R.drawable.holo_circle);
                _btn2.setImageResource(R.drawable.holo_circle);
                _btn3.setImageResource(R.drawable.holo_circle);
                btnAction(position);
            }
        });
    }
    private void btnAction(int action) {
        switch (action) {
            case 0:
                _btn1.setImageResource(R.drawable.fill_circle);
                break;
            case 1:
                _btn2.setImageResource(R.drawable.fill_circle);
                break;
            case 2:
                _btn3.setImageResource(R.drawable.fill_circle);
                break;
        }
    }
    private void initButton() {
        _btn1 = (ImageView) getView().findViewById(R.id.btn1);
        _btn1.setImageResource(R.drawable.fill_circle);
        _btn2 = (ImageView) getView().findViewById(R.id.btn2);
        _btn3 = (ImageView) getView().findViewById(R.id.btn3);
    }
    private void setButton(Button btn, String text, int h, int w) {
        btn.setWidth(w);
        btn.setHeight(h);
        btn.setText(text);
    }
}

我也在每个片段java文件中使用它(例如fragmentBmi.java):

public class fragmentBmi extends Fragment {


    public fragmentBmi() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_bmi, container, false);
    }


}

正如您所看到的,我只在onCreateView方法中使用过一次。

如何将ImageView转换问题修复到ViewGroup?

Tutorial I've used

修改

fragment_main.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#32383d">
    <android.support.v4.view.ViewPager
        android:layout_width="fill_parent" android:layout_height="fill_parent"
        android:id="@+id/textviewPager" />
    <include layout="@layout/footer"/>
</FrameLayout>

mainactivity.xml

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/fragment"
        android:name="com.none.myapplication.MainFragment"
        tools:layout="@layout/fragment_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

footer.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_gravity="bottom"
    android:background="#00000000">


    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center">

        <ImageView
            android:layout_height="10dp"
            android:layout_width="10dp"
            android:id="@+id/btn1"
            android:src="@drawable/holo_circle"
            android:layout_gravity="center_vertical">

            <ImageView
                android:layout_height="10dp"
                android:layout_width="10dp"
                android:id="@+id/btn2"
                android:layout_toRightOf="@id/btn1"
                android:src="@drawable/holo_circle"
                android:layout_margin="10dp">

                <ImageView
                    android:layout_height="10dp"
                    android:layout_width="10dp"
                    android:id="@+id/btn3"
                    android:layout_toRightOf="@id/btn2"
                    android:src="@drawable/holo_circle"
                    android:layout_gravity="center_vertical">
                </ImageView>
            </ImageView>
        </ImageView>
    </LinearLayout>
</LinearLayout>

每个片段还有xml,但它只包含文本视图,因此对我的问题毫无用处。

1 个答案:

答案 0 :(得分:1)

您必须结束ImageView标记:footer.xml

 <?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="wrap_content"
    android:layout_gravity="bottom"
    android:background="#00000000"
    android:orientation="vertical">


    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center">

        <ImageView
            android:id="@+id/btn1"
            android:layout_width="10dp"
            android:layout_height="10dp"
            android:layout_gravity="center_vertical"
            android:src="@drawable/holo_circle" />

        <ImageView
            android:id="@+id/btn2"
            android:layout_width="10dp"
            android:layout_height="10dp"
            android:layout_margin="10dp"
            android:layout_toRightOf="@id/btn1"
            android:src="@drawable/holo_circle" />

        <ImageView
            android:id="@+id/btn3"
            android:layout_width="10dp"
            android:layout_height="10dp"
            android:layout_gravity="center_vertical"
            android:layout_toRightOf="@id/btn2"
            android:src="@drawable/holo_circle" />

    </LinearLayout>
</LinearLayout>

当您将图像放在imageview标记

下时会发生异常

由于imageview不是ViewGroup Linearlayout,因此您无法在其下添加多个元素。