Android开发的java初学者遇到问题
嗨,我正在申请一份家庭作业和一堵墙。我想输入两个信息,比如某人的名字和电子邮件,然后显示一条带有姓名和电子邮件的感谢信息。这是我的感谢页面,当我点击它时,我很难让我的按钮做任何事情。我有这个代码,我没有出现错误
import 安卓.support.v7.app.ActionBarActivity;
import 安卓.os.Bundle;
import 安卓.view.Menu;
import 安卓.view.MenuItem;
import 安卓.widget.TextView;
public class SignupActivity extends ActionBarActivity {
TextView greetMessage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_signup);
Bundle bundle = this.getIntent().getExtras();
if(null != bundle){
String name = bundle.getString("name");
String email = bundle.getString("email");
if(null != name && name != "" && email != "") {
greetMessage = (TextView)findViewById(R.id.textView1);
greetMessage.setText("Thank you " + name + "you can expect and email from us soon to
your email address at " + email);
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.signup, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
还有我的其他活动代码
import 安卓.support.v7.app.ActionBarActivity;
import 安卓.content.Intent;
import 安卓.os.Bundle;
import 安卓.view.Menu;
import 安卓.view.MenuItem;
import 安卓.view.View;
import 安卓.view.View.OnClickListener;
import 安卓.widget.Button;
import 安卓.widget.EditText;
public class ThankYouActivity extends ActionBarActivity implements OnClickListener {
EditText nameField;
EditText emailField;
Button submitButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_signup);
emailField = (EditText) findViewById(R.id.editText2);
nameField = (EditText) findViewById(R.id.editText1);
submitButton = (Button) findViewById(R.id.button1);
submitButton.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_enter, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onClick(View v) {
String name = nameField.getText().toString();
String email = emailField.getText().toString();
if (null != name && name != "" && email != "") {
Intent intent = new Intent(this,ThankYouActivity.class);
intent.putExtra("name", name);
this.startActivity(intent);
}
}
}
感谢您提前提出建议
# 1 楼答案
你在这两项活动中都有:
将其更改为其他xml布局
编辑: 你不会故意在传递姓名下面传递电子邮件:
请说:
应该是这样的:
EDIT2: 在onClick的4行中,将“this”更改为getApplicationContext(),如下所示:(如果可以的话)
在最后一行删除“this”,它应该是这样的:
# 2 楼答案
好吧,你把名字搞混了一点:p 因为,注册活动指的是“谢谢”活动,而“谢谢”活动指的是“布局注册”
在SingupActivity中,更改以下内容:
在您的注册活动中,更改以下内容:
在活动_谢谢_您创建了
TextView
,但您没有为其设置id,所以将其更改为:当您将其添加到textview时,请转到
SignupActivity
并将其连接在一起:另外,不要忘记在Android清单中更改名称
在onClick方法中,在startActivty之前删除以下内容: