我完全遵循https://developer.android.com/training/basics/firstapp/starting-activity#java上的指南来创建我的第一个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'
有人可以帮忙吗?
答案 0 :(得分:1)
您需要执行findViewById
和textView.setText(message);
内部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);
}
}
答案 1 :(得分:0)
尝试一下(您必须将这些行放入方法中):
@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);**
}