在学习安卓四大核心组件Activity中,我做了一个在一个界面输入信息,点击注册按钮就会跳转到另外一个页面显示刚才注册输入的信息。
目录
1、编写过程中的问题
2、效果演示
Activity类activity_register.xml
编写过程中的问题
在写代码中我一个窒息的bug:
在代码编写过程中,没有报错,但是当运行程序时,却显示keeps stopping,程序打不开。没有找出问题前,我认为我的代码无敌但后来静下心来发现是空指针异常,为什么会空指针异常呢?就要回头看代码的通过资源索引获得界面空间实例里面的值是否参数赋值重复并且要和监听器里面值和变量一至,这样就不会出现程序不能启动。出现这种问基本都是到吗问题,在以后的编程中一定要细心。
在这次教训后,我总结了代码的编写顺序:
效果演示
用户注册及显示信息界面代码
Java类RegisterActivity
package com.example.a13468.registeractivity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class RegisterActivity extends Activity {
/**
* 设置姓名编辑框
*/
private EditText etdusername;
/**
*
* 设置性别编辑框
*/
private EditText edtsex;
/**
*
* 设置年龄编辑框
*/
private EditText edtage;
/**
*
* 设置电话编辑框
*/
private EditText edttell;
/**
*
* 设置邮箱编辑框
*/
private EditText edtemali;
/**
*
*设置主页编辑框
*/
private EditText homepage;
/**
*
* 设置备注编辑框
*/
private EditText note;
/**
*
* 设置注册按钮
*/
private Button btn1;
/**
*
* 设置取消按钮
*/
private Button btn2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 利用布局资源文件设置用户界面
setContentView(R.layout.activity_register);
//通过资源索引获得界面空间实例
etdusername=(EditText) findViewById(R.id.edtusername);
edtsex=(EditText)findViewById(R.id.edtsex);
edtage=(EditText)findViewById(R.id.edtage);
edttell=(EditText)findViewById(R.id.edttell);
edtemali=(EditText)findViewById(R.id.edtemail);
homepage=(EditText)findViewById(R.id.homepage);
note=(EditText)findViewById(R.id.note);
btn1=(Button) findViewById(R.id.btn1);
btn2=(Button) findViewById(R.id.btn2);
//给注册按钮设置监听器
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 保存用户输入的数据:用户名与密码
String strUsername = etdusername.getText().toString().trim();
String strSex=edtsex.getText().toString().trim();
String strAge=edtage.getText().toString().trim();
String strTell=edttell.getText().toString().trim();
String strEmail=edtemali.getText().toString().trim();
String strHomepage=homepage.getText().toString().trim();
String strNote=note.getText().toString().trim();
if((!strUsername.equals(null)) && (!strSex.equals(null))&& (!strAge.equals(null)) && (!strTell.equals(null)) && (!strEmail.equals(null))
&& (!strHomepage.equals(null)) && (!strNote.equals(null))) {
Toast.makeText(RegisterActivity.this, "恭喜,注册成功!",
Toast.LENGTH_LONG).show();
//实现跳转窗口,创建意图(参数1:上下文;参数2;目标文件)
Intent intent = new Intent(RegisterActivity.this, InforActivity.class);
//创建数据包
Bundle data=new Bundle();
//将数据放入数据包中
data.putString("user",strUsername);
data.putString("sex",strSex);
data.putString("age",strAge);
data.putString("tell",strTell);
data.putString("email",strEmail);
data.putString("hp",strHomepage);
data.putString("nt",strNote);
//通过意图携带数据包
intent.putExtras(data);
// 按照意图启动目标组件
startActivity(intent);
}else {
Toast.makeText(RegisterActivity.this, "注册失败!",
Toast.LENGTH_LONG).show();
}
}
});
// 给取消按钮注册监听器,实现监听器接口,编写事件处理方法
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
}
Activity类activity_register.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@mipmap/login"
tools:context="com.example.a13468.registeractivity.RegisterActivity">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/usernamr"
android:textSize="30sp"
android:textColor="#c8f517e6"
android:paddingTop="20dp"/>
<EditText
android:id="@+id/edtusername"
android:layout_width="250dp"
android:layout_height="40dp"
android:ems="10" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/sex"
android:textSize="30sp"
android:textColor="#c8f517e6"
android:paddingTop="20dp"/>
<EditText
android:id="@+id/edtsex"
android:layout_width="250dp"
android:layout_height="40dp"
android:ems="2" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/age"
android:textSize="30sp"
android:textColor="#c8f517e6"
android:paddingTop="20dp"/>
<EditText
android:id="@+id/edtage"
android:layout_width="250dp"
android:layout_height="40dp"
android:ems="2" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/tell"
android:textSize="30sp"
android:textColor="#c8f517e6"
android:paddingTop="20dp"/>
<EditText
android:id="@+id/edttell"
android:layout_width="250dp"
android:layout_height="40dp"
android:ems="12" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/email"
android:textSize="30sp"
android:textColor="#c8f517e6"
android:paddingTop="20dp"/>
<EditText
android:id="@+id/edtemail"
android:layout_width="250dp"
android:layout_height="40dp"
android:ems="16" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/home_page"
android:textSize="30sp"
android:textColor="#c8f517e6"
android:paddingTop="20dp"/>
<EditText
android:id="@+id/homepage"
android:layout_width="250dp"
android:layout_height="40dp"
android:ems="20" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/note"
android:textSize="30dp"
android:textColor="#c8f517e6"
android:paddingTop="20dp"/>
<EditText
android:id="@+id/note"
android:layout_width="250dp"
android:layout_height="80dp"
android:ems="2" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="45dp">
<Button
android:id="@+id/btn1"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:text="@string/Registration"/>
<Button
android:id="@+id/btn2"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:text="@string/cancelled"/>
</LinearLayout>
</LinearLayout>
JAva类InforActivity
package com.example.a13468.registeractivity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class InforActivity extends Activity {
private TextView infouser;
private TextView infosex;
private TextView infoage;
private TextView infotell;
private TextView infoemail;
private TextView infohomepage;
private TextView infonote;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//利用布局资源文件设置用户界面
setContentView(R.layout.activity_infor);
//通过资源标识获得控件实例
infouser=(TextView) findViewById(R.id.info_user);
infosex=(TextView)findViewById(R.id.info_sex);
infoage=(TextView)findViewById(R.id.info_age);
infotell=(TextView)findViewById(R.id.info_tell);
infoemail=(TextView)findViewById(R.id.info_email);
infohomepage=(TextView) findViewById(R.id.info_homepage);
infonote=(TextView)findViewById(R.id.info_note);
//获取意图
Intent intent= getIntent();
//判断是否为空
if(intent !=null){
//获取意图携带的数据包
Bundle data =intent.getExtras();
//从数据包中提取各项数据
String strUsername=data.getString("user");
String strSex=data.getString("sex");
String strAge=data.getString("age");
String strTell=data.getString("tell");
String strEmail=data.getString("email");
String strHomepage=data.getString("hp");
String strNote=data.getString("nt");
//拼接用户信息
String strUserInfo="姓名:"+strUsername;
String strsexInfo="性别:"+strSex;
String strageInfo="年龄:"+strAge;
String strtellInfo="电话:"+strTell;
String stremailInfo="邮箱:"+strEmail;
String strhpInfo="主页:"+strHomepage;
String strntInfo="备注:"+strNote;
//设置标签文本属性,显示用户信息
infouser.setText(strUserInfo);
infosex.setText(strsexInfo);
infoage.setText(strageInfo);
infotell.setText(strtellInfo);
infoemail.setText(stremailInfo);
infohomepage.setText(strhpInfo);
infonote.setText(strntInfo);
}
}
}
Activity类activity_infor.xml
package com.example.a13468.registeractivity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class InforActivity extends Activity {
private TextView infouser;
private TextView infosex;
private TextView infoage;
private TextView infotell;
private TextView infoemail;
private TextView infohomepage;
private TextView infonote;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//利用布局资源文件设置用户界面
setContentView(R.layout.activity_infor);
//通过资源标识获得控件实例
infouser=(TextView) findViewById(R.id.info_user);
infosex=(TextView)findViewById(R.id.info_sex);
infoage=(TextView)findViewById(R.id.info_age);
infotell=(TextView)findViewById(R.id.info_tell);
infoemail=(TextView)findViewById(R.id.info_email);
infohomepage=(TextView) findViewById(R.id.info_homepage);
infonote=(TextView)findViewById(R.id.info_note);
//获取意图
Intent intent= getIntent();
//判断是否为空
if(intent !=null){
//获取意图携带的数据包
Bundle data =intent.getExtras();
//从数据包中提取各项数据
String strUsername=data.getString("user");
String strSex=data.getString("sex");
String strAge=data.getString("age");
String strTell=data.getString("tell");
String strEmail=data.getString("email");
String strHomepage=data.getString("hp");
String strNote=data.getString("nt");
//拼接用户信息
String strUserInfo="姓名:"+strUsername;
String strsexInfo="性别:"+strSex;
String strageInfo="年龄:"+strAge;
String strtellInfo="电话:"+strTell;
String stremailInfo="邮箱:"+strEmail;
String strhpInfo="主页:"+strHomepage;
String strntInfo="备注:"+strNote;
//设置标签文本属性,显示用户信息
infouser.setText(strUserInfo);
infosex.setText(strsexInfo);
infoage.setText(strageInfo);
infotell.setText(strtellInfo);
infoemail.setText(stremailInfo);
infohomepage.setText(strhpInfo);
infonote.setText(strntInfo);
}
}
}