【问题标题】:android, String value isn't visible in debuggerandroid,字符串值在调试器中不可见
【发布时间】:2021-05-24 16:04:08
【问题描述】:

我想在 android 中跟踪编辑文本的值,我使用的是调试模式。

这是一段代码,我从用户的输入中检索数据,我想在调试器中看到它。

    EditText personName=findViewById(R.id.editTextTextPersonName2);
    EditText number=findViewById(R.id.editTextNumber);
    Intent sendBack=new Intent();
    String sentName=personName.toString();
    Integer a=Integer.parseInt(number.getText().toString());
    sendBack.putExtra("name", sentName);
    setResult(RESULT_OK, sendBack);
    Activity2.this.finish();

但是,我注意到由整数(Number)返回的值显示在调试器中,而字符串(plainText)值不显示。

我希望能够看到字符串的值,就像屏幕截图中显示的整数一样。

【问题讨论】:

  • 也许你想做String sentName=personName.getText().toString();而不是String sentName=personName.toString();
  • 我已经添加了 getText(),现在我可以在调试器中可视化来自用户的哪些数据,非常感谢你。

标签: java android debugging


【解决方案1】:

实际上,字符串在调试器中是可见的。正如您执行String sentName=personName.toString(); 一样,EditText#toString 函数正在返回有关 personName (EditText) 的所有信息,而不是它的值。

这个EditText的信息包含view的id、位置、大小、包名、值等

您可以在屏幕截图中看到该字符串以所使用的 EditText 的包名称开头(即“androidx.appcompat.widget.AppCompatEditText”),然后是其他一些信息,例如 ObjectId 等。

如果您只需要访问此 EditText 中的值,只需将您的代码从 String sentName=personName.toString(); 修改为 String sentName=personName.getText().toString();

【讨论】:

  • 这解决了我的问题,非常感谢你。
【解决方案2】:

我猜你在第一行写错了两次“personName”。 检查这条线。 EditText personName=personName=findViewById(R.id.editTextTextPersonName2); 另外,更具体地说,如果您使用的是 API 级别 EditText personName= (EditText)findViewById(R.id.editTextTextPersonName2);并使用String sentName = personName.getText()从EditText View中获取文本数据。

【讨论】:

    猜你喜欢
    • 2021-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多