Android - 实现导航栏

时间:2015-06-16 08:36:04

标签: android navigation drawer

我正在尝试在我的Android应用程序中实现导航栏。但是,我的编译器抱怨有以下错误:

  

错误:(39,25)错误:找不到符号变量drawer_open

     

错误:(39,47)错误:找不到符号变量drawer_close

我的代码:

import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;



public class MainActivity extends ActionBarActivity {
    /***********************************************************
                            MEMBERS
     **********************************************************/
    private Toolbar m_toolbar;
    private String m_navTitles[] = {"Nr. 1", "Nr. 2", "Nr. 3", "Nr. 4" };
    private int m_icons[] = {R.drawable.ic_action, R.drawable.ic_action, R.drawable.ic_action, R.drawable.ic_action};
    private DrawerLayout m_drawer;
    private ActionBarDrawerToggle m_drawerToggle;

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

        /* Set toolbar */
        m_toolbar = (Toolbar) findViewById(R.id.tool_bar); //find toolbar from layout
        setSupportActionBar(m_toolbar); //call to set toolbar as the ActionBar

        /* Set navigation drawer */
        m_drawer = (DrawerLayout) findViewById(R.id.DrawerLayout); //assign drawer object to view
        m_drawerToggle = new ActionBarDrawerToggle(this, m_drawer, m_toolbar, R.string.drawer_open, R.string.drawer_close){
            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
                // code here will execute once the drawer is opened
            }

            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);
                // Code here will execute once drawer is closed
            }

        }; // made drawer toggle object
        m_drawer.setDrawerListener(m_drawerToggle);
        m_drawerToggle.syncState();               
    }

请注意我的导航抽屉代码尚未完成。我想先修复drawer_open / drawer_close错误。

非常感谢提前!

2 个答案:

答案 0 :(得分:3)

您的strings.xml中有drawer_opendrawer_close吗?

我认为你没有在字符串中添加这两个字符串。

答案 1 :(得分:0)

试试这个你的pbm在这里解决了复制这段代码并替换你的:

import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;



public class MainActivity extends ActionBarActivity {
    /***********************************************************
                            MEMBERS
     **********************************************************/
    private Toolbar m_toolbar;
    private String m_navTitles[] = {"Nr. 1", "Nr. 2", "Nr. 3", "Nr. 4" };
    private int m_icons[] = {R.drawable.ic_action, R.drawable.ic_action, R.drawable.ic_action, R.drawable.ic_action};
    private DrawerLayout m_drawer;
    private ActionBarDrawerToggle m_drawerToggle;

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

        /* Set toolbar */
        m_toolbar = (Toolbar) findViewById(R.id.tool_bar); //find toolbar from layout
        setSupportActionBar(m_toolbar); //call to set toolbar as the ActionBar

        /* Set navigation drawer */
        m_drawer = (DrawerLayout) findViewById(R.id.DrawerLayout); //assign drawer object to view
        m_drawerToggle = new ActionBarDrawerToggle(this, m_drawer, m_toolbar, "drawer_open", "drawer_close"){
            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
                // code here will execute once the drawer is opened
            }

            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);
                // Code here will execute once drawer is closed
            }

        }; // made drawer toggle object
        m_drawer.setDrawerListener(m_drawerToggle);
        m_drawerToggle.syncState();               
    }