我刚刚开始学习 android 编程,并在 developer.android.com 开始了在线教程。一直在一步步检查代码,但现在出现错误,无法继续。
在线上:
EditText editText = (EditText) findViewByID(R.id.edit_message);
我收到错误“未定义 MainActivity 类型的 findViewByID(int) 方法”
该代码似乎与网站上显示的相同,所以不确定我做错了什么。任何帮助表示赞赏。完整代码如下:
package com.example.myfirstapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
/** Called when the user clicks the send button */
public void sendMessage(View view) {
// do something in response to button
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewByID(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}