【发布时间】:2013-10-28 14:01:24
【问题描述】:
所以我一直在阅读有关创建 Android 应用程序的初学者指南 (http://developer.android.com/training/basics/firstapp/index.html)。除了我不断出错的一步之外,它运行良好。
MainActivity.java 对我不起作用。我在 3 个不同的地方遇到各种错误。
这是我的代码的样子:
package com.fredde.myfirstapp;
public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
import android.app.Activity;enter code here
import android.content.Intent;
import android.view.View;
import android.widget.EditText;
import android.view.View;
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
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);
}
因此,我希望阅读过本指南的人可以帮助我,或者只是希望即使没有完成此特定项目也能看出问题所在的人。提前致谢!
放轻松,我是一个完全的初学者。
【问题讨论】:
-
我不知道您会遇到哪些错误,但可以肯定的是。您没有为活动设置任何布局,因此 findViewById 肯定会返回 null。您可能会在下一行得到 NullRefException。我建议您多阅读有关如何实施活动的内容。
-
你到底是怎么在 Activity 类中得到这些导入的?
-
你忘了活动
onCreate方法 -
现在剩下的是,复制G V的代码后,在EditText这一行出现错误 editText = (EditText) findViewById(R.id.edit_message); “ R 无法解析为变量” @ramaral