导航抽屉在扩展到另一个Android类时不会打开

时间:2015-04-07 03:45:27

标签: android drawer

我将NavigationObject类扩展到其他活动类,但是当我点击菜单时抽屉不会出现。

这是我的NavigationObject:

public class NavigationObject extends ActionBarActivity implements OnClickListener{
private DrawerLayout mDrawerLayout;
private ArrayAdapter<String> mAdapter;
private ActionBarDrawerToggle mDrawerToggle;
private String mActivityTitle;
ListView listView;
List<RowItem> rowItems;


protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Firebase.setAndroidContext(this);
    setContentView(R.layout.navdrawer); 

    mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
    mActivityTitle = getTitle().toString();

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);
    getSupportActionBar().setIcon(R.drawable.ic_launcher);
    System.out.println("ERROR 1");
    addDrawerItems();
    setupDrawer();
  } 


public boolean onCreateOptionsMenu(Menu menu) {      
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.my_menu, menu);
    return super.onCreateOptionsMenu(menu);
  }


//junk
   public void addDrawerItems() {
   System.out.println("ERROR 2");
    String[] drawer_Array = { "User Profile", "Setting", "Contact us", "Help", "Logout" };
    String[] desc_Array = { "Private", "Change settings", "Ask us anything", "Simple Guide", "Logs out user" };
    Integer[] image_Array = { R.drawable.user, R.drawable.setting, R.drawable.contactme, R.drawable.help, R.drawable.logout };

    //mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, drawer_Array);
    //mDrawerList.setAdapter(mAdapter);
    rowItems = new ArrayList<RowItem>();
    for (int i = 0; i < drawer_Array.length; i++) {
        RowItem item = new RowItem(image_Array[i], drawer_Array[i], desc_Array[i]);
        rowItems.add(item);
    }
    System.out.println("ERROR 3");
    listView = (ListView) findViewById(R.id.navList);
    CustomDrawerBaseAdapter adapter = new CustomDrawerBaseAdapter(this, rowItems);
    listView.setAdapter(adapter);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            if(position == 0){
                Intent toProfile = new Intent(getApplicationContext(), userprofActivity.class);
                startActivity(toProfile);
                //Toast.makeText(getMsgActivity.this, "Time for an upgrade!", Toast.LENGTH_SHORT).show();   
            }
            else if(position == 1){
                //Toast.makeText(getMsgActivity.this, "Time for an Wake!", Toast.LENGTH_SHORT).show();
            }
            else if(position == 2){
                //Toast.makeText(getMsgActivity.this, "Time for an Eat!", Toast.LENGTH_SHORT).show();
            }
            else if(position == 3){
                //Toast.makeText(getMsgActivity.this, "Time for an Dance!", Toast.LENGTH_SHORT).show();
            }
            else if(position == 4){
                //Toast.makeText(getMsgActivity.this, "Time for an Sleep!", Toast.LENGTH_SHORT).show();
            }   
        }
    });

    System.out.println("ERROR 4");
  }

     public void setupDrawer() {
        //getActionBar().setDisplayHomeAsUpEnabled(true);
        //getActionBar().setHomeButtonEnabled(true);
   System.out.println("ERROR 5");
        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.drawer_open, R.string.drawer_close) {

            // enabling action bar app icon and behaving it as toggle button

                public void onDrawerOpened(View drawerView) {
                    System.out.println("ERROR 5");
                    super.onDrawerOpened(drawerView);
                    listView.bringToFront();
                    mDrawerLayout.requestLayout();
                    getSupportActionBar().setTitle("User Menu Option");
                    invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
                }

                public void onDrawerClosed(View view) {
                    System.out.println("ERROR 6");
                    super.onDrawerClosed(view);
                    getSupportActionBar().setTitle(mActivityTitle);
                    invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
                }
        };
        System.out.println("ERROR 6.5");
        mDrawerToggle.setDrawerIndicatorEnabled(true);
        mDrawerLayout.setDrawerListener(mDrawerToggle);
    }

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

    /* Called whenever we call invalidateOptionsMenu() */
    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        System.out.println("ERROR 7");
        // If the nav drawer is open, hide action items related to the content view
        boolean drawerOpen = mDrawerLayout.isDrawerOpen(listView);
        menu.findItem(R.id.action_write).setVisible(!drawerOpen);
        menu.findItem(R.id.action_grabmsg).setVisible(!drawerOpen);
        menu.findItem(R.id.action_mydiary).setVisible(!drawerOpen);
        return super.onPrepareOptionsMenu(menu);
    }



    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        // TODO Auto-generated method stub
        super.onConfigurationChanged(newConfig);
        mDrawerToggle.onConfigurationChanged(newConfig);
        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            System.out.println("landscape");

        } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
            System.out.println("portrait");
        }
    } 
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        //bundle = getIntent().getExtras();
        //String language_chosen = bundle.getString("lang_pick");

        if (id == R.id.action_write) {
            Intent writeNewMsg = new Intent(getApplicationContext(), writeMsgActivity.class);
            //writeNewMsg.putExtra("lang_pick", language_chosen);
            startActivity(writeNewMsg);
        }
        else if(id == R.id.action_grabmsg){
            Intent grabNewMsg = new Intent(getApplicationContext(), getMsgActivity.class);
            //grabNewMsg.putExtra("lang_pick", language_chosen);
            finish();
            startActivity(grabNewMsg);

        }
        else if(id == R.id.action_mydiary){

        }

        // Activate the navigation drawer toggle
        if (mDrawerToggle.onOptionsItemSelected(item)) {
            System.out.println("ERROR 8"); //This is where the error is. It is returned but never used.
            return true;
        }

        return super.onOptionsItemSelected(item);
    }


    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

    }
}

