Error performing findviewbyid

2

Hello, I want to find the id of two Textviews, but they are returning NULL. Here is my code:

   private TextView nomeUser;
private TextView cargoUser;

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

    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.addDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

    menuTec menuTec = new menuTec();
    FragmentManager manager = getSupportFragmentManager();
    manager.beginTransaction().replace(R.id.content,menuTec, menuTec.getTag()).commit();


   nomeUser = (TextView) findViewById(R.id.nomeFuncAlterar);
    cargoUser = (TextView) findViewById(R.id.cargoFuncAlterar);

    Intent intent = getIntent();

        if(intent != null) {
            String nome = intent.getStringExtra("chave1");
            String cargo = intent.getStringExtra("chave2");

            nomeUser.setText(nome);
            cargoUser.setText(cargo);
        }
    }

When I do:

nomeUser = (TextView) findViewById(R.id.nomeFuncAlterar);
cargoUser = (TextView) findViewById(R.id.cargoFuncAlterar);

username and chargeUSer are given NULL.

And my XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="@dimen/nav_header_height"
    android:background="@color/azulgerdau"
    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"
    android:weightSum="1">

    <de.hdodenhof.circleimageview.CircleImageView
        android:id="@+id/profile"
        android:layout_width="65dp"
        android:layout_height="65dp"
        android:paddingTop="@dimen/nav_header_vertical_spacing"
        android:src="@drawable/profile" />

    <TextView
        android:id="@+id/nomeFuncAlterar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/nav_header_vertical_spacing"
        android:text="@string/colaborador"
        android:textAppearance="@style/TextAppearance.AppCompat.Body1"
        android:textSize="18sp" />

    <TextView
        android:id="@+id/cargoFuncAlterar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/cargo"
        android:textSize="18sp" />

</LinearLayout>

And the following error appears in the log:

Process: com.example.gerdaumanagement.gerdaumanagement, PID: 14528                                                                                            java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.gerdaumanagement.gerdaumanagement/com.example.gerdaumanagement.gerdaumanagement.MenuDrawer}: 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:2665)                                                                                             
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)                                                                                             
   at android.app.ActivityThread.-wrap12(ActivityThread.java)                                                                                             
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)                                                                                             
   at android.os.Handler.dispatchMessage(Handler.java:102)                                                                                             
   at android.os.Looper.loop(Looper.java:154)                                                                                             
   at android.app.ActivityThread.main(ActivityThread.java:6119)                                                                                             
   at java.lang.reflect.Method.invoke(Native Method)                                                                                             
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)                                                                                             
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)                                                                                             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.gerdaumanagement.gerdaumanagement.MenuDrawer.onCreate(MenuDrawer.java:58)                                                                                             
   at android.app.Activity.performCreate(Activity.java:6679)                                                                                             
   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)                                                                                             
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)                                                                                             
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)                                                                                              
   at android.app.ActivityThread.-wrap12(ActivityThread.java)                                                                                              
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 

Can anyone help me?

    
asked by anonymous 27.06.2017 / 17:11

1 answer

1

It looks like you're calling the method through XML:

<Button
  android:id="@+id/altera_perfil"
  android:layout_weight="match_parent"
  android:layout_width="wrap_content"
  android:onClick="AlterarPerfil" />

You can not do this because the method expects parameters (String name, String charge)

You need to do it in the conventional way:

Button altera_perfil = (Button) findViewById(R.id.altera_perfil);
altera_perfil.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

              //Aqui vc chama o método passando os parâmetros

            }
        });

And take the android: onClick there in the button layout

    
27.06.2017 / 17:19