在导航抽屉活动中更改图片和文本

时间:2017-11-05 16:37:36

标签: android xml

我想在您创建新的导航抽屉活动时以编程方式更改下拉图像和文本。我试过这个:(假设currentUser.firstName不为空。

NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);


    TextView message  = (TextView) navigationView.findViewById(R.id.welcomeTextview);
    message.setText("Hello " + currentUser.firstName.toString());

    ImageView profilePicbtn = (ImageView)navigationView.findViewById(R.id.imageViewProfile);
    profilePicbtn.setBackgroundResource(R.drawable.nat_profile);

我做了'navigationView.findviewById()'因为imageView和TextView在nav_header_dashboard.xml中。当我运行它时,它会崩溃,如果我删除它运行的TextView和ImageView声明。我只想更改顶部的图片,并在单击下拉菜单时提供问候消息。还假设图像'nat_profile'退出。

这些是我的文件:

Dashboard.java

import android.graphics.Color;
import android.graphics.PorterDuff;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import java.util.*;
import android.content.Intent;
import android.widget.ImageView;
import android.widget.TextView;

public class Dashboard extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {


 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_dashboard);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    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 navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

    //testing lazaro
    //TextView message  = (TextView) navigationView.findViewById(R.id.welcomeTextview);
    //message.setText("Hello " + currentUser.firstName.toString());

    ImageView profilePicbtn = (ImageView)navigationView.findViewById(R.id.imageViewProfile);
    profilePicbtn.setBackgroundResource(R.drawable.nat_profile);

activity_dashboard.xml:

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

<include
    layout="@layout/app_bar_dashboard"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_dashboard"
    app:menu="@menu/activity_dashboard_drawer" />

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

nav_header_dashboard.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="@dimen/nav_header_height"
android:background="@drawable/side_nav_bar"
android:gravity="bottom"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:theme="@style/ThemeOverlay.AppCompat.Dark">

<ImageView
    android:id="@+id/imageViewProfile"
    android:layout_width="90dp"
    android:layout_height="90dp"
    android:paddingTop="@dimen/nav_header_vertical_spacing"
    app:srcCompat="@drawable/nat_profile" />

<TextView
    android:id="@+id/welcomeTextview"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="@dimen/nav_header_vertical_spacing"
    android:textAppearance="@style/TextAppearance.AppCompat.Body1" />

</LinearLayout>

这是我在values文件夹中的strings.xml:

<resources>
   <string name="app_name">Life Management</string>
   <string name="title_activity_login">Sign in</string>

   <!-- Strings related to login -->
   <string name="prompt_email">Email</string>
   <string name="prompt_password">Password (optional)</string>
   <string name="action_sign_in">Sign in or register</string>
   <string name="action_sign_in_short">Sign in</string>
   <string name="error_invalid_email">This email address is invalid</string>
   <string name="error_invalid_password">This password is too short</string>
   <string name="error_incorrect_password">This password is incorrect</string>
   <string name="error_field_required">This field is required</string>
   <string name="permission_rationale">"Contacts permissions are needed for providing email
    completions."
   </string>
   <string name="title_activity_dashboard">DashboardActivity</string>

<string name="navigation_drawer_open">Open navigation drawer</string>
<string name="navigation_drawer_close">Close navigation drawer</string>

<string name="action_settings">Settings</string>


<string name="action_signout">Sign Out</string>
<string name="action_joy">JOY</string>
<string name="action_passion">PASSION</string>
<string name="action_givingback">GIVING BACK</string>
<string name="section_format">Hello World from section: %1$d</string>
<string name="title_activity_previous_cycle">Life Management</string>
<string name="title_home">Home</string>
<string name="title_dashboard">Dashboard</string>
<string name="title_notifications">Notifications</string>
</resources>

崩溃报告:

11-05 10:37:32.621 2890-2890/com.example.seniorprojectfall.test E/AndroidRuntime: FATAL EXCEPTION: main
                                                                              Process: com.example.seniorprojectfall.test, PID: 2890
                                                                              java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.seniorprojectfall.test/com.example.seniorprojectfall.test.Dashboard}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
                                                                                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
                                                                                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
                                                                                  at android.app.ActivityThread.-wrap11(Unknown Source:0)
                                                                                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:105)
                                                                                  at android.os.Looper.loop(Looper.java:164)
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:6541)
                                                                                  at java.lang.reflect.Method.invoke(Native Method)
                                                                                  at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
                                                                               Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
                                                                                  at **com.example.seniorprojectfall.test.Dashboard.onCreate(Dashboard.java:354)**
                                                                                  at android.app.Activity.performCreate(Activity.java:6975)
                                                                                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
                                                                                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
                                                                                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) 
                                                                                  at android.app.ActivityThread.-wrap11(Unknown Source:0) 
                                                                                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593) 
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:105) 
                                                                                  at android.os.Looper.loop(Looper.java:164) 
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:6541) 
                                                                                  at java.lang.reflect.Method.invoke(Native Method) 
                                                                                  at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 

Dashboard.java:354)是行:message.setText(“Hello”+ currentUser.firstName.toString());

2 个答案:

答案 0 :(得分:1)

更改

TextView message = (TextView) navigationView.findViewById(R.id.welcomeTextview);

To(我添加了getHeaderView

TextView message = (TextView) navigationView.getHeaderView(0).findViewById(R.id.welcomeTextview);

也为第二个textVew

执行此操作

答案 1 :(得分:0)

您可以尝试查找导航抽屉标题布局,如下所示

for i in "ls"; do if [[$i ==*"cat ids.txt"*]]; then "unzip $i" fi; done 

而不是

View headerLayout = navigationView.getHeaderView(0);

然后你可以找到像这样的文本视图

NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);