我的问题在这里:

            // Activate the navigation drawer toggle
        if (mDrawerToggle.onOptionsItemSelected(item)) {
            System.out.println("ERROR 8"); //This is where the error is. It is returned but never used.
            return true;
        }

这里从未调用过这些方法:

                    public void onDrawerOpened(View drawerView) {
                    System.out.println("ERROR 5.5");
                    super.onDrawerOpened(drawerView);
                    listView.bringToFront();
                    mDrawerLayout.requestLayout();
                    getSupportActionBar().setTitle("User Menu Option");
                    invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
                }

                public void onDrawerClosed(View view) {
                    System.out.println("ERROR 6");
                    super.onDrawerClosed(view);
                    getSupportActionBar().setTitle(mActivityTitle);
                    invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
                }

这是抽屉的xml,称为NavigationObject类的navdrawer.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ListView
    android:id="@+id/navList"
    android:layout_width="200dp"
    android:layout_height="match_parent"
    android:layout_gravity="left|start"
    android:background="#ffeeeeee"/> 

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

更新 - 仍然需要帮助

这是我在其他类上使用extend的地方(这个类在xml中没有抽屉xml,因为我继承自NavigationObject,它有自己的xml,名为navdrawer):

public class MainActivity extends NavigationObject implements OnClickListener{
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Firebase.setAndroidContext(this);
    setContentView(R.layout.get_msg);   

问题:每次我点击抽屉上的选项菜单栏时,它会从NavigationObject类打印“8”,但在此之后它什么都不做。代码中没有错误,因为除了System.out.println(“ERROR 5.5”)和System.out.println(“ERROR 6”)之外,我放置的所有系统输出都用于打开和关闭抽屉。滑动不起作用。

当我在MainActivity活动类中推送所有NavigationObject类时,这是有效的,但是现在当我将抽屉作为一个单独的类(NavigationObject类)进行继承时它不起作用。

菜单栏工作正常。但是在“ERROR 8”中选择的选项并没有返回任何内容,但仍然打印出来,所以它正在工作,但它也没有做任何事情。

非常感谢任何帮助,这是我的最后一年项目,很快就会到期:)。

1 个答案:

答案 0 :(得分:0)

在派生类中设置不同的布局(R.layout.get_msg)。此布局是否包含NavigationDrawer?如果没有 - 那就是你的错误。

修改

试试这个:将NavigationDrawer的初始化移动到基本活动中的单独方法,如下所示:

public class NavigationObject extends ActionBarActivity ...

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Firebase.setAndroidContext(this);
}

protected void initNavigationDrawer() {
    mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
    mActivityTitle = getTitle().toString();

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);
    getSupportActionBar().setIcon(R.drawable.ic_launcher);
    System.out.println("ERROR 1");
    addDrawerItems();
    setupDrawer();
} 

在MainActivity中,在设置内容视图后,在onCreate()方法中调用init方法。

public class MainActivity extends NavigationObject ...

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.get_msg); // <-- must contain drawer_layout
    initNavigationDrawer();
}