在导航抽屉上设置ImageView后应用程序关闭

时间:2017-11-30 15:43:55

标签: android android-layout textview

我尝试在代码中的导航抽屉中设置ImageViewTextview,但它会使应用程序崩溃:

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

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        setSupportActionBar(toolbar);
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();
        navigationView.setNavigationItemSelectedListener(this);
        //Image and textview in nav_bar
        View hview = navigationView.inflateHeaderView(R.layout.nav_header_main);
        CircleImageView circleImageView = (CircleImageView) findViewById(R.id.profile_image);
        TextView name = (TextView) findViewById(R.id.name);
        TextView mailid = (TextView) findViewById(R.id.mailid);
        circleImageView.setImageResource(R.mipmap.logo);
        name.setText("HI");
        mailid.setText("Hello");


        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}

1 个答案:

答案 0 :(得分:0)

尝试使用以下代码更改代码:

CircleImageView circleImageView = (CircleImageView) hview.findViewById(R.id.profile_image);
TextView name = (TextView) hview.findViewById(R.id.name);
TextView mailid = (TextView) hview.findViewById(R.id.mailid);