下面的代码在 5.0 上运行良好,但在 4.4.4 上会遇到
D/AndroidRuntime﹕ Shutting down VM
W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xb0d42b20)
E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.xxx.xxx, PID: 2352
android.view.InflateException: Binary XML file line #6: Error inflating class <unknown>
主要 Activity
package com.example.xxx.xxx;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends ActionBarActivity {
private Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar= (Toolbar)findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
NavigationDrawerFragment drawerFragment = (NavigationDrawerFragment)
getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
drawerFragment.setUp(R.id.fragment_navigation_drawer,(DrawerLayout)findViewById(R.id.drawer_layout), toolbar);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
以及我的主要 Activity xml
<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="MainActivity">
<include
android:id="@+id/app_bar"
layout="@layout/app_bar" />
</RelativeLayout>
<fragment
android:id="@+id/fragment_navigation_drawer"
android:name="com.example.xxx.xxx.NavigationDrawerFragment"
android:layout_width="@dimen/nav_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
app:layout="@layout/fragment_navigation_drawer"
tools:layout="@layout/fragment_navigation_drawer" />
</android.support.v4.widget.DrawerLayout>
有什么原因导致此错误仅出现在 KitKat 而不是 Lollipop 上?据我所知,我的导入都是 android.support.v4 或 .v7,应该适用于 4.4。
最佳答案
也许现在回答已经太晚了,你已经解决了它,但作为回应,我最近遇到了同样的问题,事实证明,我的风格中的 'Theme.AppCompat...' 导致pre lollipop 上的 inflate 异常并将其更改为“android:Theme.Holo...”使问题消失。
对于解决方案,您应该对样式中选定的主题进行一些操作。
希望有帮助。
关于java - 4.4.4 上的 android.view.InflateException 而 5.0 则没问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27913659/