首页 > 解决方案 > 我的 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());

    }
}

标签: javaandroid-studio

解决方案


这不像你所做的那样工作。要做的是将对象作为捆绑参数传递给症状活动。

Bundle bundle = new Bundle();

bundle.putSerializable("值", your_person_class_obj); 意图.putExtras(捆绑);

为此,您的类必须实现 Serializable 接口。

public class PersonInfo implements Serializable {

}

您的代码不起作用,因为您将对象设置在一个活动中,而第二个活动不可用。参考 - 检查此链接以获取更多详细信息


推荐阅读