二进制XML文件行#18:使用ActionbarSherlock在旧版本中输出类错误

时间:2014-05-06 08:08:03

标签: android android-layout actionbarsherlock

我正在使用Navigation DrawerActionBarSherLock来支持Api 8中的Slider。它在Android版本上运行良好> 4.0
对于较低版本,我在android 2.3.6上进行了测试,显示错误。

MainActivity.java:

import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.mytestbuddy.careermirror.NavDrawerListAdapter;
import com.mytestbuddy.careermirror.NavDrawerItem;

import java.util.ArrayList;

import android.content.res.Configuration;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.widget.DrawerLayout;
import android.util.Log;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

public class MainActivity extends SherlockFragmentActivity {

    private DrawerLayout mDrawerLayout;
    private ListView mDrawerList;
    private ActionBarDrawerToggle mDrawerToggle;

    // nav drawer title
    private CharSequence mDrawerTitle;

    // used to store app title
    private CharSequence mTitle;

    // slide menu items
    private String[] navMenuTitles;
    private TypedArray navMenuIcons;

    private ArrayList<NavDrawerItem> navDrawerItems;
    private NavDrawerListAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mTitle = mDrawerTitle = getTitle();

        // load slide menu items
        navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);

        // nav drawer icons from resources
        navMenuIcons = getResources()
                .obtainTypedArray(R.array.nav_drawer_icons);

        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerList = (ListView) findViewById(R.id.list_slidermenu);

        navDrawerItems = new ArrayList<NavDrawerItem>();

        // adding nav drawer items to array
        // Home
        navDrawerItems.add(new NavDrawerItem(navMenuTitles[0], navMenuIcons
                .getResourceId(0, -1)));
        // Profile
        navDrawerItems.add(new NavDrawerItem(navMenuTitles[1], navMenuIcons
                .getResourceId(1, -1)));
        // Logout
        navDrawerItems.add(new NavDrawerItem(navMenuTitles[2], navMenuIcons
                .getResourceId(2, -1)));

        // Recycle the typed array
        navMenuIcons.recycle();

        mDrawerList.setOnItemClickListener(new SlideMenuClickListener());

        // setting the nav drawer list adapter
        adapter = new NavDrawerListAdapter(getApplicationContext(),
                navDrawerItems);
        mDrawerList.setAdapter(adapter);

        // enabling action bar app icon and behaving it as toggle button
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);

        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
                R.drawable.ic_drawer, // nav menu toggle icon
                R.string.app_name, // nav drawer open - description for
                                    // accessibility
                R.string.app_name // nav drawer close - description for
                                    // accessibility
        ) {
            public void onDrawerClosed(View view) {
                getSupportActionBar().setTitle(mTitle);
                // calling onPrepareOptionsMenu() to show action bar icons
                supportInvalidateOptionsMenu();
            }

            public void onDrawerOpened(View drawerView) {
                getSupportActionBar().setTitle(mDrawerTitle);
                // calling onPrepareOptionsMenu() to hide action bar icons
                supportInvalidateOptionsMenu();
            }
        };
        mDrawerLayout.setDrawerListener(mDrawerToggle);

        if (savedInstanceState == null) {
            // on first time display view for first nav item
            displayView(0);
        }
    }

    /**
     * Slide menu item click listener
     * */
    private class SlideMenuClickListener implements
            ListView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
            // display view for selected nav drawer item
            displayView(position);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

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

    /* *
     * Called when invalidateOptionsMenu() is triggered
     */
    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        // if nav drawer is opened, hide the action items
        boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
        // menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
        return super.onPrepareOptionsMenu(menu);
    }

    private void displayView(int position) {
        // update the main content by replacing fragments
        Fragment fragment = null;
        switch (position) {
        case 0:
            fragment = new Dashboard();
            break;
        case 1:
            fragment = new Profile();
            break;
        case 2:
            fragment = new Logout();
            break;

        default:
            break;
        }

        if (fragment != null) {
            FragmentManager fragmentManager = getSupportFragmentManager();
            fragmentManager.beginTransaction()
                    .replace(R.id.frame_container, fragment).commit();

            // update selected item and title, then close the drawer
            mDrawerList.setItemChecked(position, true);
            mDrawerList.setSelection(position);
            setTitle(navMenuTitles[position]);
            mDrawerLayout.closeDrawer(mDrawerList);
        } else {
            // error in creating fragment
            Log.e("MainActivity", "Error in creating fragment");
        }
    }

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

    /**
     * When using the ActionBarDrawerToggle, you must call it during
     * onPostCreate() and onConfigurationChanged()...
     */

    @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 toggls
        mDrawerToggle.onConfigurationChanged(newConfig);
    }
}

