'Error inflating class Button' 尽管之前工作过

'Error inflating class Button' despite working before

在我的布局中实现一些按钮后,在应用程序部署期间 logcat 中出现以下错误:

android.view.InflateException: Binary XML file line #37: Error inflating class Button

我真的不明白为什么以前工作正常的时候会出现这种情况。

XML

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

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin">

        <TextView
            android:id="@+id/Information_textView"
            style="@android:style/TextAppearance.Large"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/information"
            android:textColor="@android:color/black" />
        <TableLayout
            android:id="@+id/Information_tableLayout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="20dp"
            android:layout_below="@id/Information_textView">

            <TableRow
                android:id="@+id/tableRow0"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">

                <Button
                    android:id="@+id/Information_btnMale"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:drawableStart="@drawable/ic_male"
                    android:text="@string/male"
                    android:textColor="@android:color/white"
                    android:textSize="30sp"/>
            </TableRow>

            <TableRow
                android:id="@+id/tableRow1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">

                <Button
                    android:id="@+id/Information_btnFemale"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:drawableStart="@drawable/female"
                    android:text="@string/female"
                    android:textColor="@android:color/white"
                    android:textSize="30sp"/>
            </TableRow>
        </TableLayout>
    </RelativeLayout>
</ScrollView>

Java

public class FragmentInformation extends android.support.v4.app.Fragment {

    public FragmentInformation() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        return inflater.inflate(R.layout.information, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        View v = getView();
        assert v != null;


        button0 = (Button) v.findViewById(R.id.Information_btnMale);
        button0.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                HeaderDialog headerDialog = new HeaderDialog(getActivity());
                headerDialog.setTitle(getResources().getString(R.string.male);                headerDialog.setMessage(getResources().getString(R.string.male_information);
                headerDialog.setNegativeButton("OK", null);
            }
        });


        Button button1 = (Button) v.findViewById(R.id.Information_btnFemale);
        button1.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                HeaderDialog headerDialog = new HeaderDialog(getActivity());
                headerDialog.setTitle(getResources().getString(R.string.female);
                headerDialog.setNegativeButton("OK", null);
            }
        });


        super.onActivityCreated(savedInstanceState);
    }
}

Logcat

06-27 18:44:58.214 7756-7756/com.companyname.informationapp E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                    Process: com.companyname.informationapp, PID: 7756
                                                                                    android.view.InflateException: Binary XML file line #413: Error inflating class Button
                                                                                        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
                                                                                        at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
                                                                                        at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
                                                                                        at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
                                                                                        at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
                                                                                        at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
                                                                                        at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
                                                                                        at com.companyname.informationapp.FragmentInformation.onCreateView(FragmentInformation.java:30)
                                                                                        at android.support.v4.app.Fragment.performCreateView(Fragment.java:2192)
                                                                                        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1299)
                                                                                        at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1528)
                                                                                        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1595)
                                                                                        at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:758)
                                                                                        at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2363)
                                                                                        at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2149)
                                                                                        at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2103)
                                                                                        at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:1984)
                                                                                        at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:626)
                                                                                        at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:166)
                                                                                        at android.support.v4.view.ViewPager.populate(ViewPager.java:1268)
                                                                                        at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:668)
                                                                                        at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:630)
                                                                                        at android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:611)

也许,您可能已经从支持库 23.2.0 升级,之后进行了更改

由于内存使用和配置更新问题,它已在 23.3.0 中删除。在 23.4.0 中,添加了相同的功能,但需要手动启用

static {
    AppCompatDelegate.setCompatVectorFromSourcesEnabled(true);
}