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?