所以我不断收到此错误“由:java.io.NotSerializableException异常:android.net.Uri $ StringUri”当我按下按钮实现以下方法:
public void shareItems() {
StringBuilder dataString = new StringBuilder();
String name = itemName.getText().toString();
String price = itemPrice.getText().toString();
String description = itemDescription.getText().toString();
String location = itemLocation.getText().toString();
dataString.append(" product: " + name + "\n");
dataString.append(" price: " + price + "\n");
dataString.append(" pick up location: " + location + "\n");
dataString.append(" description: " + description);
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(Intent.EXTRA_SUBJECT, "product for sale");
i.putExtra(Intent.EXTRA_EMAIL, new String[]{"[email protected]"});
i.putExtra(Intent.EXTRA_TEXT, dataString.toString());
try {
startActivity(Intent.createChooser(i, "share"));
} catch (ActivityNotFoundException e) {
Toast.makeText(getActivity(), "Please install a messaging app",
Toast.LENGTH_LONG).show();
}
}
public void sendWhatsapp() {
StringBuilder dataString = new StringBuilder();
String name = itemName.getText().toString();
String price = itemPrice.getText().toString();
String description = itemDescription.getText().toString();
String location = itemLocation.getText().toString();
dataString.append(" product: " + name + "\n");
dataString.append(" price: " + price + "\n");
dataString.append(" pick up location: " + location + "\n");
dataString.append(" description: " + description);
Items item = new Items();
item.setMobile(adapter.getMobile());
mobile = item.getMobile();
System.out.println(mobile);
Uri uri = Uri.parse("smsto:" + "0542001117");
Intent sendIntent = new Intent(Intent.ACTION_SENDTO, uri);
sendIntent.putExtra(Intent.EXTRA_TEXT, dataString.toString());
sendIntent.setPackage("com.whatsapp");
try {
startActivity(Intent.createChooser(sendIntent, "Contact seller"));
} catch (ActivityNotFoundException e) {
Toast.makeText(getActivity(), "Please install WhatsApp",
Toast.LENGTH_LONG).show();
}
}
根本不使用任何Uri。它说这个错误在这个类中,它实现了serialiazable,但我不明白它是如何导致问题的,或者它是如何被这些按钮触发的。
public class Items implements Serializable{
private static final long serialVersionUID = 10L;
private String itemDescription;
private String itemLocation;
private Uri itemImage;
private String itemName;
private int itemPrice;
private int itemID;
private String mobile;
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public static long getSerialVersionUID() {
return serialVersionUID;
}
public String getItemName() {
return itemName;
}
public void setItemName(String itemName) {
this.itemName = itemName;
}
public int getItemPrice() {
return itemPrice;
}
public void setItemPrice(int itemPrice) {
this.itemPrice = itemPrice;
}
public int getItemID() {
return itemID;
}
public void setItemID(int itemID) {
this.itemID = itemID;
}
public String getItemDescription() {
return itemDescription;
}
public void setItemDescription(String itemDescription) {
this.itemDescription = itemDescription;
}
public String getItemLocation() {
return itemLocation;
}
public void setItemLocation(String itemLocation) {
this.itemLocation = itemLocation;
}
public Uri getItemImage() {
return itemImage;
}
public void setItemImage(Uri itemImage) {
this.itemImage = itemImage;
}
public Items(String item, int price, int id, String description, String location, Uri image, String phone){
itemName = item;
itemPrice = price;
itemID = id;
itemDescription = description;
itemLocation = location;
itemImage = image;
mobile = phone;
}
public Items(){
}
}
编辑:这是我使用的序列化:
public class CustomListviewAdapter extends ArrayAdapter<Items> {
private int layoutResource;
private Activity activity;
static FragmentManager fragmentManager;
private Context context;
private ArrayList<Items> itemList = new ArrayList<>();
public CustomListviewAdapter(Activity act, int resource, ArrayList<Items> data) {
super(act, resource, data);
layoutResource = resource;
activity = act;
itemList = data;
notifyDataSetChanged();
}
@Override
public int getCount() {
return itemList.size();
}
@Override
public Items getItem(int position) {
return itemList.get(position);
}
@Override
public int getPosition(Items item) {
return super.getPosition(item);
}
@Override
public long getItemId(int position) {
return super.getItemId(position);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ViewHolder holder = null;
if (row == null || (row.getTag() == null)) {
LayoutInflater inflater = LayoutInflater.from(activity);
row = inflater.inflate(layoutResource, null);
holder = new ViewHolder();
holder.itemName = (TextView) row.findViewById(R.id.name);
holder.itemPrice = (TextView) row.findViewById(R.id.calories);
holder.itemImage = (ImageView) row.findViewById(R.id.thumbnail);
row.setTag(holder);
}else {
holder = (ViewHolder) row.getTag();
}
holder.item = getItem(position);
holder.itemName.setText(holder.item.getItemName());
holder.itemImage.setImageURI(holder.item.getItemImage());
holder.itemPrice.setText(String.valueOf(holder.item.getItemPrice()));
final ViewHolder finalHolder = holder;
row.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
android.support.v4.app.FragmentTransaction ft =
((FragmentActivity)activity).getSupportFragmentManager().beginTransaction();
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ItemDetails frag = new ItemDetails();
Bundle mBundle = new Bundle();
mBundle.putSerializable("userObj", finalHolder.item);
frag.setArguments(mBundle);
ft.replace(android.R.id.content, frag);
ft.addToBackStack(null);
ft.commit();
}
});
return row;
}
public class ViewHolder {
Items item;
TextView itemName;
TextView itemPrice;
ImageView itemImage;
}
}
,并在所在班级导致崩溃的方法:
public class ItemDetails extends Fragment {
private View view;
private TextView itemName, itemLocation, itemPrice, itemDescription;
private ImageView itemImage;
private Button shareButton, sendWhatsapp;
private int itemId;
private DataBaseAdapter adapter;
private String mobile;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.activity_item_details, container, false);
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
itemName = (TextView) view.findViewById(R.id.detsItemName);
itemPrice = (TextView) view.findViewById(R.id.detsPrice);
itemLocation = (TextView) view.findViewById(R.id.detsItemLocation);
itemDescription = (TextView) view.findViewById(R.id.detsDescription);
itemImage = (ImageView) view.findViewById(R.id.itemImage);
shareButton = (Button) view.findViewById(R.id.detsShare);
sendWhatsapp = (Button) view.findViewById(R.id.sendWhatsappBtn);
Bundle mBundle = getArguments();
Items item = (Items) mBundle.getSerializable("userObj");
itemName.setText(item.getItemName());
itemPrice.setText(valueOf(item.getItemPrice()));
itemLocation.setText(item.getItemLocation());
itemDescription.setText(item.getItemDescription());
itemImage.setImageURI(item.getItemImage());
adapter = new DataBaseAdapter(getActivity());
adapter.open();
itemPrice.setTextSize(34.9f);
itemPrice.setTextColor(Color.BLUE);
shareButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
shareItems();
}
});
sendWhatsapp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
sendWhatsapp();
}
});
return view;
}
public void shareItems() {
StringBuilder dataString = new StringBuilder();
String name = itemName.getText().toString();
String price = itemPrice.getText().toString();
String description = itemDescription.getText().toString();
String location = itemLocation.getText().toString();
dataString.append(" المنتج: " + name + "\n");
dataString.append(" السعر: " + price + "\n");
dataString.append(" موقع الاستلام: " + location + "\n");
dataString.append(" الوصف: " + description);
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(Intent.EXTRA_SUBJECT, "منتج للبيع");
i.putExtra(Intent.EXTRA_EMAIL, new String[]{"[email protected]"});
i.putExtra(Intent.EXTRA_TEXT, dataString.toString());
try {
startActivity(Intent.createChooser(i, "شارك"));
} catch (ActivityNotFoundException e) {
Toast.makeText(getActivity(), "المرجو تحميل برنامج ارسال",
Toast.LENGTH_LONG).show();
}
}
public void sendWhatsapp() {
StringBuilder dataString = new StringBuilder();
String name = itemName.getText().toString();
String price = itemPrice.getText().toString();
String description = itemDescription.getText().toString();
String location = itemLocation.getText().toString();
dataString.append(" المنتج: " + name + "\n");
dataString.append(" السعر: " + price + "\n");
dataString.append(" موقع الاستلام: " + location + "\n");
dataString.append(" الوصف: " + description);
Items item = new Items();
item.setMobile(adapter.getMobile());
mobile = item.getMobile();
System.out.println(mobile);
Uri uri = Uri.parse("smsto:" + "0542001117");
Intent sendIntent = new Intent(Intent.ACTION_SENDTO, uri);
sendIntent.putExtra(Intent.EXTRA_TEXT, dataString.toString());
sendIntent.setPackage("com.whatsapp");
try {
startActivity(Intent.createChooser(sendIntent, "مراسلة البائع"));
} catch (ActivityNotFoundException e) {
Toast.makeText(getActivity(), "المرجو تحميل WhatsApp",
Toast.LENGTH_LONG).show();
}
}
}再次
编辑,logcat的:
--------- beginning of crash
04-19 22:47:13.215 25143-25143/edu.seu.seniorproject.producingfamily
E/AndroidRuntime: FATAL EXCEPTION: main
Process: edu.seu.seniorproject.producingfamily, PID: 25143
java.lang.RuntimeException: Parcelable encountered IOException writing
serializable object (name =
edu.seu.seniorproject.producingfamily.item_details.Items)
at android.os.Parcel.writeSerializable(Parcel.java:1447)
at android.os.Parcel.writeValue(Parcel.java:1395)
at android.os.Parcel.writeArrayMapInternal(Parcel.java:665)
at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1330)
at android.os.Bundle.writeToParcel(Bundle.java:1079)
at android.os.Parcel.writeBundle(Parcel.java:690)
at android.support.v4.app.FragmentState.writeToParcel(Fragment.java:148)
at android.os.Parcel.writeTypedArray(Parcel.java:1233)
at android.support.v4.app.FragmentManagerState.writeToParcel(FragmentManager.java:564)
at android.os.Parcel.writeParcelable(Parcel.java:1416)
at android.os.Parcel.writeValue(Parcel.java:1322)
at android.os.Parcel.writeArrayMapInternal(Parcel.java:665)
at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1330)
at android.os.Bundle.writeToParcel(Bundle.java:1079)
at android.os.Parcel.writeBundle(Parcel.java:690)
at android.app.ActivityManagerProxy.activityStopped(ActivityManagerNative.java:3152)
at android.app.ActivityThread$StopInfo.run(ActivityThread.java:3417)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.io.NotSerializableException: android.net.Uri$StringUri
at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1344)
at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1651)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1497)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1461)
at java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:959)
at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:360)
at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1054)
at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1384)
at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1651)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1497)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1461)
at android.os.Parcel.writeSerializable(Parcel.java:1442)
at android.os.Parcel.writeValue(Parcel.java:1395)
at android.os.Parcel.writeArrayMapInternal(Parcel.java:665)
at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1330)
at android.os.Bundle.writeToParcel(Bundle.java:1079)
at android.os.Parcel.writeBundle(Parcel.java:690)
at android.support.v4.app.FragmentState.writeToParcel(Fragment.java:148)
at android.os.Parcel.writeTypedArray(Parcel.java:1233)
at android.support.v4.app.FragmentManagerState.writeToParcel(FragmentManager.java:564)
at android.os.Parcel.writeParcelable(Parcel.java:1416)
at android.os.Parcel.writeValue(Parcel.java:1322)
at android.os.Parcel.writeArrayMapInternal(Parcel.java:665)
at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1330)
at android.os.Bundle.writeToParcel(Bundle.java:1079)
at android.os.Parcel.writeBundle(Parcel.java:690)
at android.app.ActivityManagerProxy.activityStopped(ActivityManagerNative.java:3152)
at android.app.ActivityThread$StopInfo.run(ActivityThread.java:3417)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
请编辑您的问题并发布整个Java堆栈跟踪。 – CommonsWare
@CommonsWare好的没有 – Yousef