-1

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)

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Vr33ni
  • 101
  • 16
  • Do you want to change a string resource? It's not possible. But you can change the text in a TextView or an EditText. – Phantômaxx Dec 29 '17 at 21:36
  • yes thats what I wanted..Hmmm, how would I do that? I want to have a section in the navigation drawer saying Username followed by the username that can be retrieved from firebaseAuth.getCurrentUser().getDisplayName()... Thanks for your help! – Vr33ni Dec 30 '17 at 01:24
  • Check is your textview is with same ID in XML file, as defined here or not ? – Aditya Dec 30 '17 at 04:38
  • It is the same, yes. I also tried it with the text being a placeholder and there not even being a text, but same result. – Vr33ni Dec 30 '17 at 07:10
  • You can use a separate txt file to store "dynamic" strings. Or a database table. – Phantômaxx Dec 30 '17 at 09:18

1 Answers1

0

You can find a simple example right at the top of this resource.

 public class MainActivity extends Activity {

    protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
         final TextView helloTextView = (TextView) findViewById(R.id.text_view_id);
         helloTextView.setText(R.string.user_greeting);
     }
 }

If you still have problems, please provide your code.

Yuliwee
  • 327
  • 1
  • 11