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
@adventistaam had ... but not changed at all.
– Lucas Charles
The Alterarperfil function is in the same Activity?
– adventistaam
@adventistaam is not, is in another Ragment. the Menu Drawer
– Lucas Charles
try to use
(TextView) view.findViewById(R.id.nomeFuncAlterar);
– adventistaam
No no, no error. @adventistaam
– Lucas Charles
this Drawer Menu is another screen?
– adventistaam
@adventist Yes.
– Lucas Charles
Let’s go continue this discussion in chat.
– adventistaam
@Lucascharles I see no evidence of error in
findviewbyid
and yes in theintent.getStringExtra("chave1")
who may be comingnull
. You can take a test by puttingString nome = " "+intent.getStringExtra("chave1");
– viana
@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.)
– Lucas Charles
I removed the tag again
android-studio
because the question has nothing to do with the used IDE.– ramaral
Is that a fragment?! Has onCreateView ?
– viana
@acklay got here, Thank you so much:) The problem was that it was in my navigationView.
– Lucas Charles
@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.
– viana