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

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 01:18
    关注

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

    img

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

报告相同问题?

问题事件

  • 系统已结题 9月26日
  • 已采纳回答 9月19日
  • 创建了问题 9月18日
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部