【问题标题】:Cannot resolve symbol "setText" on Android Developer Tutorial webpage无法解析 Android 开发者教程网页上的符号“setText”
【发布时间】:2020-06-15 13:42:18
【问题描述】:

我正在学习 android studio,并且正在学习“MyFirstApp”教程。但是,当我尝试运行该应用程序时,我收到错误“无法解析符号“setText”。这是我的代码:

package com.example.myfirstapp;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

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 = findViewById(R.id.textView);
    textView.setText(message);
}

这应该和教程中的代码一样输入:https://developer.android.com/training/basics/firstapp/starting-activity.html#java

这是 activity_display_message.xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    tools:context=".DisplayMessageActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="@string/textview"
        android:textSize="14sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

我试图检查一个类似的问题,但似乎我没有做任何不同的事情:Android tutorial "Cannot resolve symbol 'setText' " 谁能帮助我?

【问题讨论】:

    标签: java android-studio


    【解决方案1】:

    此代码必须在 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);
    

    【讨论】:

    • 我赞成! :)
    • @user71311 如果此答案解决了您的问题,如果您也可以将其标记为答案,将会很有帮助。
    【解决方案2】:

    只需将 TextViewpublic 并在 onCreate() 方法之外声明即可。

    package com.example.myfirstapp;
    import android.content.Intent;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.widget.TextView;
    
    public class DisplayMessageActivity extends AppCompatActivity {
    
        public  TextView textView = findViewById(R.id.textView);
    
        @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);
    
         // set the string as its text
    
         textView.setText(message);
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-15
      • 2014-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多