导航抽屉图标丢失

时间:2015-03-07 18:22:08

标签: android

我的应用目前有一种奇怪的行为

当我第一次跑步时没问题,但点击几下后,导航抽屉图标就消失了。

enter image description here

enter image description here

我的代码

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

            mTitle = mDrawerTitle = getTitle();
            menu_array = getResources().getStringArray(R.array.menu_array);


            GCMRegistrar.checkDevice(MainActivity.this);
            GCMRegistrar.checkManifest(MainActivity.this);

            final String regId = GCMRegistrar.getRegistrationId(MainActivity.this);
            if (regId.equals(""))
            {
                GCMRegistrar.register(MainActivity.this, SENDER_ID);
            }
            else
            {
                Log.v("GCMTest", "Ya registrado");
            }

            setDrawer();

            getActionBar().setDisplayHomeAsUpEnabled(true);
            getActionBar().setHomeButtonEnabled(true);

            mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.drawable.ic_navigation_drawer, R.string.drawer_open, R.string.drawer_close)
            {
                public void onDrawerClosed(View view)
                {
                    getActionBar().setTitle(mTitle);
                    invalidateOptionsMenu();
                }

                public void onDrawerOpened(View drawerView)
                {
                    getActionBar().setTitle(mDrawerTitle);
                    invalidateOptionsMenu();
                }
            };

            mDrawerLayout.setDrawerListener(mDrawerToggle);

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


private void setDrawer()
    {
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerList = (ListView) findViewById(R.id.left_drawer);

        mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);

        mDrawerList.setAdapter(new CustomDrawerAdapter(this, R.layout.drawer_list_item, menu_array));
        mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
    }

public class CustomDrawerAdapter extends ArrayAdapter<DrawerItem>
{

    Context context;
    String[] drawerItemList;
    int layoutResID;
    private TypedArray imgs;

    public CustomDrawerAdapter(Context context, int layoutResourceID, String[] listItems)
    {
        super(context, layoutResourceID);
        this.context = context;
        this.drawerItemList = listItems;
        this.layoutResID = layoutResourceID;
        imgs = context.getResources().obtainTypedArray(R.array.random_imgs);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        // TODO Auto-generated method stub

        DrawerItemHolder drawerHolder;
        View view = convertView;

        if (view == null)
        {
            LayoutInflater inflater = ((Activity) context).getLayoutInflater();
            drawerHolder = new DrawerItemHolder();

            view = inflater.inflate(layoutResID, parent, false);
            drawerHolder.ItemName = (TextView) view.findViewById(R.id.text1);
            drawerHolder.icon = (ImageView) view.findViewById(R.id.imageView1);

            view.setTag(drawerHolder);

        }
        else
        {
            drawerHolder = (DrawerItemHolder) view.getTag();

        }


        drawerHolder.icon.setImageResource(imgs.getResourceId(position, -1));
        drawerHolder.ItemName.setText(drawerItemList[position]);
        imgs.recycle();
        return view;
    }

    private static class DrawerItemHolder
    {
        TextView ItemName;
        ImageView icon;
    }
}


<string-array name="menu_array">
        <item>Puntos de carga</item>
        <item>Mi SUBE</item>
        <item>Configuración</item>
        <item>Acerca de</item>
    </string-array>
    <string-array name="random_imgs">
        <item>@drawable/menu_puntos</item>
        <item>@drawable/ic_card</item>
        <item>@drawable/user</item>
        <item>@drawable/menu_acerca</item>
    </string-array>

感谢您的帮助!!!!

1 个答案:

答案 0 :(得分:1)

问题在于您设置/保存/回收图像的方式:

使用此行imgs = context.getResources().obtainTypedArray(R.array.random_imgs);,您可以将图片加载到TypedArray,以便它们可以显示。 但是一旦imgs.recycle();运行所有这些都被回收并停止出现。

所以建议将它们初始化为另一个int数组并安全地回收TypedArray

更改行:

  1. private TypedArray imgs;private int imgs [];

  2. imgs = context.getResources().obtainTypedArray(R.array.random_imgs);
    imgs = initializeImages();

  3. drawerHolder.icon.setImageResource(imgs.getResourceId(position, -1));来     drawerHolder.icon.setImageResource(imgs[position]);

  4. 完全删除imgs.recycle();

  5. 添加此方法:

    private int[] initializeImages() {
       TypedArray ar = context.getResources().obtainTypedArray(R.array.random_imgs);
       int len = ar.length();
       int[] resIds = new int[len];
       for (int i = 0; i < len; i++) {
          resIds[i] = ar.getResourceId(i, 0);
       } 
       ar.recycle();
       return resIds;
    }
    
  6. xml string-array应保持原样。


  7. 另一种选择是在java代码中设置drawable,而不是在xml中设置。

    例如

    private int imgs [] = {R.drawable.menu_puntos,
         R.drawable.ic_card, 
         R.drawable.user, 
         R.drawable.menu_acerca 
        };