我收到一个致命的错误说明:二进制XML文件行#9:二进制XML文件行#9:错误导致类
我所做的所有研究都指向创建自定义样式来解决问题:
Got Error inflating class android.support.design.widget.TabLayout Error inflating class android.support.design.widget.TabLayout
或者可能是SupportLibrary - 或添加缺少的背景:
然而,这些解决方案似乎都没有解决这个强制关闭问题,我不确定如何解决这个问题。
任何建议都表示赞赏。
public class ChatFrag extends Fragment {
...
public ChatViewPagerAdapter adapter;
private ViewPager viewPager;
private TabLayout allTabs;
...
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context = getActivity().getApplicationContext();
pref = getActivity().getSharedPreferences("myPrefs", Context.MODE_PRIVATE);
setup = Setup.getInstance();
isLoaded = false;
MessagesFrag = this;
View main = inflater.inflate(R.layout.activity_chat, container, false);
...
getAllWidgets();
setupViewPager();
...
private void setupViewPager() {
adapter = new ChatViewPagerAdapter(getActivity().getSupportFragmentManager());
activeChats = new ActiveChats();
visitors = new Visitors();
adapter.addFragment(activeChats, "Active Chats");
adapter.addFragment(visitors, "Visitors");
setViewPageAdapter();
}
private void getAllWidgets() {
viewPager = (ViewPager) getActivity().findViewById(R.id.viewpager);
viewPager.setOffscreenPageLimit(1);
allTabs = (TabLayout) getActivity().findViewById(R.id.tabs);
}
private void setViewPageAdapter() {
viewPager.setAdapter(adapter);
allTabs.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
if (tab.getPosition() != viewPager.getCurrentItem()) {
viewPager.setCurrentItem(tab.getPosition());
}
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
if (tab.getPosition() == viewPager.getCurrentItem()) {
adapter.notifyDataSetChanged();
}
}
});
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabGravity="fill"
app:tabMode="fixed"
style="@style/MyCustomTabLayout"/>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</LinearLayout>
--------- beginning of crash
09-21 11:08:15.434 25875-25875/com.redacted.redactedmobile E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.redacted.redactedmobile, PID: 25875
android.view.InflateException: Binary XML file line #9: Binary XML file line #9: Error inflating class android.support.design.widget.TabLayout
at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at com.redacted.redactedmobile.ChatFrag.onCreateView(ChatFrag.java:84)
dependencies {
...
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.android.support:multidex:1.0.1'
compile 'com.android.support:design:23+'
...
}
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="android:colorPrimaryDark">#227900</item>
<item name="android:colorPrimary">#227900</item>
<item name="colorPrimaryDark">#227900</item>
<item name="colorPrimary">#227900</item>
<item name="android:windowDisablePreview">true</item>
</style>
<style name="AppTheme.Base" parent="android:Theme.Material">
<item name="android:colorPrimary">#3F51B5</item>
<item name="android:colorPrimaryDark">#303F9F</item>
<item name="android:colorAccent">#FF4081</item>
</style>
<style name="MyCustomTabLayout" parent="Widget.Design.TabLayout">
<item name="tabIndicatorColor">#FF4081</item>
</style>
</resources>
答案 0 :(得分:0)
你可以删除那个样式试试这个而不是样式来改变标签指示颜色:
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabGravity="fill"
app:tabMode="fixed"
app:tabIndicatorColor="@color/your-color-name"/>
并将颜色设置为颜色......