当我尝试从包中恢复我的 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>
我正在尝试使用保存的实例状态方法来保存文本视图的内容,但它一直返回空指针异常(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>