如何修复java错误中的错误?

时间:2014-12-10 05:47:31

标签: java android

我正在创建一个Android项目,我正在创建一个导航抽屉。创建时我遇到了1个错误,我在下面用java代码描述了这个错误。 以下是我的MainActivity.java代码

    package com.example.laga;

    import android.support.v7.app.ActionBarActivity;
    import android.os.Bundle;
    import android.app.Activity;
    import android.content.res.Configuration;
    import android.support.v4.app.ActionBarDrawerToggle;
    import android.support.v4.view.GravityCompat;
    import android.support.v4.widget.DrawerLayout;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.ArrayAdapter;
    import android.widget.ListView;
    import android.widget.TextView;
    import android.widget.Toast;



    public class MainActivity extends ActionBarActivity {

        private String[] drawerListViewItems;
        private DrawerLayout drawerLayout;
        private ListView drawerListView;
        private ActionBarDrawerToggle actionBarDrawerToggle;


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

         // get list items from strings.xml
            drawerListViewItems = getResources().getStringArray(R.array.items);
            // get ListView defined in activity_main.xml
            drawerListView = (ListView) findViewById(R.id.left_drawer);

            // Set the adapter for the list view
            drawerListView.setAdapter(new ArrayAdapter<String>(this,
                    R.layout.drawer_listview_item, drawerListViewItems));

            // 2. App Icon 
            drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

            // 2.1 create ActionBarDrawerToggle
            actionBarDrawerToggle = new ActionBarDrawerToggle(
                    this,                  /* host Activity */
                    drawerLayout,         /* DrawerLayout object */
                    R.drawable.ic_drawer,  /* nav drawer icon to replace 'Up' caret */
                    R.string.drawer_open,  /* "open drawer" description */
                    R.string.drawer_close  /* "close drawer" description */
                    );

            // 2.2 Set actionBarDrawerToggle as the DrawerListener
            drawerLayout.setDrawerListener(actionBarDrawerToggle);

            // 2.3 enable and show "up" arrow
            getActionBar().setDisplayHomeAsUpEnabled(true); 


            drawerListView.setOnItemClickListener(new DrawerItemClickListener());

        }

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

        @Override
        public void onConfigurationChanged(Configuration newConfig) {
            super.onConfigurationChanged(newConfig);
            actionBarDrawerToggle.onConfigurationChanged(newConfig);
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {

             // call ActionBarDrawerToggle.onOptionsItemSelected(), if it returns true
            // then it has handled the app icon touch event

            if (actionBarDrawerToggle.onOptionsItemSelected(item)) {
                return true;
            }
            return super.onOptionsItemSelected(item);
        }

        private class DrawerItemClickListener implements ListView.OnItemClickListener {
            @Override
            public void onItemClick(AdapterView parent, View view, int position, long id) {
                Toast.makeText(MainActivity.this, ((TextView)view).getText(), Toast.LENGTH_LONG).show();
                drawerLayout.closeDrawer(drawerListView);

            }
        }


        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }

    }

上面的代码显示了像

这样的错误
Error
 ic_drawer cannot be resolved or is not a field

任何人都可以帮我解决这个错误。请

3 个答案:

答案 0 :(得分:1)

 R.drawable.ic_drawer  

您的ic_drawer文件夹中需要一个名为res/drawable的可绘制内容。由于您目前没有,因此出现错误。

答案 1 :(得分:1)

首先检查可绘制文件夹中的ic_drawer图像是否可用。如果没有,那么把它放在可绘制的文件夹中并清洁&amp;重建它。

享受。

答案 2 :(得分:0)

确保您的 res / drawable 文件夹中有一个名为ic_drawer的图片。 您甚至可以使用位于可绘制文件夹中的现有图像更改该可绘制内容,例如:

 actionBarDrawerToggle = new ActionBarDrawerToggle(
                    this,                  /* host Activity */
                    drawerLayout,         /* DrawerLayout object */
                    R.drawable.ic_launcher,  /* nav drawer icon to replace 'Up' caret */
                    R.string.drawer_open,  /* "open drawer" description */
                    R.string.drawer_close  /* "close drawer" description */
                    );

不要忘记导入正确的R文件

import com.example.laga.R

希望它能帮助你ツ