Logcat:

05-06 13:33:09.546: E/AndroidRuntime(25482): FATAL EXCEPTION: main
05-06 13:33:09.546: E/AndroidRuntime(25482): android.view.InflateException: Binary XML file line #18: Error inflating class <unknown>
05-06 13:33:09.546: E/AndroidRuntime(25482):    at android.view.LayoutInflater.createView(LayoutInflater.java:518)
05-06 13:33:09.546: E/AndroidRuntime(25482):    at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
05-06 13:33:09.546: E/AndroidRuntime(25482):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
05-06 13:33:09.546: E/AndroidRuntime(25482):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
05-06 13:33:09.546: E/AndroidRuntime(25482):    at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
05-06 13:33:09.546: E/AndroidRuntime(25482):    at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
05-06 13:33:09.546: E/AndroidRuntime(25482):    at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
05-06 13:33:09.546: E/AndroidRuntime(25482):    at com.mytestbuddy.careermirror.NavDrawerListAdapter.getView(NavDrawerListAdapter.java:47)
05-06 13:33:09.546: E/AndroidRuntime(25482):    at android.widget.AbsListView.obtainView(AbsListView.java:1592)
05-06 13:33:09.546: E/AndroidRuntime(25482):    at android.widget.ListView.makeAndAddView(ListView.java:1782)
05-06 13:33:09.546: E/AndroidRuntime(25482):    at android.widget.ListView.fillSpecific(ListView.java:1325)
05-06 13:33:09.546: E/AndroidRuntime(25482):    at android.widget.ListView.layoutChildren(ListView.java:1623)
05-06 13:33:09.546: E/AndroidRuntime(25482):    at android.widget.AbsListView.onLayout(AbsListView.java:1422)
05-06 13:33:09.546: E/AndroidRuntime(25482):    at android.view.View.layout(View.java:7175)
05-06 13:33:09.546: E/AndroidRuntime(25482):    at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:672)
05-06 13:33:09.546: E/AndroidRuntime(25482):    at android.view.View.layout(View.java:7175)
05-06 13:33:09.546: E/AndroidRuntime(25482):    at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
05-06 13:33:09.546: E/AndroidRuntime(25482):    at android.view.View.layout(View.java:7175)
05-06 13:33:09.546: E/AndroidRuntime(25482):    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
05-06 13:33:09.546: E/AndroidRuntime(25482):    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130)
05-06 13:33:09.546: E/AndroidRuntime(25482):    at android.widget.LinearLayout.onLayout(LinearLayout.java:1047)
05-06 13:33:09.546: E/AndroidRuntime(25482):    at android.view.View.layout(View.java:7175)
05-06 13:33:09.546: E/AndroidRuntime(25482):    at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
05-06 13:33:09.546: E/AndroidRuntime(25482):    at android.view.View.layout(View.java:7175)
05-06 13:33:09.546: E/AndroidRuntime(25482):    at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
05-06 13:33:09.546: E/AndroidRuntime(25482):    at android.view.View.layout(View.java:7175)
05-06 13:33:09.546: E/AndroidRuntime(25482):    at android.view.ViewRoot.performTraversals(ViewRoot.java:1146)
05-06 13:33:09.546: E/AndroidRuntime(25482):    at android.view.ViewRoot.handleMessage(ViewRoot.java:1865)
05-06 13:33:09.546: E/AndroidRuntime(25482):    at android.os.Handler.dispatchMessage(Handler.java:99)
05-06 13:33:09.546: E/AndroidRuntime(25482):    at android.os.Looper.loop(Looper.java:130)
05-06 13:33:09.546: E/AndroidRuntime(25482):    at android.app.ActivityThread.main(ActivityThread.java:3687)
05-06 13:33:09.546: E/AndroidRuntime(25482):    at java.lang.reflect.Method.invokeNative(Native Method)
05-06 13:33:09.546: E/AndroidRuntime(25482):    at java.lang.reflect.Method.invoke(Method.java:507)
05-06 13:33:09.546: E/AndroidRuntime(25482):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
05-06 13:33:09.546: E/AndroidRuntime(25482):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
05-06 13:33:09.546: E/AndroidRuntime(25482):    at dalvik.system.NativeStart.main(Native Method)
05-06 13:33:09.546: E/AndroidRuntime(25482): Caused by: java.lang.reflect.InvocationTargetException
05-06 13:33:09.546: E/AndroidRuntime(25482):    at java.lang.reflect.Constructor.constructNative(Native Method)
05-06 13:33:09.546: E/AndroidRuntime(25482):    at java.lang.reflect.Constructor.newInstance(Constructor.java:415)
05-06 13:33:09.546: E/AndroidRuntime(25482):    at android.view.LayoutInflater.createView(LayoutInflater.java:505)
05-06 13:33:09.546: E/AndroidRuntime(25482):    ... 35 more
05-06 13:33:09.546: E/AndroidRuntime(25482): Caused by: java.lang.UnsupportedOperationException: Can't convert to dimension: type=0x2
05-06 13:33:09.546: E/AndroidRuntime(25482):    at android.content.res.TypedArray.getDimensionPixelSize(TypedArray.java:463)
05-06 13:33:09.546: E/AndroidRuntime(25482):    at android.view.View.<init>(View.java:2108)
05-06 13:33:09.546: E/AndroidRuntime(25482):    at android.widget.TextView.<init>(TextView.java:390)
05-06 13:33:09.546: E/AndroidRuntime(25482):    at android.widget.TextView.<init>(TextView.java:383)
05-06 13:33:09.546: E/AndroidRuntime(25482):    ... 38 more

