- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有一个 Android 应用程序,其中包含实现 Parcelable 接口(interface)的自定义对象。我设置它的方式是我的程序最初创建一个 ArrayList
自定义类的 Products
来自 bundle 中的文件。我可以看到并确认数组列表及其实例变量已正确填充。这个类有几个实例变量,一个是另一个ArrayList
但是有了 String
类(class)。记住这个事实。
我正在尝试通过 ArrayList<Product>
像这样进入一个新 Activity :
try {
Intent i = new Intent(RootActivity.this, ProductsActivity.class); //Intent from this activity to the next
i.putParcelableArrayListExtra("Products", app_products); //Puts my ArrayList<Class A> as an extra
startActivity(i); //Launch the activity
}
catch(Exception e){
Log.d("Activity Error", "Error Here:" + e.getMessage());
}
我正在通过拉取 ArrayList
从我的新 Activity 中的 Intent 收集信息通过使用
app_products = getIntent().getParcelableArrayListExtra("Products");
对于我的自定义类,它看起来像这样,以及已实现的 Parcelable 方法。
public class Product implements Parcelable{
private String name;
private String cost;
private ArrayList<String> similarItems;
public Product{
name = null;
cost = null;
similarItems = new ArrayList<String>();
}
public Product(String name, String cost){
this();
this.name = name;
this.cost = cost;
}
public addSimilarItem(String item){
similarItems.add(item);
}
public static final Parcelable.Creator<Product> CREATOR
= new Parcelable.Creator<Product>()
{
public Product createFromParcel(Parcel in) {
return new Product(in);
}
public Product[] newArray(int size) {
return new Product[size];
}
};
public int describeContents(){
return 0;
}
private Product(Parcel in){
name = in.readString();
cost = in.readString();
similarItems = in.readArrayList(String.class.getClassLoader());
}
public void writeToParcel(Parcel out, int flags){
out.writeString(name);
out.writeString(cost);
out.writeList(similarItems);
}
}
所以这很好用 WITHOUT 我的字符串数组列表被添加到类中
注释掉out.writeList(similarItems);
还有similarItems = in.readArrayList(String.class.getClassLoader());
但是一旦您将它们重新添加到类中,应用程序就会崩溃,但它甚至不会抛出调试消息。我已经把所有东西都包裹起来了 try-catch
语句和 android 甚至不报告应用程序崩溃与跳板上的正常对话框。我真的很茫然。
值得一提的是,尽管 android 不会抛出异常,但我已经使用了一些日志语句来了解程序崩溃的位置。我可以看到我的 ArrayList 中的所有项目都经过 writeToParcelMethod 并完成写入。永远不会调用 Product(Parcel in) 方法。最后,我还可以看到我正在启动新 Activity 的类(class)进入 Pause State
。我的新 Activity 从未创建。
如果我可以提供任何其他信息,请告诉我。
最佳答案
可以肯定您的问题是 writeList()
的使用。 writeList()
似乎表明它遵循与列表中包含的项目的 writeValue()
相同的契约(Contract)。 readList()
然而,似乎表明值必须是 Parcelable
(String
不是)。
无论哪种方式,通常这些调用都必须非常具体地链接到它们的反函数(例如,writeString()
必须用 readString()
读入,而不是 readValue()
) 所以你应该使用提供的方法来读/写 String
List
s:
// Takes in a List<String> which may be null
out.writeStringList(similarItems);
// Returns either null or a new ArrayList<String> with the contents
similarItems = in.createStringArrayList();
关于java - Android:自定义 Parcelable 类在没有抛出异常的情况下使应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25652753/
我是 Java 新手,这是我的代码, if( a.name == b.name && a.displayname == b.displayname && a.linknam
在下面的场景中,我有一个 bool 值。根据结果,我调用完全相同的函数,唯一的区别是参数的数量。 var myBoolean = ... if (myBoolean) { retrieve
我是一名研究 C++ 的 C 开发人员: 我是否正确理解如果我抛出异常然后堆栈将展开直到找到第一个异常处理程序?是否可以在不展开的情况下在任何 throw 上打开调试器(即不离开声明它的范围或任何更高
在修复庞大代码库中的错误时,我观察到一个奇怪的情况,其中引用的动态类型从原始 Derived 类型更改为 Base 类型!我提供了最少的代码来解释问题: struct Base { // some
我正在尝试用 C# 扩展给定的代码,但由于缺乏编程经验,我有点陷入困境。 使用 Visual Studio 社区,我尝试通过控制台读出 CPU 核心温度。该代码使用开关/外壳来查找传感器的特定名称(即
这可能是一个哲学问题。 假设您正在向页面发出 AJAX 请求(这是使用 Prototype): new Ajax.Request('target.asp', { method:"post", pa
我有以下 HTML 代码,我无法在所有浏览器中正常工作: 我试图在移动到
我对 Swift 很陌生。我如何从 addPin 函数中检索注释并能够在我的 addLocation 操作 (buttonPressed) 中使用它。我正在尝试使用压力触摸在 map 上添加图钉,在两
我设置了一个详细 View ,我是否有几个 Nib 文件根据在 Root View Controller 的表中选择的项目来加载。 我发现,对于 Nibs 的类,永远不会调用 viewDidUnloa
我需要动态访问 json 文件并使用以下代码。在本例中,“bpicsel”和“temp”是变量。最终结果类似于“data[0].extit1” var title="data["+bpicsel+"]
我需要使用第三方 WCF 服务。我已经在我的证书存储中配置了所需的证书,但是在调用 WCF 服务时出现以下异常。 向 https://XXXX.com/AHSharedServices/Custome
在几个 SO 答案(1、2)中,建议如果存在冲突则不应触发 INSERT 触发器,ON CONFLICT DO NOTHING 在触发语句中。也许我理解错了,但在我的实验中似乎并非如此。 这是我的 S
如果进行修改,则会给出org.hibernate.NonUniqueObjectException。在我的 BidderBO 类(class)中 @Override @Transactional(pr
我使用 indexOf() 方法来精细地查找数组中的对象。 直到此刻我查了一些资料,发现代码应该无法正常工作。 我在reducer中尝试了上面的代码,它成功了 let tmp = state.find
假设我有以下表格: CREATE TABLE Game ( GameID INT UNSIGNED NOT NULL, GameType TINYINT UNSIGNED NOT NU
代码: Alamofire.request(URL(string: imageUrl)!).downloadProgress(closure: { (progress) in
我是一名优秀的程序员,十分优秀!