Java 为什么可以';“我解析符号”;setText";在安卓工作室?

Java 为什么可以';“我解析符号”;setText";在安卓工作室?,java,android,Java,Android,我一直在使用安卓开发者的“构建你的第一个应用程序”教程,并一直在创建第二个活动。我是java新手,因为我以前只在Python和C++中编码过,所以我也对语法和语法都很生疏。我一直遵循教程,直到代码中的这一点,并得到以下结论: package com.example.myapplication; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bun

我一直在使用安卓开发者的“构建你的第一个应用程序”教程,并一直在创建第二个活动。我是java新手,因为我以前只在Python和C++中编码过,所以我也对语法和语法都很生疏。我一直遵循教程,直到代码中的这一点,并得到以下结论:

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
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);
}
我运行代码时遇到的问题是,它无法解析符号“setText”,因此,为什么以及我必须做些什么才能克服这个问题。它还表示“未知类:'消息'”。有人有办法帮我吗?

试试这个:

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);
    }
}

嗨,Ryan,你在错误的地方初始化了TextView。它通常应该在onCreate方法中,就在这一行setContentView(R.layout.activity\u display\u message)下面;