在對組件進行註冊時,只註冊了EditText,卻忘記了Button,致使程序一直閃退java
輸入信息android
點擊添加app
成功跳轉頁面,並將數據傳遞ide
User.javathis
package com.example.register; import android.os.Parcel; import android.os.Parcelable; public class User implements Parcelable { private String name; private String age; private String sex; public User(String name, String age, String sex) { this.name = name; this.age = age; this.sex = sex; } public User() { } //讀取 protected User(Parcel in) { name = in.readString(); age = in.readString(); sex = in.readString(); } //寫入 @Override public void writeToParcel(Parcel dest, int flags) { dest.writeString(name); dest.writeString(age); dest.writeString(sex); } @Override public int describeContents() { return 0; } public static final Creator<User> CREATOR = new Creator<User>() { @Override public User createFromParcel(Parcel in) { return new User(in); } @Override public User[] newArray(int size) { return new User[size]; } }; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } }
UserAddActivity.javaspa
package com.example.register; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.text.TextUtils; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import androidx.annotation.Nullable; public class UserAddActivity extends Activity { private EditText mName; private EditText mAge; private EditText mSex; private Button mAddBtn; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_adduser); initView(); initListener(); } private void initListener() { mAddBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //添加按鈕被點擊了 handlerAdduser(); } }); } private void handlerAdduser() { String name = mName.getText().toString().trim(); if (TextUtils.isEmpty(name)) { Toast.makeText(this, "輸入的姓名爲空", Toast.LENGTH_SHORT).show(); return; } String age = mAge.getText().toString().trim(); if (TextUtils.isEmpty(age)) { Toast.makeText(this, "輸入的年齡爲空", Toast.LENGTH_SHORT).show(); return; } String sex = mSex.getText().toString().trim(); if (TextUtils.isEmpty(sex)) { Toast.makeText(this, "輸入的性別爲空", Toast.LENGTH_SHORT).show(); return; } //跳轉並傳值給AddShowActivity Intent intent = new Intent(this, AddShowActivity.class); User user = new User(); //將屬性封裝到user對象中 user.setName(name); user.setAge(age); user.setSex(sex); intent.putExtra("userKey",user); startActivity(intent); } private void initView() { mName=this.findViewById(R.id.add_name); mAge=this.findViewById(R.id.add_age); mSex=this.findViewById(R.id.add_sex); mAddBtn=this.findViewById(R.id.add_btn); } }
activity_adduser.xmlcode
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:text="添加人員信息" android:textSize="30sp" android:layout_marginTop="20dp" android:layout_gravity="center" android:layout_height="wrap_content"> </TextView> <EditText android:id="@+id/add_name" android:layout_width="match_parent" android:text="請輸入姓名" android:layout_gravity="center" android:layout_height="wrap_content"> </EditText> <EditText android:id="@+id/add_age" android:layout_width="match_parent" android:text="請輸入年齡" android:layout_gravity="center" android:layout_height="wrap_content"> </EditText> <EditText android:id="@+id/add_sex" android:layout_width="match_parent" android:text="請輸入性別" android:layout_gravity="center" android:layout_height="wrap_content"> </EditText> <Button android:id="@+id/add_btn" android:layout_width="wrap_content" android:layout_gravity="center" android:text="添加" android:layout_height="wrap_content"> </Button> </LinearLayout>
AddShowActivity.javaxml
package com.example.register; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.os.Parcelable; import android.widget.TextView; import androidx.annotation.Nullable; public class AddShowActivity extends Activity { @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_addshow); TextView nameshow =(TextView) this.findViewById(R.id.add_name_show); TextView ageshow =(TextView) this.findViewById(R.id.add_age_show); TextView sexshow =(TextView) this.findViewById(R.id.add_sex_show); //經過getIntent獲取MainActivity傳來的intent Intent intent = getIntent(); if (intent!=null) { User user =intent.getParcelableExtra("userKey"); if (user!=null) { nameshow.setText(user.getName()); ageshow.setText(user.getAge()); sexshow.setText(user.getSex()); } } } }
activity_addshow.xml對象
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:text="添加信息展現" android:textSize="30sp" android:layout_gravity="center" android:layout_height="wrap_content"> </TextView> <TextView android:id="@+id/name" android:layout_width="wrap_content" android:text="姓名" android:textSize="20sp" android:layout_height="wrap_content"> </TextView> <TextView android:id="@+id/add_name_show" android:layout_width="wrap_content" android:textSize="25sp" android:layout_height="wrap_content"> </TextView> <TextView android:id="@+id/age" android:layout_width="wrap_content" android:text="年齡" android:textSize="20sp" android:layout_height="wrap_content"> </TextView> <TextView android:id="@+id/add_age_show" android:layout_width="wrap_content" android:textSize="25sp" android:layout_height="wrap_content"> </TextView> <TextView android:id="@+id/sex" android:layout_width="wrap_content" android:text="性別" android:textSize="20sp" android:layout_height="wrap_content"> </TextView> <TextView android:id="@+id/add_sex_show" android:layout_width="wrap_content" android:textSize="25sp" android:layout_height="wrap_content"> </TextView> </LinearLayout>