Error while performing findviewbyid

Asked

Viewed 1,838 times

2

Hello, I want to find the id of two Textviews, but they are returning NULL. Follows 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);

nameUser and cargoUSer receive 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) 

Someone can help me?

  • There is a space between menuDrawer. AlterarPerfil ?

  • @adventistaam had ... but not changed at all.

  • The Alterarperfil function is in the same Activity?

  • @adventistaam is not, is in another Ragment. the Menu Drawer

  • try to use (TextView) view.findViewById(R.id.nomeFuncAlterar);

  • No no, no error. @adventistaam

  • this Drawer Menu is another screen?

  • @adventist Yes.

  • @Lucascharles I see no evidence of error in findviewbyid and yes in the intent.getStringExtra("chave1") who may be coming null. You can take a test by putting String nome = " "+intent.getStringExtra("chave1");

  • @acklay When I debug the code, the String name is correct. But findviewbyid is null. Since it’s a Fragment (I believe it is) you wouldn’t have to have a view.findviewbyid or a getActivity(). findview by id? But it’s not working, because there is no inflate in that Fragment (Android’s own Drawer menu.)

  • I removed the tag again android-studio because the question has nothing to do with the used IDE.

  • Is that a fragment?! Has onCreateView ?

  • @acklay got here, Thank you so much:) The problem was that it was in my navigationView.

  • @Lucascharles yourself can answer your own question when you find an answer that is different from the answers you already have. Most of the time when the question is not complete enough, it is difficult to find a concrete answer. There is no way to reproduce the error. Good luck there.

Show 10 more comments

1 answer

1


It seems that you are calling the method via XML:

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

You cannot do this because the method expects parameters (String name,String cargo)

You need to do it 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 takes the android:onClick there on the button layout

  • android:onClick calls another function, and within that function has the call from Alterarperfil. Enter the code in the question.

  • I did not understand why I was negative, the error is explicit in the Log: Could not execute method for android:onClick

  • I don’t know either. But Onclick doesn’t call the profile change directly.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.