android.view.InflateException:二进制XML文件行#2:错误类膨胀

时间:2015-01-07 14:59:04

标签: android layout android-fragments actionbarsherlock android-inflate

尝试在姜饼2.3中运行此代码时出错。该代码在软糖中运行良好。上面的代码有一个名为Sherlock的支持库。我仍然无法用生姜面包来帮助我吗?

完成日志

01-07 20:26:21.399: E/AndroidRuntime(6339): FATAL EXCEPTION: main
01-07 20:26:21.399: E/AndroidRuntime(6339): android.view.InflateException: Binary XML file line #2: Error inflating class <unknown>
01-07 20:26:21.399: E/AndroidRuntime(6339):     at android.view.LayoutInflater.createView(LayoutInflater.java:518)
01-07 20:26:21.399: E/AndroidRuntime(6339):     at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
01-07 20:26:21.399: E/AndroidRuntime(6339):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
01-07 20:26:21.399: E/AndroidRuntime(6339):     at android.view.LayoutInflater.inflate(LayoutInflater.java:386)
01-07 20:26:21.399: E/AndroidRuntime(6339):     at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
01-07 20:26:21.399: E/AndroidRuntime(6339):     at com.androidbegin.sidemenututorial.MenuListAdapter.getView(MenuListAdapter.java:51)
01-07 20:26:21.399: E/AndroidRuntime(6339):     at android.widget.AbsListView.obtainView(AbsListView.java:1610)
01-07 20:26:21.399: E/AndroidRuntime(6339):     at android.widget.ListView.makeAndAddView(ListView.java:1795)
01-07 20:26:21.399: E/AndroidRuntime(6339):     at android.widget.ListView.fillDown(ListView.java:718)
01-07 20:26:21.399: E/AndroidRuntime(6339):     at android.widget.ListView.fillFromTop(ListView.java:775)
01-07 20:26:21.399: E/AndroidRuntime(6339):     at android.widget.ListView.layoutChildren(ListView.java:1646)
01-07 20:26:21.399: E/AndroidRuntime(6339):     at android.widget.AbsListView.onLayout(AbsListView.java:1440)
01-07 20:26:21.399: E/AndroidRuntime(6339):     at android.view.View.layout(View.java:7175)
01-07 20:26:21.399: E/AndroidRuntime(6339):     at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:672)
01-07 20:26:21.399: E/AndroidRuntime(6339):     at android.view.View.layout(View.java:7175)
01-07 20:26:21.399: E/AndroidRuntime(6339):     at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
01-07 20:26:21.399: E/AndroidRuntime(6339):     at android.view.View.layout(View.java:7175)
01-07 20:26:21.399: E/AndroidRuntime(6339):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
01-07 20:26:21.399: E/AndroidRuntime(6339):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130)
01-07 20:26:21.399: E/AndroidRuntime(6339):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1047)
01-07 20:26:21.399: E/AndroidRuntime(6339):     at android.view.View.layout(View.java:7175)
01-07 20:26:21.399: E/AndroidRuntime(6339):     at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
01-07 20:26:21.399: E/AndroidRuntime(6339):     at android.view.View.layout(View.java:7175)
01-07 20:26:21.399: E/AndroidRuntime(6339):     at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
01-07 20:26:21.399: E/AndroidRuntime(6339):     at android.view.View.layout(View.java:7175)
01-07 20:26:21.399: E/AndroidRuntime(6339):     at android.view.ViewRoot.performTraversals(ViewRoot.java:1146)
01-07 20:26:21.399: E/AndroidRuntime(6339):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1866)
01-07 20:26:21.399: E/AndroidRuntime(6339):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-07 20:26:21.399: E/AndroidRuntime(6339):     at android.os.Looper.loop(Looper.java:130)
01-07 20:26:21.399: E/AndroidRuntime(6339):     at android.app.ActivityThread.main(ActivityThread.java:3687)
01-07 20:26:21.399: E/AndroidRuntime(6339):     at java.lang.reflect.Method.invokeNative(Native Method)
01-07 20:26:21.399: E/AndroidRuntime(6339):     at java.lang.reflect.Method.invoke(Method.java:507)

MainActivity.java

import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.MenuItem;

import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.Fragment;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.support.v4.view.GravityCompat;

public class MainActivity extends SherlockFragmentActivity {

    // Declare Variables
    DrawerLayout mDrawerLayout;
    ListView mDrawerList;
    ActionBarDrawerToggle mDrawerToggle;
    MenuListAdapter mMenuAdapter;
    String[] title;
    String[] subtitle;
    int[] icon;
    Fragment fragment1 = new Fragment1();
    Fragment fragment2 = new Fragment2();
    Fragment fragment3 = new Fragment3();
    private CharSequence mDrawerTitle;
    private CharSequence mTitle;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Get the view from drawer_main.xml
        setContentView(R.layout.drawer_main);

        // Get the Title
        mTitle = mDrawerTitle = getTitle();

        // Generate title
        title = new String[] { "Title Fragment 1", "Title Fragment 2",
                "Title Fragment 3" };

        // Generate subtitle
        subtitle = new String[] { "Subtitle Fragment 1", "Subtitle Fragment 2",
                "Subtitle Fragment 3" };

        // Generate icon
        icon = new int[] { R.drawable.action_about, R.drawable.action_settings,
                R.drawable.collections_cloud };

        // Locate DrawerLayout in drawer_main.xml
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

