4.4.4上的android.view.InflateException,而5.0则没问题

时间:2015-01-13 01:29:43

标签: java android android-5.0-lollipop android-4.4-kitkat

下面的代码在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>

主要活动

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);
  }
}

我的主要活动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。

1 个答案:

答案 0 :(得分:2)

对于答案你可能已经太迟了而且你已经解决了,但作为回应我最近遇到了同样的问题,结果发现&#39; Theme.AppCompat ... &#39;以我的风格导致棒棒糖前的膨胀异常并将其更改为&#39; android:Theme.Holo ... &#39;让问题消失了。

对于解决方案,您应该对样式中的所选主题执行某些操作。

希望有所帮助。