1

は、ここでは、私は6 clases 従業員部門EmployeeActivityViewEmployeeInfo持っViewDepartmentInfo、 私のサンプルタスク、アプリの場合は、追加の従業員、その上にユーザーがクリックEmployeeActivityに移動する必要があります。部門をクリックするとDepartmentクラスに移動しますが、EmployeeActivityに従業員オブジェクトを作成してViewEmployeeInfoアクティビティに送信し、作成した5つのEditTextで表示する必要があります。送信ボタンをクリックしてputExtraが満たされたインテント経由でデータを送信すると私は葯活性にEditTextsには値を参照しない直列化オブジェクトを受け取りHOD、私はint型、文字列、文字列、ダブル、文字列もなっ別のアクティビティへのEditTextの値を渡すnull値

Company task 02

NullPointerExceptionが

Company task 01

だとしてregual putExtra方法でそれを送信しようとしましたここで Company task 04

Company task 03

は私のコードは、まずこのEmployeeクラス

012です
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; 
    } 
} 

この

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

activity_mainこのメインクラス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);*/ 

    } 
} 

答えて

1

問題が解決された、それは送信ボタンのアクション内でご提案のための

employee1 = new Employee(Integer.parseInt(editTextID.getText().toString()) 
         , editTextName.getText().toString(), editTextAddress.getText().toString() 
         , Double.parseDouble(editTextSalary.getText().toString()) 
         , editTextJob.getText().toString()); 
0

あなたは内線をかけますra "in emp1"キーを取得し、getSerializableExtra( "emp")で "emp"のキーを使用して取得しようとしています。シンプルなタイプミス。

編集:入力ミスではありません。その代わり、putExtraのようにキャストputExtra(employee1 "EMP"、)の( "EMP"、(直列化可能)employee1)

+0

感謝をemployee1 の新しいオブジェクトを置くことになっていた私は、ユーザーの前にemployee1オブジェクトにデータを設定しているため、問題がでした何でもタイプ! –

0

は、代わりに

 Intent i = new Intent(EmployeeActivity.this, ViewEmployeeInfo.class); 
    i.putExtra("emp",employee1); 
    startActivity(i); 
Bundle.Serializable

Intent i = new Intent(EmployeeActivity.this, ViewEmployeeInfo.class); 
    i.putSerializable("emp",employee1); 
    startActivity(i); 

を使用して、シリアライズリストを渡します

受信

関連する問題