android.support.design.widget.TabItem无法强制转换为android.view.ViewGroup

时间:2018-03-26 07:51:35

标签: java android dialog fragment

我在运行时遇到以下错误。

  

E / AndroidRuntime:致命异常:主要                     处理:com.example.usamaakmal.trackpoint,PID:8774                     android.view.InflateException:二进制XML文件行#0:android.support.design.widget.TabItem无法强制转换为android.view.ViewGroup                     引起:java.lang.ClassCastException:android.support.design.widget.TabItem无法强制转换为   android.view.ViewGroup                         在android.view.LayoutInflater.rInflate(LayoutInflater.java:870)                         在android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:832)                         在android.view.LayoutInflater.rInflate(LayoutInflater.java:872)                         在android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:832)                         在android.view.LayoutInflater.rInflate(LayoutInflater.java:872)                         在android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:832)                         在android.view.LayoutInflater.inflate(LayoutInflater.java:518)                         在android.view.LayoutInflater.inflate(LayoutInflater.java:426)                         在com.example.usamaakmal.trackpoint.PresentationLayer.Dialogs.DateTimePickerDialog.onCreateView(DateTimePickerDialog.java:20)                         在android.support.v4.app.Fragment.performCreateView(Fragment.java:2354)                         在android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1419)                         在android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1740)                         在android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1809)                         在android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:799)                         在android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2580)                         在android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2367)                         在android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2322)                         在android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2229)                         在android.support.v4.app.FragmentManagerImpl $ 1.run(FragmentManager.java:700)                         在android.os.Handler.handleCallback(Handler.java:761)                         在android.os.Handler.dispatchMessage(Handler.java:98)                         在android.os.Looper.loop(Looper.java:156)                         在android.app.ActivityThread.main(ActivityThread.java:6523)                         at java.lang.reflect.Method.invoke(Native Method)                         在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:942)                         在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832)

我的XML布局如下

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:text="Select Date And Time"
        android:textSize="20dp"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp"
        android:id="@+id/tv_date_time_title"/>


    <android.support.design.widget.TabLayout
        android:id="@+id/date_time_tab_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/tv_date_time_title"
        android:layout_centerVertical="true">

        <android.support.design.widget.TabItem
            android:id="@+id/tab_date_item"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Date">

            <DatePicker android:id="@+id/dp_date_picker"></DatePicker>

        </android.support.design.widget.TabItem>

        <android.support.design.widget.TabItem
            android:id="@+id/tab_time_item"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Time">

            <TimePicker android:id="@+id/tp_time_picker"></TimePicker>
        </android.support.design.widget.TabItem>
    </android.support.design.widget.TabLayout>
</RelativeLayout>

我的自定义Dialog片段类在

下面
public class DateTimePickerDialog extends DialogFragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.date_time_picker_layout,container,false);
        return view;
    }
}

1 个答案:

答案 0 :(得分:4)

TabItem扩展ViewReference here。因此,您无法在TabItem内添加其他视图。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:text="Select Date And Time"
        android:textSize="20dp"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp"
        android:id="@+id/tv_date_time_title"/>


    <android.support.design.widget.TabLayout
        android:id="@+id/date_time_tab_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/tv_date_time_title"
        android:layout_centerVertical="true">

        <android.support.design.widget.TabItem
            android:id="@+id/tab_date_item"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Date" />

        <android.support.design.widget.TabItem
            android:id="@+id/tab_time_item"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Time" />
    </android.support.design.widget.TabLayout>
</RelativeLayout>