木和氢 2022-09-18 15:40 采纳率: 66.7%
浏览 65
已结题

Android Activity文件调用setText

问题遇到的现象和发生背景

Android Studio 中Activity文件调用setText出错

用代码块功能插入代码,请勿粘贴截图

这是Activity文件

public class MainActivity extends AppCompatActivity {
    private RadioGroup radioGroup;
    private View textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        radioGroup=findViewById(R.id.rg);
        textView=findViewById(R.id.tv);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                if(i==R.id.nan){
                    textView.setText("您是男的");
                }else{
                    textView.setText("您是女的");
                }
            }
        });

    }
}

这是它的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="8dp"
    tools:context=".MainActivity">
    <RadioGroup
        android:id="@+id/rg"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    <RadioButton
        android:id="@+id/nan"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="男"/>
    <RadioButton
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="女"/>
    </RadioGroup>

    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30dp"/>
运行结果及报错内容

img

我想要达到的结果

哪里错了,运行成功

  • 写回答

4条回答 默认 最新

  • 立青_ 2022-09-19 09:18
    关注

    你这TextView咋还是自定义的呢?

    img

    import android.widget.TextView;
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 9月27日
  • 已采纳回答 9月19日
  • 创建了问题 9月18日

悬赏问题

  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装