将edittext值传递给另一个获取空值的活动
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了将edittext值传递给另一个获取空值的活动相关的知识,希望对你有一定的参考价值。
这里是我的示例任务应用程序,我有六个条款Employee,Department,EmployeeActivity,ViewEmployeeInfo,ViewDepartmentInfo,当用户点击添加员工时,它必须导航到EmployeeActivity,同样的事情点击部门时它导航到Department类,我创建了一个EmployeeActivity上的employee对象将它发送到ViewEmployeeInfo活动,它必须在我创建它的五个EditText上查看它,问题是当我单击Submit按钮通过Intent发送数据时使用带有Serializable对象的putExtra方法我看不到值在关于anther活动的EditTexts上,我尝试用regual putExtra方法发送它作为int,String,String,double,String也得到了NullPointerException
这是我的代码,首先是Employee类
package com.companyactivityexample.companyactivityexample;
import android.widget.Button;
import android.widget.EditText;
import java.io.Serializable;
/**
* Created by MML on 24/12/2017.
*/
public class Employee implements Serializable {
private int id;
private String name;
private String address;
private double salary;
private String job;
public Employee(int id, String name, String address, double salary, String job) {
this.id = id;
this.name = name;
this.address = address;
this.salary = salary;
this.job = job;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
}
这个activity_main
<?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"
tools:context="com.companyactivityexample.companyactivityexample.MainActivity"
android:orientation="vertical"
>
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:layout_marginStart="40dp"
android:layout_marginEnd="40dp"
android:layout_marginBottom="10dp"
android:text="Add employee"
android:textSize="25sp"
android:textAllCaps="false"
/>
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="40dp"
android:layout_marginEnd="40dp"
android:text="Add department"
android:textSize="25sp"
android:textAllCaps="false"
/>
</LinearLayout>
和这个主类
`package com.companyactivityexample.companyactivityexample;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button addEmployee;
private Button addDepartment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addEmployee = (Button) findViewById(R.id.button1);
addDepartment = (Button) findViewById(R.id.button2);
addEmployee.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(MainActivity.this,EmployeeActivity.class);
startActivity(i);
}
});
addDepartment.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(MainActivity.this,Department.class);
startActivity(i);
}
});
}
}
employee_activity xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
>
<EditText
android:id="@+id/empID"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:gravity="center"
android:hint="ID"
android:textSize="25sp"
android:textColorHint="@color/black"
android:inputType="number"
/>
<EditText
android:id="@+id/empName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:gravity="center"
android:hint="Name"
android:textSize="25sp"
android:textColorHint="@color/black"
android:inputType="text"
/>
<EditText
android:id="@+id/empAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:gravity="center"
android:hint="Address"
android:textSize="25sp"
android:textColorHint="@color/black"
android:inputType="text"
/>
<EditText
android:id="@+id/empSalary"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:gravity="center"
android:hint="Salary"
android:textSize="25sp"
android:textColorHint="@color/black"
android:inputType="numberDecimal"
/>
<EditText
android:id="@+id/empJob"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:gravity="center"
android:hint="Job"
android:textSize="25sp"
android:textColorHint="@color/black"
android:inputType="text"
/>
<Button
android:id="@+id/submit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="40dp"
android:layout_marginEnd="40dp"
android:text="Submit"
android:textSize="25sp"
android:textAllCaps="false"
/>
</LinearLayout>
</ScrollView>
EmployeeActivity类
package com.companyactivityexample.companyactivityexample;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
/**
* Created by MML on 27/12/2017.
*/
public class EmployeeActivity extends AppCompatActivity {
private EditText editTextID;
private EditText editTextName;
private EditText editTextAddress;
private EditText editTextSalary;
private EditText editTextJob;
private Button submitButton;
private Employee employee1;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.employee_activity);
editTextID = (EditText) findViewById(R.id.empID);
editTextName = (EditText) findViewById(R.id.empName);
editTextAddress = (EditText) findViewById(R.id.empAddress);
editTextSalary = (EditText) findViewById(R.id.empSalary);
editTextJob = (EditText) findViewById(R.id.empJob);
submitButton = (Button) findViewById(R.id.submit);
if(isEmpty(editTextID) || isEmpty(editTextSalary)){
editTextID.setText("0");
editTextSalary.setText("0");
}
employee1 = new Employee(Integer.parseInt(editTextID.getText().toString())
, editTextName.getText().toString(), editTextAddress.getText().toString()
, Double.parseDouble(editTextSalary.getText().toString())
, editTextJob.getText().toString());
submitButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(EmployeeActivity.this, ViewEmployeeInfo.class);
i.putExtra("emp",employee1);
startActivity(i);
}
});
}
/*i.putExtra("id",id);
i.putExtra("name",name);
i.putExtra("address",address);
i.putExtra("salary",salary);
i.putExtra("job",job);*/
private static boolean isEmpty(EditText etText) {
if (etText.getText().toString().trim().length() > 0)
return false;
return true;
}
}
view_employee_info活动xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:fillViewport="true"
tools:context=".ViewEmployeeInfo">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="20dp"
android:layout_marginStart="20dp"
android:gravity="center"
android:text="ID"
android:textColor="@color/black"
android:textSize="25sp"
/>
<EditText
android:id="@+id/editTextID"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="20dp"
android:layout_marginStart="20dp"
android:inputType="number" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="20dp"
android:layout_marginStart="20dp"
android:gravity="center"
android:text="Name"
android:textColor="@color/black"
android:textSize="25sp"
/>
<EditText
android:id="@+id/editTextName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="20dp"
android:layout_marginStart="20dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="20dp"
android:layout_marginStart="20dp"
android:gravity="center"
android:text="Address"
android:textColor="@color/black"
android:textSize="25sp"
/>
<EditText
android:id="@+id/editTextAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="20dp"
android:layout_marginStart="20dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="20dp"
android:layout_marginStart="20dp"
android:gravity="center"
android:text="Salary"
android:textColor="@color/black"
android:textSize="25sp"
/>
<EditText
android:id="@+id/editTextSalary"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="20dp"
android:layout_marginStart="20dp"
android:inputType="numberDecimal" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="20dp"
android:layout_marginStart="20dp"
android:gravity="center"
android:text="Job"
android:textColor="@color/black"
android:textSize="25sp"
/>
<EditText
android:id="@+id/editTextJob"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="20dp"
android:layout_marginStart="20dp" />
</LinearLayout>
<Button
android:id="@+id/editButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="30dp"
android:layout_marginStart="30dp"
android:layout_marginTop="20dp"
android:text="Edit"
android:textAllCaps="false"
android:textSize="25sp" />
</LinearLayout>
</ScrollView>
ViewEmployeeInfo类
package com.companyactivityexample.companyactivityexample;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
/**
* Created by MML on 24/12/2017.
*/
public class ViewEmployeeInfo extends AppCompatActivity {
private EditText editID;
private EditText editName;
private EditText editAddress;
private EditText editSalary;
private EditText editJob;
private Button editButton;
private Employee employee;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view_employee_info);
Intent i = getIntent();
employee = (Employee) i.getSerializableExtra("emp");
editID = (EditText) findViewById(R.id.editTextID);
editID.setText(employee.getId() + "");
editName = (EditText) findViewById(R.id.editTextName);
editName.setText(employee.getName());
editAddress = (EditText) findViewById(R.id.editTextAddress);
editAddress.setText(employee.getAddress());
editSalary = (EditText) findViewById(R.id.editTextSalary);
editSalary.setText(employee.getSalary() + "");
editJob = (EditText) findViewById(R.id.editTextJob);
editJob.setText(employee.getJob());
editButton = (Button) findViewById(R.id.editButton);
/*Intent i = getIntent();
int getID = i.getIntExtra("id", 0);
String getName = i.getStringExtra("name");
String getAddress = i.getStringExtra("address");
double getSalary = i.getDoubleExtra("salary", 0.0);
String getJob = i.getStringExtra("job");
editID.setText(getID);
editName.setText(getName);
editAddress.setText(getAddress);
editSalary.setText(getSalary + "");
editJob.setText(getJob);*/
}
}
你把额外的东西放在“emp1”的键上,并试图使用getSerializableExtra(“emp”)中的“emp”键来检索它。简单的拼写错误。
编辑:不是拼写错误。而不是putExtra(“emp”,employee1),像putExtra(“emp”,(serializable)employee1)
使用Bundle.Serializable传递可序列化列表
Intent i = new Intent(EmployeeActivity.this, ViewEmployeeInfo.class);
i.putSerializable("emp",employee1);
startActivity(i);
代替
Intent i = new Intent(EmployeeActivity.this, ViewEmployeeInfo.class);
i.putExtra("emp",employee1);
startActivity(i);
接收
Intent i = getIntent();
employee = (Employee) i.getSerializableExtra("emp");
问题解决了,它应该将employee1的新对象放在提交按钮的操作中
employee1 = new Employee(Integer.parseInt(editTextID.getText().toString())
, editTextName.getText().toString(), editTextAddress.getText().toString()
, Double.parseDouble(editTextSalary.getText().toString())
, editTextJob.getText().toString());
以上是关于将edittext值传递给另一个获取空值的活动的主要内容,如果未能解决你的问题,请参考以下文章