I am trying to create a user profile section in my app. In my Login Activity, I ask for the username from FirebaseAUTH.getCurrentUser().getDisplayName() and save it in a string.
In my xml file for the main activity, I put a textView with a string containing a placeholder.
I tried both in the Login and the Main Activity Java files to .setText .format etc, but the text simply doesnt change..
Any hints on how to solve this?
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
private HomeFragment hf;
private WebViewFragment wvf;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
firebaseAuth = FirebaseAuth.getInstance();
if (firebaseAuth.getCurrentUser().getDisplayName() != null) {
String name = firebaseAuth.getCurrentUser().getDisplayName();
final TextView profile = (TextView)findViewById(R.id.profile_section);
profile.setText("Test");
}
E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.vreeni.firebaseauthentiction, PID: 6060 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.vreeni.firebaseauthentiction/com.example.vreeni.firebaseauthentication.MainActivity}: 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:2822) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2897) at android.app.ActivityThread.-wrap11(Unknown Source:0) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1598) at android.os.Handler.dispatchMessage(Handler.java:105) at android.os.Looper.loop(Looper.java:251) at android.app.ActivityThread.main(ActivityThread.java:6563) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 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.vreeni.firebaseauthentication.MainActivity.onCreate(MainActivity.java:52) at android.app.Activity.performCreate(Activity.java:6975) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)