【发布时间】:2021-11-09 22:43:25
【问题描述】:
在这个您可以复制并粘贴到 Android Studio 中进行测试的简单代码中,有两个类。在称为 MainActivity 的第一个类中,有一个字段供用户键入名称。
在名为 MainActivity2 的第二个类中,有另一个字段显示用户在名为 MainActivity1 的第一个类中键入的内容。
但是,此代码显示错误,它无法识别 MainActivity2 中的变量“名称”,在此代码 sn-p 中:String receivedName = receiverBundle.getString("name");
请问有谁知道如何解决这个小错误?
拜托,您可以复制并粘贴这些简单的代码并在您的 Android Studio 中进行测试:
/*This code is the first Java class that there is a field where the user can write own name:*/
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
MainActivity1:
``` public class MainActivity extends AppCompatActivity {
EditText name;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name = findViewById(R.id.name);
button = findViewById(R.id.button);
String nameInString = name.getText().toString();
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent senderIntent = new Intent(getApplicationContext(), MainActivity2.class);
Bundle senderBundle = new Bundle();
senderBundle .putString("name", nameInString);
startActivity(senderIntent);
}
});
}
}
/* This class there is the field that must appear the name that the user wrote on the first class */
```
MainActivity2:
```
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity2 extends AppCompatActivity {
TextView resultName;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
resultName = findViewById(R.id.nameReceived);
Intent receiverIntent = getIntent();
Bundle receiverBundle = receiverIntent.getExtras();
String receivedName = receiverBundle.getString
("name");
resultName.setText(receivedName);
}
}
/* Front-end code of MainActivity1*/
```
activity_main.xml
```
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.645" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Complete Name:"
android:textSize="20dp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.372"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.169" />
<EditText
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Type your name"
android:inputType="textPersonName"
app:layout_constraintBottom_toTopOf="@+id/button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.501" />
</androidx.constraintlayout.widget.ConstraintLayout>
/* MainActivity2的前端代码*/
Activity_main2.xml
<TextView
android:id="@+id/nameReceived"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Received name:"
android:textSize="25dp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.476"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.307" />
</androidx.constraintlayout.widget.ConstraintLayout>```
【问题讨论】:
-
你能发布抛出的错误吗?
标签: android android-intent bundle