为什么我的parceled对象返回null? Android Dev

时间:2015-10-26 01:24:17

标签: java android object parcel

我一直试图包裹一个对象并将其值返回给MainActivity方法。所有对象的属性都已分配,当我在分配它之前在我的AddItemActivity中记录mItem对象时,它都会检出。在某个地方,我的数据正在迷失......无论是从我创建新Intent的最初阶段,还是在MainActivity的接收端。我要做的是...获取用户输入,创建项目对象,将该对象传递给MainActivity,并将其添加到Inventory。有什么想法吗?

这是MainActivity:

import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Parcelable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.gardnerwebideas.inventorytracker.R;
import com.gardnerwebideas.inventorytracker.adapters.ItemAdapter;
import com.gardnerwebideas.inventorytracker.models.Inventory;
import com.gardnerwebideas.inventorytracker.models.Item;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class MainActivity extends ListActivity {

    private static final String TAG = MainActivity.class.getSimpleName();
    private Item mNewItem;
    private Inventory mInventory;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button addButton = (Button) findViewById(R.id.addToInventoryButton);

        mInventory = new Inventory();
        mNewItem = new Item();

        addButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addToInventory();
            }
        });

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        switch(requestCode) {
            case (1): {
                if (resultCode == Activity.RESULT_OK) {
                    Intent intent = getIntent();
                    mNewItem = intent.getParcelableExtra(AddItemActivity.ADD_ITEM);
                    new ItemAdapter(this, mInventory);
                    Toast.makeText(this,
                            "Inventory size: "
                                    + String.valueOf(mInventory.getSize())
                                    + mInventory.toString(),
                            Toast.LENGTH_LONG).show();
                }
                break;
            }
            default:
                break;
        }
    }

    public void addToInventory() {
        Intent intent = new Intent(this, AddItemActivity.class);
        startActivityForResult(intent, 1);
    }

}

这是AddItemActivity:

import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.gardnerwebideas.inventorytracker.models.Item;
import com.gardnerwebideas.inventorytracker.R;

import java.util.Date;

public class AddItemActivity extends AppCompatActivity {

    public static final String TAG = AddItemActivity.class.getSimpleName();

    public static final String ADD_ITEM = "ADD_ITEM";
    private Item mItem;
    private String mName;
    private String mDate;
    private String mDescription;
    private String mPrice;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_item);

        Button addButton = (Button) findViewById(R.id.addButton);

        addButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                createNewItem();
                mItem = new Item(mName, mDescription, mPrice, mDate);
                Log.d(TAG, "THE ITEM IS: " + mItem.toString());

                Intent intent = new Intent();
                intent.putExtra(ADD_ITEM, mItem);
                setResult(Activity.RESULT_OK, intent);
                finish();
            }
        });

    }

    private void createNewItem() {
        EditText itemName = (EditText) findViewById(R.id.nameField);
        EditText itemDescription = (EditText) findViewById(R.id.descriptionField);
        EditText itemPrice = (EditText) findViewById(R.id.priceField);

        mName = itemName.getText().toString();
        mDescription = itemDescription.getText().toString();
        mPrice = itemPrice.getText().toString();
        mDate = String.valueOf(new Date());
    }

}

Item类:

import android.os.Parcel;
import android.os.Parcelable;

import java.util.Date;

public class Item implements Parcelable {

    private String mName;
    private String mInventoryDate;
    private String mDescription;
    private String mPrice;

    public Item(String name, String description, String price, String stringDate) {
        mName = name;
        mInventoryDate = stringDate;
        mDescription = description;
        mPrice = price;
    }

    public String[] getItemInfo() {
        String[] itemInfo = {mName, mDescription, mInventoryDate, mPrice};
        return itemInfo;
    }

    public String getName() {
        return mName;
    }

    public void setName(String name) {
        mName = name;
    }

    public String getInventoryDate() {
        return mInventoryDate;
    }

    public void setInventoryDate(String stringDate) {
        mInventoryDate = stringDate;
    }

    public String getDescription() {
        return mDescription;
    }

    public void setDescription(String description) {
        mDescription = description;
    }

    public String getPrice() {
        return mPrice;
    }

    public void setPrice(String price) {
        mPrice = price;
    }

    @Override
    public String toString() {
        return mName + " " + mInventoryDate + " " + mDescription + " " + mPrice;
    }

    @Override
    public int describeContents() {
        return 0; //Not necessary...
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(mName);
        dest.writeString(mInventoryDate);
        dest.writeString(mDescription);
        dest.writeString(mPrice);
    }

    private Item(Parcel in) {
        mName = in.readString();
        mInventoryDate = in.readString();
        mDescription = in.readString();
        mPrice = in.readString();
    }

    public static final Creator<Item> CREATOR = new Creator<Item>() {
        @Override
        public Item createFromParcel(Parcel source) {
            return new Item(source);
        }

        @Override
        public Item[] newArray(int size) {
            return new Item[size];
        }
    };

}

库存类:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class Inventory {

    private List<Item> mInventoryItems;

    public Inventory() {
        mInventoryItems = new ArrayList<>();
    }

    public int getSize() {
        return mInventoryItems.size();
    }

    public void addItem(Item item) {
        mInventoryItems.add(item);
    }

    public Item getPosition(int position) {
        return mInventoryItems.get(position);
    }

    public List<Item> getInventoryItems() {
        return mInventoryItems;
    }

    public void setInventoryItems(List<Item> items) {
        mInventoryItems = items;
    }

    @Override
    public String toString() {
        return "Inventory{" +
                "mInventoryItems=" + mInventoryItems +
                '}';
    }

}

我已经在这里待了大约18个小时试图解决这个问题,我只是错过了最后一篇文章,我才知道。

1 个答案:

答案 0 :(得分:3)

问题出在onActivityResult()方法中,您可以致电:

Intent intent = getIntent();

getIntent()返回用于创建Activity的Intent。您希望使用传递给onActivityResult()方法的Intent,所以:

mNewItem = data.getParcelableExtra(AddItemActivity.ADD_ITEM);