You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Firebase注册功能点击TextView触发IllegalStateException崩溃问题

Fixing Firebase Registration Crash: NullPointerException on TextView.getText()

Hey there, let's get that registration crash sorted out! From your error logs, the root cause is crystal clear:

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.widget.TextView.getText()' on a null object reference at com.example.supdude.appcomidita.registroUsuarios.onClick(registroUsuarios.java:65)

This means when your code tries to call getText() on your "Done" TextView at line 65, that TextView object hasn't been properly initialized—it's null. Here's how to fix this step by step:

Step 1: Double-check your TextView's ID match

First, make sure the ID you're using in your Java code matches exactly with what's in your XML layout file. Even a tiny typo (like uppercase vs lowercase) will make findViewById() return null.

  • In your layout XML, confirm the "Done" TextView has an ID like <TextView android:id="@+id/tv_done" ... />
  • In registroUsuarios.java, you must reference it as findViewById(R.id.tv_done)—no typos allowed!

Step 2: Initialize the TextView after loading the layout

You can't get a reference to a view until you've inflated the layout. Make sure your onCreate() method follows this order:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.your_registration_layout); // Load the layout FIRST
    // Now initialize your TextView
    TextView doneTextView = findViewById(R.id.tv_done);
}

If you call findViewById() before setContentView(), it will always return null.

Step 3: Fix your click event handling

It looks like you're using android:onClick="onClick" in your XML to bind the click. Here are two solid fixes:

Option A: Safeguard the onClick method

Ensure your onClick method is public, accepts a View parameter, and checks for null before accessing the TextView:

public void onClick(View view) {
    TextView doneTv = findViewById(R.id.tv_done);
    if (doneTv != null) {
        // Safely get the text now
        String doneText = doneTv.getText().toString();
        // Proceed with your Firebase registration logic
        FirebaseAuth.getInstance().createUserWithEmailAndPassword(...);
    }
}

Option B: Use a programmatic click listener (more reliable)

Ditch the XML android:onClick binding and set the listener directly in onCreate()—this gives you more control and avoids potential binding issues:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.your_registration_layout);
    
    TextView doneTextView = findViewById(R.id.tv_done);
    doneTextView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // TextView is guaranteed to be non-null here
            String doneText = doneTextView.getText().toString();
            // Execute Firebase registration
            registerUser();
        }
    });
}

private void registerUser() {
    // Your Firebase Auth and Database registration logic goes here
}

Step 4: Extra check if using Fragments (if applicable)

If registroUsuarios were a Fragment, you'd need to initialize views in onViewCreated() instead of onCreate(), since the Fragment's view hierarchy isn't ready in onCreate(). But based on your class name, this is likely an Activity, so this is just a bonus tip!

Once you fix the TextView initialization issue, that NullPointerException will disappear, and your registration flow should work as expected.

内容的提问来源于stack exchange,提问作者Eduardo Moreno

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 07:25:53