        // Locate ListView in drawer_main.xml
        mDrawerList = (ListView) findViewById(R.id.listview_drawer);

        // Set a custom shadow that overlays the main content when the drawer
        // opens
        mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
                GravityCompat.START);

        // Pass string arrays to MenuListAdapter
        mMenuAdapter = new MenuListAdapter(MainActivity.this, title, subtitle,
                icon);

        // Set the MenuListAdapter to the ListView
        mDrawerList.setAdapter(mMenuAdapter);

        // Capture listview menu item click
        mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

        // Enable ActionBar app icon to behave as action to toggle nav drawer
        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        // ActionBarDrawerToggle ties together the the proper interactions
        // between the sliding drawer and the action bar app icon
        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
                R.drawable.ic_drawer, R.string.drawer_open,
                R.string.drawer_close) {

            public void onDrawerClosed(View view) {
                // TODO Auto-generated method stub
                super.onDrawerClosed(view);
            }

            public void onDrawerOpened(View drawerView) {
                // TODO Auto-generated method stub
                // Set the title on the action when drawer open
                getSupportActionBar().setTitle(mDrawerTitle);
                super.onDrawerOpened(drawerView);
            }
        };

        mDrawerLayout.setDrawerListener(mDrawerToggle);

        if (savedInstanceState == null) {
            selectItem(0);
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        if (item.getItemId() == android.R.id.home) {

            if (mDrawerLayout.isDrawerOpen(mDrawerList)) {
                mDrawerLayout.closeDrawer(mDrawerList);
            } else {
                mDrawerLayout.openDrawer(mDrawerList);
            }
        }

        return super.onOptionsItemSelected(item);
    }

    // ListView click listener in the navigation drawer
    private class DrawerItemClickListener implements
            ListView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
            selectItem(position);
        }
    }

    private void selectItem(int position) {

        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        // Locate Position
        switch (position) {
        case 0:
            ft.replace(R.id.content_frame, fragment1);
            break;
        case 1:
            ft.replace(R.id.content_frame, fragment2);
            break;
        case 2:
            ft.replace(R.id.content_frame, fragment3);
            break;
        }
        ft.commit();
        mDrawerList.setItemChecked(position, true);

        // Get the title followed by the position
        setTitle(title[position]);
        // Close drawer
        mDrawerLayout.closeDrawer(mDrawerList);
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        // Sync the toggle state after onRestoreInstanceState has occurred.
        mDrawerToggle.syncState();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        // Pass any configuration change to the drawer toggles
        mDrawerToggle.onConfigurationChanged(newConfig);
    }

    @Override
    public void setTitle(CharSequence title) {
        mTitle = title;
        getSupportActionBar().setTitle(mTitle);
    }
}

Menulistadapter.java

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class MenuListAdapter extends BaseAdapter {

    // Declare Variables
    Context context;
    String[] mTitle;
    String[] mSubTitle;
    int[] mIcon;
    LayoutInflater inflater;

    public MenuListAdapter(Context context, String[] title, String[] subtitle,
            int[] icon) {
        this.context = context;
        this.mTitle = title;
        this.mSubTitle = subtitle;
        this.mIcon = icon;
    }

    @Override
    public int getCount() {
        return mTitle.length;
    }

    @Override
    public Object getItem(int position) {
        return mTitle[position];
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        // Declare Variables
        TextView txtTitle;
        TextView txtSubTitle;
        ImageView imgIcon;

        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View itemView = inflater.inflate(R.layout.drawer_list_item, parent,
                false);

        // Locate the TextViews in drawer_list_item.xml
        txtTitle = (TextView) itemView.findViewById(R.id.title);
        txtSubTitle = (TextView) itemView.findViewById(R.id.subtitle);

        // Locate the ImageView in drawer_list_item.xml
        imgIcon = (ImageView) itemView.findViewById(R.id.icon);

        // Set the results into TextViews
        txtTitle.setText(mTitle[position]);
        txtSubTitle.setText(mSubTitle[position]);

        // Set the results into ImageView
        imgIcon.setImageResource(mIcon[position]);

        return itemView;
    }

}

drawer_main.xml

<android.support.v4.widget.DrawerLayout    
xmlns:android="http://schemas.android.com
/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<ListView
    android:id="@+id/listview_drawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="#111"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp" />

   </android.support.v4.widget.DrawerLayout>

drawer_list.xml

  <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 style="?attr/spinnerDropDownItemStyle"
 android:layout_width="match_parent"
 android:layout_height="?attr/dropdownListPreferredItemHeight"
 android:background="?android:attr/activatedBackgroundIndicator"
 android:orientation="horizontal" >

 <ImageView
    android:id="@+id/icon"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:adjustViewBounds="true" />

 <LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:gravity="center_vertical|left"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/title"
        style="?attr/spinnerDropDownItemStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:singleLine="true" />

    <TextView
        android:id="@+id/subtitle"
        style="?attr/spinnerDropDownItemStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:singleLine="true"
        android:textAppearance="?attr/textAppearanceSmall" />
  </LinearLayout>

  </LinearLayout>

1 个答案:

答案 0 :(得分:0)

如果您尝试支持Android版本较小的v11,则需要在drawer_list.xml文件中删除这些行

uSupVec.add((Vector) uSup);

这些代码仅支持版本11或更高版本。

uSupVec.add(new Vector(uSup)); 添加到drawer_list.xml文件

解决方案请按此链接进行正确解决 https://stackoverflow.com/a/20123120/5761017