在导航抽屉标题

时间:2018-01-29 15:33:04

标签: java android android-studio

我的Android应用程序中有一个导航抽屉。我已将活动中的内容传输到具有导航抽屉的活动。当我想将标题中的Textviews设置为传输的值时,应用程序会崩溃。下面是java代码的标题。请帮助,因为我是Android应用程序开发的新手。

    NavigationView navigationView=(NavigationView) findViewById(R.id.navbar);
    navigationView.setNavigationItemSelectedListener(this);
    //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+"/"+myemail+"/"+mycontact);
    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();

}

@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 :(得分:3)

确保在设置值之前正确初始化所有View。例如:

NavigationView navigationView = (NavigationView) findViewById(R.id.navbar);
View header = navigationView.getHeaderView(0);
TextView name = header.findViewById(R.id.name_text_view);
TextView email = header.findViewById(R.id.email_text_view);
TextView contact = header.findViewById(R.id.contact_text_view);

然后您可以设置您的值。

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