Java 如何修改TextView的内容

Java 如何修改TextView的内容,java,android,Java,Android,我完全按照指南创建了我的第一个Android应用程序,但是发布的代码给了我一个错误。显然没有TextView.setText(String)方法,我下面的代码没有编译: package com.example.myfirstapp; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import

我完全按照指南创建了我的第一个Android应用程序,但是发布的代码给了我一个错误。显然没有TextView.setText(String)方法,我下面的代码没有编译:

    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 = (TextView)findViewById(R.id.textView);
        textView.setText(message);
    }
错误消息:无法解析符号“setText”


有人能帮忙吗?

您需要执行
findviewbyd
textView.setText(消息)
内部
onCreate()
方法

试试这个

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);
    }
}
尝试以下方法(您必须将这些行放入您的方法中):

您不可能完全按照教程进行操作,因为您用}括号关闭了onCreate方法,并将所有其他命令放在类的主体中,任何方法之外。
    @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);**   
     }