我刚刚开始自学android开发,并且在这个基本的hello world应用程序中遇到了错误。
我尝试搜索,但对我来说一切都像中文。
这是我正在关注的教程:https://developer.android.com/training/basics/firstapp/starting-activity
,这是我的错误的屏幕截图: [我将在下面添加代码] [1]:https://i.stack.imgur.com/Xm1tg.png
<code>
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);//on this line i get an error under "message"
</code>
答案 0 :(得分:2)
在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.setText(message);
}
}
答案 1 :(得分:0)
您需要在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);//on this line i get an error under "message"