Android - 致命异常:膨胀 class abLayout 时出错 - 自定义样式无法解决
Android - FATAL EXCEPTION: Error inflating class abLayout - Custom Style Does Not Resolve
我收到一个致命错误,指出:二进制 XML 文件行 #9:二进制 XML 文件行 #9:膨胀 class
时出错
我所做的所有研究都指向创建自定义样式来解决问题:
Error inflating class android.support.design.widget.TabLayout
或者 SupportLibrary - 或者添加缺失的背景:
android.view.inflateexception binary xml file line #1 error inflating class android.widget.relativeLayout
但是 none 这些解决方案似乎解决了这个强制关闭问题,我不确定如何解决它。
如有任何建议,我们将不胜感激。
ChatFrag.Java
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();
}
}
});
}
activity_chat.xml
<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>
LogCat
--------- 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)
Build.gradle
dependencies {
...
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.android.support:multidex:1.0.1'
compile 'com.android.support:design:23+'
...
}
v21styles.xml
<?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>
您可以删除该样式试试这个而不是更改选项卡指示器颜色的样式:
<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"/>
并在颜色中设置颜色...
我收到一个致命错误,指出:二进制 XML 文件行 #9:二进制 XML 文件行 #9:膨胀 class
时出错我所做的所有研究都指向创建自定义样式来解决问题:
或者 SupportLibrary - 或者添加缺失的背景:
android.view.inflateexception binary xml file line #1 error inflating class android.widget.relativeLayout
但是 none 这些解决方案似乎解决了这个强制关闭问题,我不确定如何解决它。
如有任何建议,我们将不胜感激。
ChatFrag.Java
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();
}
}
});
}
activity_chat.xml
<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>
LogCat
--------- 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)
Build.gradle
dependencies {
...
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.android.support:multidex:1.0.1'
compile 'com.android.support:design:23+'
...
}
v21styles.xml
<?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>
您可以删除该样式试试这个而不是更改选项卡指示器颜色的样式:
<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"/>
并在颜色中设置颜色...