导航栏的Onclick监听器

时间:2018-01-26 16:10:13

标签: java android

我使用java和android studio开发了一个android应用程序。但是当我单击导航抽屉中的项目时,我希望它显示一个对话框,但没有任何反应。此外,当我尝试在抽屉中设置文本时,应用程序崩溃。我是android开发的新手。拜托,我非常感谢帮助。

以下是我的代码:

private TextView name;
public TextView email;
public TextView contact;
private DrawerLayout drawerLayout;
AlertDialog.Builder builder;
private ActionBarDrawerToggle actionBarDrawerToggle;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    drawerLayout=(DrawerLayout) findViewById(R.id.drawer);
    builder=new AlertDialog.Builder(MainActivity.this);
    name=(TextView) findViewById(R.id.myname);
    email=(TextView) findViewById(R.id.myemail);
    contact=(TextView) findViewById(R.id.mycontact);

    actionBarDrawerToggle=new ActionBarDrawerToggle(this, drawerLayout,R.string.open,R.string.close);
    drawerLayout.addDrawerListener(actionBarDrawerToggle);
    actionBarDrawerToggle.syncState();
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    //RECEIVING THE INTENT
    /* Get values from Intent
    Intent intent = getIntent();
    String myname = intent.getStringExtra("dname");
    String myemail = intent.getStringExtra("demail");
    String mycontact = intent.getStringExtra("dcontact");

    name.setText(myname);
    email.setText(myemail);
    contact.setText(mycontact);

    builder.setTitle("Log In Successful");
    builder.setMessage("Welcome, "+myname);
    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            //Toast.makeText(getApplicationContext(),"Clock Out Cancelled",Toast.LENGTH_SHORT).show();
        }
    });
    AlertDialog alertDialog=builder.create();
    alertDialog.show();*/
    //name.setText(response);
    /*try {
        JSONObject jsonObject = new JSONObject(fname);
        String fstname=jsonObject.getString("clockTym");
        String lstname=jsonObject.getString("timediff");
        String demail=jsonObject.getString("penalty");
        String dcontact=jsonObject.getString("tot_penalty");

        name.setText(fstname+" "+lstname);
        email.setText(demail);
        contact.setText(dcontact);

    } catch (JSONException e) {
        e.printStackTrace();
    }*/

}

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

public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();

    if (id == R.id.cin){
        // Handle the camera action
        builder.setTitle("Log In Successful");
        builder.setMessage("Welcome, ");
        builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                //Toast.makeText(getApplicationContext(),"Clock Out Cancelled",Toast.LENGTH_SHORT).show();
            }
        });
        AlertDialog alertDialog=builder.create();
        alertDialog.show();
    } else if (id == R.id.db) {
        builder.setTitle("Log In Successful");
        builder.setMessage("Welcome, ");
        builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                //Toast.makeText(getApplicationContext(),"Clock Out Cancelled",Toast.LENGTH_SHORT).show();
            }
        });
        AlertDialog alertDialog=builder.create();
        alertDialog.show();
    } else if (id == R.id.permit) {
        builder.setTitle("Log In Successful");
        builder.setMessage("Welcome, ");
        builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                //Toast.makeText(getApplicationContext(),"Clock Out Cancelled",Toast.LENGTH_SHORT).show();
            }
        });
        AlertDialog alertDialog=builder.create();
        alertDialog.show();
    } else if (id == R.id.cout) {

    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}
}

1 个答案:

答案 0 :(得分:1)

您没有初始化实际包含列表项的导航抽屉,尽管您已经拥有onNavigationItemSelected侦听器,因此在oncreate内初始化导航视图并附加监听器

NavigationView navView = (NavigationView) findViewById(R.id.your_nav_id);
navView.setNavigationItemSelectedListener(this);