我有这个main_activity布局,在我的NavigationView中使用另一个布局作为headerLayout。我想以动态方式更改用作标题布局的textview文本。在我的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)



您能帮我解决这个问题吗?
顺便说一句,为我的英语不好而感到抱歉。我希望你明白这一点。

最佳答案

Main_activity.xml中,为NavigationView提供ID(例如android:id="@+id/navigation"

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");

关于android - 在布局的textview中动态设置文本作为标题Layout,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36051599/

10-11 22:40
查看更多