将 edittext 值传递给另一个 activity 得到空值

passing edittext values to another activity getting null values

这是我的示例任务应用程序,我有六个类 EmployeeDepartmentEmployeeActivity , ViewEmployeeInfo, ViewDepartmentInfo, 当用户点击添加员工时,它必须导航到 EmployeeActivity,同样的,当点击部门时,它导航到部门 class,我在 EmployeeActivity 上创建了一个员工对象,将它发送到 ViewEmployeeInfo activity 并且它必须在我创建它的五个 EditText 上查看它,问题是当我单击“提交”按钮以使用带有可序列化对象的 putExtra 方法通过 Intent 发送数据时,我在花药 activity 上的 EditTexts 上看不到任何值,我试过用常规的 putExtra 方法发送它作为 int,String,String,double,String 也有 NullPointerException

这是我的代码,首先是员工 class

    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>

和这个主要Class

`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_activityxml

    <?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>

EmployeeActivityclass

    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_infoactivityxml

    <?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>

ViewEmployeeInfoclass

    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());