这个问题在这里已有答案:
我正在尝试在我的应用中创建用户个人资料部分。在我的登录活动中,我要求FirebaseAUTH.getCurrentUser()。getDisplayName()中的用户名并将其保存在字符串中。
在我的主活动的xml文件中,我将textView与包含占位符的字符串放在一起。
我在Login和Main Activity Java文件中尝试了.setText .format等,但文本根本没有改变..
关于如何解决这个问题的任何提示?
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进程:com.example.vreeni.firebaseauthentiction,PID:6060 java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.vreeni.firebaseauthentiction / com.example.vreeni.firebaseauthentication.MainActivity }:java.lang.NullPointerException:尝试在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2822)上的空对象引用上调用虚方法'void android.widget.TextView.setText(java.lang.CharSequence)'在Android.app.A活动中的android.app.A活动时,Android.A.运行时,Android.A.运行时,运行android.app.A活动,运行知识。 android.app.Looper.loop(Looper.java:251)的os.Handler.dispatchMessage(Handler.java:105)位于android.app.ActivityThread.main(ActivityThread.java:6563)的java.lang.reflect.Method .invoke(Native Method)位于com.android.internal.os.Zygote $ MethodAndArgsCaller.run(Zygote.java:240)at com.andr oid.internal.os.ZygoteInit.main(ZygoteInit.java:767)引起:java.lang.NullPointerException:尝试在null上调用虚方法'void android.widget.TextView.setText(java.lang.CharSequence)'在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)的android.app.Activity.performCreate(Activity.java:6975)的com.example.vreeni.firebaseauthentication.MainActivity.onCreate(MainActivity.java:52)中的对象引用)
你可以在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);
}
}
如果您仍有问题,请提供您的密码。