我不确定我的代码怎么了。我确保所有对象实现可序列化。我已经看过其他问题,但仍然找不到解决方案。有人可以帮我找出怎么了吗?谢谢你
这就是logcat中所说的
12-04 00:54:15.826 5602-5602/com.b07.joesapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.b07.joesapp, PID: 5602
java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = com.b07.joesapp.admin.AdminInterfaceImpl)
at android.os.Parcel.writeSerializable(Parcel.java:1822)
at android.os.Parcel.writeValue(Parcel.java:1770)
at android.os.Parcel.writeArrayMapInternal(Parcel.java:838)
at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1542)
at android.os.Bundle.writeToParcel(Bundle.java:1232)
at android.os.Parcel.writeBundle(Parcel.java:878)
at android.content.Intent.writeToParcel(Intent.java:9595)
at android.app.IActivityManager$Stub$Proxy.startActivity(IActivityManager.java:4327)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1611)
at android.app.Activity.startActivityForResult(Activity.java:4488)
at android.support.v4.app.BaseFragmentActivityApi16.startActivityForResult(BaseFragmentActivityApi16.java:54)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:67)
at android.app.Activity.startActivityForResult(Activity.java:4446)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:720)
at android.app.Activity.startActivity(Activity.java:4807)
at android.app.Activity.startActivity(Activity.java:4775)
at com.b07.joesapp.adminMenu.AdminActionsController.onClick(AdminActionsController.java:49)
at android.view.View.performClick(View.java:6294)
at android.view.View$PerformClick.run(View.java:24770)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.io.NotSerializableException: com.b07.joesapp.adminMenu.AdminHomePage
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1233)
at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1597)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1558)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1481)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1227)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:347)
at android.os.Parcel.writeSerializable(Parcel.java:1817)
at android.os.Parcel.writeValue(Parcel.java:1770)
at android.os.Parcel.writeArrayMapInternal(Parcel.java:838)
at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1542)
at android.os.Bundle.writeToParcel(Bundle.java:1232)
at android.os.Parcel.writeBundle(Parcel.java:878)
at android.content.Intent.writeToParcel(Intent.java:9595)
at android.app.IActivityManager$Stub$Proxy.startActivity(IActivityManager.java:4327)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1611)
at android.app.Activity.startActivityForResult(Activity.java:4488)
at android.support.v4.app.BaseFragmentActivityApi16.startActivityForResult(BaseFragmentActivityApi16.java:54)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:67)
at android.app.Activity.startActivityForResult(Activity.java:4446)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:720)
at android.app.Activity.startActivity(Activity.java:4807)
at android.app.Activity.startActivity(Activity.java:4775)
at com.b07.joesapp.adminMenu.AdminActionsController.onClick(AdminActionsController.java:49)
at android.view.View.performClick(View.java:6294)
at android.view.View$PerformClick.run(View.java:24770)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
这是我的课。adminhomePage
package com.b07.joesapp.adminMenu;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import com.b07.joesapp.R;
import com.b07.joesapp.admin.VoiceRecognitionAdminController;
import com.b07.users.Admin;
public class AdminHomePage extends AppCompatActivity {
private Admin admin;
private Button toAdminActions;
private Button talkAdminCtrl;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_admin_home_page);
// get the admin and the inventory to create an AdminInterfaceImpl object to be passed on
admin = (Admin) getIntent().getSerializableExtra("adminUser");
AdminActionsController controller = new AdminActionsController(this, admin);
toAdminActions = (Button) findViewById(R.id.toAdminActionsBtn);
toAdminActions.setOnClickListener(controller);
// this is what happens when the voice control button is clicked
talkAdminCtrl= (Button) findViewById(R.id.voiceControlAdminActionsBtn);
VoiceRecognitionAdminController voiceRecognitionAdmin = new VoiceRecognitionAdminController(this, admin);
talkAdminCtrl.setOnClickListener(voiceRecognitionAdmin);
}
}
AdminactionsController
package com.b07.joesapp.adminMenu;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import com.b07.database.DatabaseSelectHelper;
import com.b07.exceptions.DatabaseException;
import com.b07.exceptions.interfaceexceptions.UserNotAuthenticatedException;
import com.b07.inventory.Inventory;
import com.b07.joesapp.admin.AdminControls;
import com.b07.joesapp.admin.AdminInterfaceImpl;
import com.b07.users.Admin;
/**
* Created by shama on 12/1/2017.
*/
public class AdminActionsController implements View.OnClickListener {
private Context appContext;
private Admin adminUser;
private Inventory inventory;
private AdminInterfaceImpl adminInterface;
public AdminActionsController(Context context, Admin admin) {
appContext = context;
adminUser = admin;
try {
DatabaseSelectHelper selectHelper = new DatabaseSelectHelper(appContext);
inventory = selectHelper.getInventory();
} catch (DatabaseException e) {
//will not be handled
}
try {
adminInterface = new AdminInterfaceImpl(adminUser, inventory, appContext);
} catch (UserNotAuthenticatedException e) {
// do something if exception is caught
}
}
@Override
public void onClick(View view) {
Intent intent = new Intent(appContext, AdminControls.class);
intent.putExtra("adminInterface", adminInterface);
appContext.startActivity(intent);
}
}
admin
package com.b07.users;
import android.content.Context;
import java.io.Serializable;
/**
* Class that represents an admin.
*
* @author Linda
*
*/
public class Admin extends User implements Serializable {
/**
* Initializes admin.
*
* @param id the id of the admin
* @param name the name of the admin
* @param age the age of the admin
* @param address the address of the admin
*/
public Admin(int id, String name, int age, String address) {
//Sets all appropriate variables
super(id, name, age, address);
}
/**
* Initializes authenticated admin.
*
* @param id the id of the admin
* @param name the name of the admin
* @param age the age of the admin
* @param address the address of the admin
* @param authenticated the authentication of the admin
*/
public Admin
(int id, String name, int age, String address, boolean authenticated, Context context) {
super(id, name, age, address, authenticated);
}
/** promoteEmployee removed **/
}
库存
/**
*
*/
package com.b07.inventory;
import com.b07.exceptions.ItemNotFoundException;
import java.io.Serializable;
import java.util.HashMap;
/**
* Class that represents an inventory.
* @author Linda
*
*/
public class InventoryImpl implements Inventory, Serializable {
// declare variables inside InventoryImpl instance
int totalItems = 0;
HashMap<Item, Integer> itemMap = new HashMap<>();
/**
* Returns the item map of the inventory.
* @return itemMap
*/
@Override
public HashMap<Item, Integer> getItemMap() {
return this.itemMap;
}
/**
* Sets the item map of the inventory.
* @param itemMap the new item map
*/
@Override
public void setItemMap(HashMap<Item, Integer> itemMap) {
this.itemMap = itemMap;
this.setTotalItems(this.itemMap.size());
}
/**
* Changes the quantity of a given item.
* @param item the item
* @param value the quantity changed
* @throws ItemNotFoundException if item not in inventory
*/
@Override
public void updateMap(Item item, Integer value) throws ItemNotFoundException {
// if the item exists it is added if not an exception is thrown
if (this.itemMap.containsKey(item)) {
this.itemMap.put(item, value);
} else {
throw new ItemNotFoundException();
}
}
/**
* Returns the total number of items in the inventory.
* @return totalItems
*/
@Override
public int getTotalItems() {
return this.totalItems;
}
/**
* Sets the new total number of items in the inventory.
* @param total the new total
*/
@Override
public void setTotalItems(int total) {
this.totalItems = total;
}
/**
* Checks if the inventory contains the desired item.
*
* @param item the desired item
* @return true if it is in the inventory, false if not
*/
@Override
public boolean containsItem(Item item) {
if (itemMap.containsKey(item)) {
return true;
}
return false;
}
/**
* Returns the quantity of the desired item.
*
* @param item the desired item
* @return quantity the current quantity of item
* @throws ItemNotFoundException if item is not found in the inventory
*/
@Override
public int getItemQuantity(Item item) throws ItemNotFoundException {
// if the item exists then its quantity is returned otherwise an exception is thrown
if (this.itemMap.containsKey(item)) {
return this.itemMap.get(item);
}
throw new ItemNotFoundException();
}
}
我真的被困在这里。我一遍又一遍地检查所有对象都可以序列化,但我仍然不确定问题是什么。谢谢!
在此方法中
public void onClick(View view) {
Intent intent = new Intent(appContext, AdminControls.class);
intent.putExtra("adminInterface", adminInterface);
appContext.startActivity(intent);
}
您通过自定义对象,因此请尝试此
public void onClick(View view) {
Bundle bundle = new Bundle();
bundle.putSerializable("adminInterface", adminInterface);
Intent intent = new Intent(appContext, AdminControls.class);
intent.putExtras(bundle);
appContext.startActivity(intent);
}