android - 如何在 android 中将 setText() 与字符串对象一起使用

标签 android string textview

在下面的代码中,我对 Activity 返回使用react。当我得到正确的代码和结果时,我想把“数据”的结果放到一个不可编辑的 TextView 中。我尝试使用以下代码将 TextView 直接设置为 Intent 数据:

encodedText.setText(data.getData().toString());

但是,当我尝试执行此操作时出现异常错误。然后我尝试按照以下方式进行(见箭头):

public void onActivityResult(int requestCode, int resultCode, Intent data)
{
    if (requestCode == request_Code) {
        if (resultCode == RESULT_OK) {
            String encoded = data.getData().toString();
            TextView encodedText = (TextView) findViewById(R.id.result); 
        encodedText.setText(encoded); <------
        }            
    }
}

我在代码示例中箭头指向的那行打了一个断点,程序就是在这一行有问题。我查看了 textView 文档中的 setText() 函数,但我仍然不确定自己做错了什么。有人看到我没看到的东西吗?

最佳答案

尝试设置文本时出现 NullPointerException(encodedText.setText(encoded);) 因为您查找的 TextView findViewById()null。检查您的布局,确保布局中包含此 TextView 并避免 NullPointerException

关于android - 如何在 android 中将 setText() 与字符串对象一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9416979/

相关文章:

android - 显示标记而不在屏幕上跳转

android - phonegap 运行 android : command failed to execute: ant jar - on Windows

php - Google oAuth 登录提示,成功后重定向到下载 playstore 应用

c - 为什么 gcc 为 memcpy 复制 rodata 字符串?如何避免?

python - 从字符串到元组的转换 - 双逗号

android - android :supportsRtl ="true" in AndroidManifest xml file有什么用

c# - 到底什么是 String.LegacyMode 属性?

android textview settext 显示空指针异常

Android - 在 TextView 上缩进文本

android - 如何更改 EditText 指针颜色(不是光标)