gpt4 book ai didi

android - 将布局中textview中的文本动态设置为header Layout

转载 作者:行者123 更新时间:2023-11-29 20:02:44 24 4
gpt4 key购买 nike

我有这个 main_activity 布局,它在我的 navigationView 中使用另一个布局作为 headerlayout。我想以动态方式更改用作 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中的文本动态设置为header Layout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36051599/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com