Java 我的文本视图在android studio中不工作
我创建了一个活动,并将数据封装到另一个类中,从该类中,我无法将数据检索到另一个类的文本字段中。下面是代码,请帮我解决。当我运行应用程序时,屏幕上什么也看不到 Name.javaJava 我的文本视图在android studio中不工作,java,android-studio,Java,Android Studio,我创建了一个活动,并将数据封装到另一个类中,从该类中,我无法将数据检索到另一个类的文本字段中。下面是代码,请帮我解决。当我运行应用程序时,屏幕上什么也看不到 Name.java package com.example.voiceprescription; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity;
package com.example.voiceprescription;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.icu.text.IDNA;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.Toast;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import java.util.ArrayList;
import java.util.Locale;
public class Name extends AppCompatActivity {
EditText et_Nname,et_Nage,et_Nphone;
RadioButton rb_Nmale,rb_Nfemale;
Button btn_Nnext;enter code here
ImageButton ib_Nvoice;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_name);
et_Nphone=findViewById(R.id.et_Nphone);
et_Nname=findViewById(R.id.et_Nname);
et_Nage=findViewById(R.id.et_Nage);
ib_Nvoice=findViewById(R.id.ib_Nvoice);
rb_Nfemale=findViewById(R.id.rb_Nfemale);
rb_Nmale=findViewById(R.id.rb_Nmale);
btn_Nnext=findViewById(R.id.btn_Nnext);
btn_Nnext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name=et_Nname.getText().toString().trim();
String age=et_Nage.getText().toString().trim();
String phone=et_Nphone.getText().toString().trim();
PersonInfo personInfo=new PersonInfo();
personInfo.setName(name);
personInfo.setAge(age);
personInfo.setPhone(phone);
Intent intent=new Intent(Name.this,Symptoms.class);
startActivity(intent);
}
});
}
}
Personinfo.java
package com.example.voiceprescription;
public class PersonInfo {
private String name;
private String age;
private String phone;
private String gender;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
}
symptoms.java
package com.example.voiceprescription;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class Symptoms extends AppCompatActivity {
TextView tv1;
PersonInfo personInfo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_symptoms);
personInfo=new PersonInfo();
tv1=findViewById(R.id.tv1);
tv1.setText(personInfo.getName());
}
}
你所做的事情不是这样的。要做的是将对象作为bundle参数传递给symptoms活动
Bundle bundle = new Bundle();
bundle.putSerializable(“value”,您的“person”\u class\u obj);
意向。额外支出(捆绑)
为此,类必须实现可序列化接口
public class PersonInfo implements Serializable {
}
您的代码不起作用,因为您在一个活动中设置了对象,而在第二个活动中该对象不可用。
参考-您的应用程序的主要活动是什么?您正在尝试启动的哪项活动最终为空?你真的没有在屏幕上看到任何东西吗?还是仅仅在点击
Name
activity上的按钮后,Symptoms
activity就为空了?我的主要活动只有3个按钮。我正在尝试启动的症状活动。是的,结果是空白的,它只有一个文本字段,而且它正在变为空白。实际上,当我不使用按钮时,会显示文本字段,但当我通过intent启动它时,它会变为空白。