【问题标题】:Android Studio cannot resolve symbol textViewAndroid Studio 无法解析符号 textView
【发布时间】:2017-10-20 02:40:42
【问题描述】:

完全是 Android Studio 的新手。

我刚刚在 Windows 10 上安装了它,因为我想学习如何使用它,所以我开始遵循在线指南 (https://developer.android.com/training/basics/firstapp/starting-activity.html#Up)。

我确实是一步一步地跟着它,但是当涉及到这部分代码时:

public class DisplayMessageActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_display_message);

        // Get the Intent that started this activity and extract the string
        Intent intent = getIntent();
        String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

        // Capture the layout's TextView and set the string as its text
        TextView textView = (TextView) findViewById(R.id.textView);
        textView.setText(message);
    }

“findViewById”之后的“textView”部分显示为红色,我收到以下错误消息:“无法解析符号'textView'”。

即使我已导入 android.widget.TextView,也会发生这种情况。

也许这是个愚蠢的问题,但我对 Android Studio 完全陌生。 感谢您的回答:)

【问题讨论】:

  • 您可以将activity_display_message.xml 的内容添加到您的问题中吗?

标签: android textview


【解决方案1】:

在您的文件 R.layout.activity_display_message 中,<TextView> 标签必须有 android:id="@+id/textView"

您可以包含您的 XML 文件吗?

【讨论】:

  • 哦,谢谢!在 xml 文件中写着 android:id="@+id/textView4" 而在 java 文件中只有 textView!现在它不再给出这个错误了:)
【解决方案2】:

正如buutqn所说

<TextView> 标签必须有android:id="@+id/textView"

这在文件中:app.res.layout.activity_display_message.xml 在“文本”选项卡下(与“设计”选项卡相对)。

可以通过从设计选项卡中删除 TextView 框并插入新的(第二个)文本视图来复制此问题。

【讨论】:

    【解决方案3】:

    在我的情况下,尽管错误相同,但问题是出于其他原因。我从 OnCreate 方法中复制了以下代码。

        // Get the Intent that started this activity and extract the string
        Intent intent = getIntent();
        String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
    
        // Capture the layout's TextView and set the string as its text
        TextView textView = findViewById(R.id.textView);
        textView.setText(message);
    

    findViewById 之前的文本 (TextView) 不是必需的。

    【讨论】:

      【解决方案4】:

      Activity_display_message 的TextView 对象称为TextView2,而不是Textview。所以我尝试并改变了:

      TextView textView = (TextView) findViewById(R.id.textView);
      

      TextView textView = (TextView) findViewById(R.id.textView2);
      

      它成功了!

      【讨论】:

        【解决方案5】:

        删除该语句,然后重新键入。将出现一个小弹出窗口,指示您按下命令。当您按下命令时,这基本上是一个导入库问题(对于 macOS,它是 ⌥⏎ )将添加相应的库。如果这不起作用,请关闭 Android Studio 并重新打开它。我不是这方面的专家,但这对我有用,因此我正在分享。

        【讨论】:

          猜你喜欢
          • 2018-05-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-11-15
          • 1970-01-01
          • 2015-12-17
          • 2015-08-22
          相关资源
          最近更新 更多