如何在导航抽屉项目上启动新活动单击它在我的代码中不起作用

时间:2015-12-16 11:07:24

标签: android navigation-drawer android-navigation-drawer

我已经提到的每个堆栈问题几乎都是每个链接所以请任何人都可以告诉我代码中的错误是什么。 以下是我的代码:

public class Attdce extends Activity{

    DrawerLayout mDrawerLayout;
    ListView mDrawerList;
    ActionBarDrawerToggle drawerListener;
    String mTitle="";
    private String[] information;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_attendance);

        mTitle=(String)getTitle();
        mDrawerLayout=(DrawerLayout)findViewById(R.id.drawer_layout);
        mDrawerList=(ListView)findViewById(R.id.drawer_list);
        information=getResources().getStringArray(R.array.Information);
        mDrawerList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, information));
        mDrawerList.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                //Updated Attdce.this instead of Attendace.this
                Toast.makeText(Attdce.this, information [position]+ " was selected", Toast.LENGTH_LONG).show();
                    selectItem(position);
            }

        });
        drawerListener=new ActionBarDrawerToggle(this, mDrawerLayout, R.drawable.ic_menu_black_24dp,R.string.drawer_open, R.string.drawer_close)
        {
            @Override
            public void onDrawerClosed(View drawerView) {
            getActionBar().setTitle(mTitle);
            invalidateOptionsMenu();
            }

            @Override
                    public void onDrawerOpened(View drawerView) {
                    getActionBar().setTitle("Select");
                    invalidateOptionsMenu();
            }
        };
        mDrawerLayout.setDrawerListener(drawerListener);
        getActionBar().setHomeButtonEnabled(true);
        getActionBar().setDisplayHomeAsUpEnabled(true);
}
public void selectItem(int position)
            {

                setTitle(information[position]);
                mDrawerList.setItemChecked(position, true);
                Intent intent1;
                 switch(position){
                 case 0:
                     intent1 = new Intent(Attdce.this, AboutUs.class);
                     startActivity(intent1);
                     break;

                 case 1:
                     intent1 = new Intent(Attdce.this, Features.class);
                     startActivity(intent1);
                     break;

                 case 2:
                     intent1 = new Intent(Attdce.this, Help.class);
                     startActivity(intent1);
                     break;

                 case 3:
                     intent1 = new Intent(Attdce.this, SMSCredits.class);
                     startActivity(intent1);
                     break;

                 case 4:
                     intent1 = new Intent(Attdce.this, ChangePassword.class);
                     startActivity(intent1);
                     break;

                 case 5:
                     intent1 = new Intent(Attdce.this, Help.class);
                     startActivity(intent1);
                     break;

                 }
            }

            public void setTitle(String title)
            {
                getActionBar().setTitle(title);
            }

@Override
    public boolean onOptionsItemSelected(MenuItem item) 
    {
         if(drawerListener.onOptionsItemSelected(item)){
                return true;
            }
        return super.onOptionsItemSelected(item);
     }

 @Override
        protected void onPostCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onPostCreate(savedInstanceState);
            drawerListener.syncState();
        }


}

ACTIVITY_ATTENDANCE.XML

<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"

        >

    <FrameLayout 
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </FrameLayout>
    <ListView 
        android:id="@+id/drawer_list"

         android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="#111"
        android:dividerHeight="1dp"
        android:background="#009688"
       android:listSelector="@drawable/list_selector"/>

    //other xml code

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

任何人都可以建议我..有什么问题???我没有得到任何错误导航抽屉显示正确,但当我选择导航抽屉项目,所以没有任何其他活动打开..

2 个答案:

答案 0 :(得分:1)

只是一个建议请开始使用android推荐的NavigationView。如果您正在搜索教程here,您可以找到如何在android中使用NavigationView的详细示例。

答案 1 :(得分:0)

尝试在startActivity(intent1)

之前关闭抽屉
case 5:
                     final intent1 = new Intent(Attdce.this, Help.class);
                                drawer.closeDrawers();
                                drawer.postDelayed(new Runnable() {
                                    @Override
                                    public void run() {
                                        startActivity(intent1 );
                                    }
                                }, 200);
                               break;