activity_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/frame_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <ListView
        android:id="@+id/list_slidermenu"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@color/list_background"
        android:choiceMode="singleChoice"
        android:divider="@color/list_divider"
        android:dividerHeight="1dp"
        android:listSelector="@drawable/list_selector" />

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

NavDrawerListAdapter.java:

import java.util.ArrayList;
import android.app.Activity;
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 NavDrawerListAdapter extends BaseAdapter {

    private Context context;
    private ArrayList<NavDrawerItem> navDrawerItems;

    public NavDrawerListAdapter(Context context,
            ArrayList<NavDrawerItem> navDrawerItems) {
        this.context = context;
        this.navDrawerItems = navDrawerItems;
    }

    @Override
    public int getCount() {
        return navDrawerItems.size();
    }

    @Override
    public Object getItem(int position) {
        return navDrawerItems.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            LayoutInflater mInflater = (LayoutInflater) context
                    .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            convertView = mInflater.inflate(R.layout.drawer_list_item, null);
        }

        ImageView imgIcon = (ImageView) convertView.findViewById(R.id.icon);
        TextView txtTitle = (TextView) convertView.findViewById(R.id.title);

        imgIcon.setImageResource(navDrawerItems.get(position).getIcon());
        txtTitle.setText(navDrawerItems.get(position).getTitle());

        return convertView;
    }
}

NavDrawerListAdapter.java:47 就是这一行

convertView = mInflater.inflate(R.layout.drawer_list_item, null);

0 个答案:

没有答案