LayoutInflater和setText不能一起工作

时间:2016-01-23 14:01:21

标签: android

我正在使用我的Android项目活动(导航活动抽屉)。当我想更改侧面菜单顶部的标题时,它无法正常工作。我使用LayoutInflater从XML中读取ID,但没有。这是我的代码:

public class Dashboard extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

private static final String EXTRAS_NOME = "nome";
private static final String EXTRAS_COGNOME = "cognome";
private static final String EXTRAS_CODICE = "codice";


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_dashboard);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });

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


    LayoutInflater inflater = (LayoutInflater)this.getSystemService (Context.LAYOUT_INFLATER_SERVICE);
    View view = inflater.inflate(R.layout.nav_header_dashboard, null);
    TextView textView = (TextView) view.findViewById(R.id.txtNomeCognome);
    textView.setText("hi"); //The text does not change. Why?


}

activity_dashboard.xml

<?xml version="1.0" encoding="utf-8"?>

<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" />

nav_header_dashboard.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="@dimen/nav_header_height"
android:background="@color/colorAccent"
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/imageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingTop="@dimen/nav_header_vertical_spacing"
    android:src="@android:drawable/sym_def_app_icon" />

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

<TextView
    android:id="@+id/omega"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

@tiny sunshine使用您的代码返回此错误

01-23 14:26:33.640 3272-3272/? E/AndroidRuntime: FATAL EXCEPTION: main
                                             Process: com.application.andreamarconicola.progettoandroidam, PID: 3272
                                             java.lang.RuntimeException: Unable to start activity ComponentInfo{com.application.andreamarconicola.progettoandroidam/com.application.andreamarconicola.progettoandroidam.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:2416)
                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                 at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                 at android.os.Handler.dispatchMessage(Handler.java:102)
                                                 at android.os.Looper.loop(Looper.java:148)
                                                 at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                              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.application.andreamarconicola.progettoandroidam.Dashboard.onCreate(Dashboard.java:53)
                                                 at android.app.Activity.performCreate(Activity.java:6237)
                                                 at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                 at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                 at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                 at android.os.Looper.loop(Looper.java:148) 
                                                 at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                 at java.lang.reflect.Method.invoke(Native Method) 
                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

解决方案就是这样。谢谢@tiny阳光

//Thanks @tiny sunlight ... now work
    TextView textView = (TextView) navigationView.getHeaderView(0).findViewById(R.id.txtNomeCognome);
    textView.setText("ciao");

2 个答案:

答案 0 :(得分:1)

View view = inflater.inflate(R.layout.nav_header_dashboard, null);

将创建一个新的View。您应该使用NavigationView中的视图。

TextView textView =(TextView)navigationView.getHeaderView(0).findViewById(R.id.txtNomeCognome);

我真的不知道为什么我应该得到HeaderView(0)。我猜想在NavigationView显示之前,headerLayout还没有被添加到活动中。

答案 1 :(得分:0)

当您通过setContentView()显示另一个布局时,您正在给布局充气。

您的代码确实更改了内存中未显示的对象的标题。

假设R.id.txtNomeCognome是布局R.layout.activity_dashboard 中的文字视图,您可以使用findViewById()获取您的视图对象并在其上设置文字

TextView textView = (TextView) findViewById(R.id.txtNomeCognome);