动态地将布局中textview中的文本设置为headerLayout

时间:2016-03-17 04:19:35

标签: android textview

我有这个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)

你可以帮我解决这个问题吗? 顺便说一下,我的英语不好。我希望你明白这一点。

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

你可以试试这个

class CreateUserProfiles < ActiveRecord::Migration
  def change
    create_table :user_profiles do |t|
      t.string :username, null: false
      t.string :biography, default: ""

      t.references :user, index: true, foreign_key: true

      t.timestamps null: false
    end
    add_index :user_profiles, [:user_id, :username]
  end
end