Java 如何修复“无法解析符号“追加””

Java 如何修复“无法解析符号“追加””,java,android,Java,Android,我是Android开发的新手。我一直在跟进developer.android.com的一个例子,创建我的第一个应用程序。但现在我被这件事缠住了。我将感谢任何帮助 package com.avelmx.myfirstapp; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.TextView;

我是Android开发的新手。我一直在跟进developer.android.com的一个例子,创建我的第一个应用程序。但现在我被这件事缠住了。我将感谢任何帮助

package com.avelmx.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.append(message);

}

如果您不在函数的作用域中,请将代码移动到函数中,它将被编译。

您需要执行findViewById和textView.appendmessage;内部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 = findViewById(R.id.textView);
    textView.append(message);
 }



}
hi替换此代码:

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

TextView确实有append,但您需要在代码块中编写代码。在这种情况下,在onCreate.Man中这是令人沮丧的。面对朋克,它有效吗?如果是这样的话,则应向上投票/选择正确答案: