当我尝试从包中恢复我的 activity 时,textView returns 空指针

textView returns null pointer when I try to restore my activity from a bundle

我正在尝试使用保存的实例状态方法来保存文本视图的内容,但它一直返回空指针异常(LogCat:尝试尝试调用虚拟方法'void android.widget.TextView.setText(java.lang.CharSequence)'在空对象引用上)。该应用程序在启动时运行良好,但由于某种原因,当 activity 被销毁并恢复时(IE 旋转屏幕时)textView 为空

 public class MainActivity extends AppCompatActivity {
    private TextView mTextView;

    //other fields
    public static final int MAX_SEATS = 6;
    int numSeats;
    String resultText = "0";
    static final String KEY_RESULT_STRING = "result string";
    static final String KEY_NUM_SEATS = "numberSeats";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if(savedInstanceState != null){
            resultText = savedInstanceState.getString(KEY_RESULT_STRING);
            numSeats = savedInstanceState.getInt(KEY_NUM_SEATS);
            displayResult();
        }
        //variables for widgets
        mTextView = findViewById(R.id.textView);
        //next two lines display "where would you like to sit" on screen
        TextView mIntro = findViewById(R.id.intro);
        mIntro.setText(R.string.introduction);
        EditText mNumRequestedSeats = findViewById(R.id.seatAmountNumberEdit);

        Button reserve = (Button) findViewById(R.id.submit_button);
        reserve.setOnClickListener(view -> {
            //EditText mNumRequestedSeats = findViewById(R.id.seatAmountNumberEdit);
            try{
                String requestedSeats = mNumRequestedSeats.getText().toString();
                numSeats = Integer.parseInt(requestedSeats);
            }
            catch(NumberFormatException wrongSeat){
                Toast.makeText(getApplicationContext(),R.string.wrongFormatError, Toast.LENGTH_SHORT).show();
                numSeats = 0;
            }
            displayResult();
        });
    }
        private void displayResult(){
            if (numSeats > 6){
                resultText = "Please dial 123-456-7890 for parties larger than 6";
            }else {
                resultText = "Thank you for your reservation.";
            }
            mTextView.setText(resultText);
        }
    @Override
    public void onSaveInstanceState(Bundle savedInstanceState){
        savedInstanceState.putString(KEY_RESULT_STRING, resultText);
        savedInstanceState.putInt(KEY_NUM_SEATS, numSeats);
        super.onSaveInstanceState(savedInstanceState);
    }
}

这是布局文件:

<div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre><code>    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/numberOfSeats"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

        <TextView
            android:id="@+id/intro"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="16dp"
            android:text="@string/introduction"
            android:textAlignment="center"
            android:textAppearance="@style/TextAppearance.AppCompat.Medium"
            android:textColor="@color/dark_blue"
            android:textSize="20sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            tools:ignore="MissingConstraints">

        </TextView>

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="64dp"
            android:contentDescription="@string/waiter_image"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.497"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/submit_button"
            app:srcCompat="@drawable/waiter" />

        <Button
            android:id="@+id/submit_button"
            style="@style/Widget.MaterialComponents.Button.TextButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:backgroundTint="@color/button_grey"
            android:text="@string/mainActivitySubmit"
            android:textColor="@color/black"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/intro"
            tools:ignore="MissingConstraints" />

        <TextView
            android:id="@+id/seatAmountPrompt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="20dp"
            android:layout_marginTop="32dp"
            android:layout_marginEnd="130dp"
            android:text="@string/seatAmount"
            android:textColor="@color/dark_blue"
            android:textSize="16sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/imageView" />

        <EditText
            android:id="@+id/seatAmountNumberEdit"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="306dp"
            android:ems="3"
            android:inputType="number"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/seatAmountPrompt" />

        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="64dp"
            android:text="TextView"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/seatAmountNumberEdit" />

    </androidx.constraintlayout.widget.ConstraintLayout>

我已尝试使用默认字符串值初始化 textView,但当我更改方向时它仍然返回空指针。我究竟做错了什么? 注意:我知道在布局中使用类似 textFreeze 的东西可能更容易 XML 但这是进入更复杂项目的跳板所以我想学习正确实现保存的包。

请参考:https://developer.android.com/guide/components/activities/activity-lifecycle.html#java

您缺少 public void onRestoreInstanceState(Bundle savedInstanceState) 函数。

更新的答案

我终于有机会正确查看代码了。问题是您在调用 displayResult() 的 之后放置了 findViewById() 代码 TextView 组件尚未初始化/连接,因此它们仍然是 nullonRestoreState() 工作的原因是因为它在 Activity 视图创建后被调用。 在 onCreateView().

内的所有其他内容之后重新定位此代码
if(savedInstanceState != null){
            resultText = savedInstanceState.getString(KEY_RESULT_STRING);
            numSeats = savedInstanceState.getInt(KEY_NUM_SEATS);
            displayResult();
        }

为了补充@Sovathna Hong 提出的出色观点,它起作用的原因是因为随后调用了 non-overwritten 方法。重要的是我们要了解 onRestoreState() 在这两种情况下都会被调用,在 if(savedInstanceState != null).

的情况下,当我们没有明确覆盖它时,它只是被自动调用