简体   繁体   中英

Dynamicly settext in textview in layout as a headerLayout

i have this main_activity layout that use another layout as headerlayout inside my navigationView. i want to change textview text that being use as headerlayout in dynamically way.using this in my main_activity :

DrawerLayout drawerLayout;
TextView textview;
Toolbar toolbar;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    toolbar = (Toolbar)findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    drawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
    TextView t = (TextView)findViewById(R.id.name);
    t.setText("Some String Here");


}

Main_activity.xml

<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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:id="@+id/drawer_layout"
>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >

    <include
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        layout="@layout/toolbar_layout"
        />
</LinearLayout>

<android.support.design.widget.NavigationView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/navigation_view"
    android:layout_gravity="start"
    app:menu="@menu/drawer_menu"
    app:headerLayout="@layout/navigation_drawer_header"
    >

</android.support.design.widget.NavigationView>

navigation_drawer_header.xml

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="180dp"
android:background="#a3b1b2"
>

<ImageView
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:src="@drawable/photo"
    android:layout_margin="35dp"
    android:id="@+id/img"
    />

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Ismail Zakky"
    android:layout_toRightOf="@+id/img"
    android:textSize="20dp"
    android:textStyle="bold"
    android:layout_marginTop="50dp"
    android:id="@+id/name"
    />

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="ismailzakky@yahoo.com"
    android:layout_toRightOf="@+id/img"
    android:textSize="15dp"
    android:textStyle="bold"
    android:layout_marginTop="20dp"
    android:layout_below="@+id/name"
    android:id="@+id/email"
    /></RelativeLayout>

LOGCAT

FATAL EXCEPTION: main
Process: com.example.zakky.navigationdrawerdemo, PID: 24867
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.zakky.navigationdrawerdemo/com.example.zakky.navigationdrawerdemo.MainActivity}: 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:2379)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2442)
        at android.app.ActivityThread.access$800(ActivityThread.java:156)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1351)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:211)
        at android.app.ActivityThread.main(ActivityThread.java:5371)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:945)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:740)
 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.zakky.navigationdrawerdemo.MainActivity.onCreate(MainActivity.java:28)
        at android.app.Activity.performCreate(Activity.java:5990)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2332)

can u help me with this problem? and btw, sory for my bad english. i hope you get the point.

In Main_activity.xml , provides an ID to NavigationView (eg. android:id="@+id/navigation" )

In onCreate() :

NavigationView navigation = (NavigationView) findViewById(R.id.navigation);
View header = navigation.getHeaderView(0);
TextView name = (TextView) header.findViewById(R.id.name);
name.setText("Your name");

You can try this

drawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
 TextView t = (TextView)drawerLayout.findViewById(R.id.name);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM