java - 我的 textview 在 android studio 中不起作用
问题描述
我已经创建了一个活动,并且我已经将我的数据存储在另一个带有封装的类中,并且从那个类中,我无法将我的数据检索到另一个类的文本字段中。下面是代码,请帮我解决。当我运行应用程序时,屏幕上什么也没有。
名称.java
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);
}
});
}
}
个人信息.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;
}
}
症状.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 bundle = new Bundle();
bundle.putSerializable("值", your_person_class_obj); 意图.putExtras(捆绑);
为此,您的类必须实现 Serializable 接口。
public class PersonInfo implements Serializable {
}
您的代码不起作用,因为您将对象设置在一个活动中,而第二个活动不可用。参考 - 检查此链接以获取更多详细信息
推荐阅读
- python - 如果我想创建一个流畅和更新的 GUI,我会使用什么技术堆栈?
- swift - Swift:可以禁用结构的隐式字符串插值吗?
- python - 如何在 Tkinter 中更改 OptionMenu 的边框颜色?
- java - Java - 使用 Super CSV 导出到 CSV,具有属性的实体
- python - 方法 __missing__ 不适用于康威人生游戏中的字典
- javascript - Javascript按近日期分组对象数组
- javascript - 如何使每个按钮在悬停时在其旁边显示其各自的文本并仍然应用类型动画
- tensorflow - 无法从 Tensorflow2 中的 'keras.layers.merge'' 导入名称 '_Merge'
- firebase - SetState 导致 Futurebuilder 在每次点击时重新加载数据
- google-data-studio - 用户未使用 getAuthType() = KEY 提示输入 API KEY - GOOGLE DATA STUDIO COMMUNITY CONNECTOR