Marco
Marco

Reputation: 23

LayoutInflater and setText doesn't work together

I'm using my Android project activity ( Navigation Activity Drawer ) . When I want to change the title at the top in the side menu , it is not working . I used a LayoutInflater to read the ID from the XML right , but nothing . this is my code :

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 sunlight return this error with your code

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) 

the solution is this . Thank you @tiny sunlight

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

Upvotes: 2

Views: 937

Answers (2)

tiny sunlight
tiny sunlight

Reputation: 6251

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

will create a new View.You should use the view in NavigationView.

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

I don't really know why I should getHeaderView(0).I guess the headerLayout hasn't been added to activity before the NavigationView shows.

Upvotes: 1

Shady Atef
Shady Atef

Reputation: 2401

You are inflating a layout while there is another one displayed inflated via setContentView().

your code does change the title for an object in memory that's not displayed.

Assuming R.id.txtNomeCognome is a text view in layout R.layout.activity_dashboard you can use findViewById() to get your view object and set the text on it

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

Upvotes: 0

Related Questions