Java 将可包裹的自定义对象从一个活动发送到另一个活动时调用TargetException
我正在创建一个android应用程序,它使用这些类:Graph、Vertex和Edge。 Graph类具有列表顶点和列表边。 顶点类具有列表传入和列表传出。 顶点是类型顶点、边、边的传入和传出的列表 处理android代码的类是AddNewGroup.java和AddGroupData.java 我在AddNewGroup.java中创建一个Graph对象,然后使用下面给出的cose将其发送到AddGroupData.java:Java 将可包裹的自定义对象从一个活动发送到另一个活动时调用TargetException,java,android,parcelable,parcel,invocationtargetexception,Java,Android,Parcelable,Parcel,Invocationtargetexception,我正在创建一个android应用程序,它使用这些类:Graph、Vertex和Edge。 Graph类具有列表顶点和列表边。 顶点类具有列表传入和列表传出。 顶点是类型顶点、边、边的传入和传出的列表 处理android代码的类是AddNewGroup.java和AddGroupData.java 我在AddNewGroup.java中创建一个Graph对象,然后使用下面给出的cose将其发送到AddGroupData.java: AddNewGroup.java Graph g=new Gra
AddNewGroup.java
Graph g=new Graph(name,doc,desc);
Intent intent=new Intent(this,AddGroupData.class);
intent.putExtra("GRAPH",g);
startActivity(intent);
AddGroupData.java
public void onSave(View view){
Intent intent=this.getIntent();
g=intent.getParcelableExtra("GRAPH");
import android.os.Parcel;
import android.os.Parcelable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class Graph<T> implements Parcelable {
private final String name;
private final String doc;//date of creation
private final String desc;
private List<Vertex> vertices;//(+)outGoing,inComing(-)
private List<Edge> edges;//getFrom(),getTo()
public Graph(String name,String doc,String desc){
this.name=name;
this.doc=doc;
this.desc=desc;
vertices=new ArrayList<>();
edges=new ArrayList<>();
}
public List<Vertex> getVertices(){return vertices;}
public boolean addToGraph(String from, String to, int amount,String date, String desc){
boolean fP=false,tP=false;
Vertex<T> f=null,t=null;
for(Vertex<T> v:vertices){
if((v.getName()).equals(from)){
fP=true;
f=v;
}
if((v.getName()).equals(to)){
tP=true;
t=v;
}
}
if(fP==true && tP==true){
boolean edgeExists=false;
for(Edge<T> e:edges){//todo-make it iterate in f.outGoing
if((e.getFrom())==f &&(e.getTo())==t) {
e.appendCost(amount);
edgeExists=true;
return true;
}
}
if(edgeExists==false){
Edge<T> e=new Edge<>(f,t,amount,date,desc);
f.addOutGoing(e);
t.addIncoming(e);
edges.add(e);
return true;
}
}
else if(fP==true&& tP==false){
t = new Vertex<>(to);
Edge<T> e=new Edge<>(f,t,amount,date,desc);
vertices.add(t);
edges.add(e);
f.addOutGoing(e);
t.addIncoming(e);
return true;
}
else if(fP==false && tP==true){
f=new Vertex<>(from);
Edge<T> e=new Edge<>(f,t,amount,date,desc);
vertices.add(f);
edges.add(e);
f.addOutGoing(e);
t.addIncoming(e);
return true;
}
else{
f=new Vertex<>(from);
t=new Vertex<>(to);
Edge<T> e=new Edge<>(f,t,amount,date,desc);
vertices.add(f);
vertices.add(t);
edges.add(e);
f.addOutGoing(e);
t.addIncoming(e);
return true;
}
return false;
}
@Override
public int describeContents() {
return hashCode();
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeString(doc);
dest.writeString(desc);
dest.writeTypedList(vertices);
dest.writeTypedList(edges);
}
private Graph(Parcel p){
name=p.readString();
doc=p.readString();
desc=p.readString();
p.readTypedList(vertices,Vertex.CREATOR);
p.readTypedList(edges,Edge.CREATOR);
}
public static final Parcelable.Creator<Graph> CREATOR=new Parcelable.Creator<Graph>(){
@Override
public Graph createFromParcel(Parcel source) {
return new Graph(source);
}
@Override
public Graph[] newArray(int size) {
return new Graph[size];
}
};
}
import android.os.Parcel;
import android.os.Parcelable;
import java.util.ArrayList;
import java.util.List;
public class Vertex<T> implements Parcelable {
private String name=null;
private List<Edge> outGoing;
private List<Edge> inComing;
int am;
public String getName(){
return name;
}
public List<Edge> getOutGoing() {
return outGoing;
}
public List<Edge> getInComing(){
return inComing;
}
public boolean addOutGoing(Edge<T> e){
outGoing.add(e);
return true;
}
public boolean addIncoming(Edge<T> e){
inComing.add(e);
return true;
}
public boolean setAm(int am){this.am=am; return true;}
public int getAm(){return am;}
public Vertex(String name){
this.name=name;
outGoing=new ArrayList<>();
inComing=new ArrayList<>();
am=0;
}
public Vertex(String name, int am){
this.name=name;
outGoing=new ArrayList<>();
inComing=new ArrayList<>();
this.am=am;
}
@Override
public int describeContents() {
return hashCode();
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeInt(am);
dest.writeTypedList(outGoing);
dest.writeTypedList(inComing);
}
private Vertex(Parcel p){
name=p.readString();
am=p.readInt();
p.readTypedList(outGoing,Edge.CREATOR);
p.readTypedList(inComing,Edge.CREATOR);
}
public static final Parcelable.Creator<Vertex>CREATOR=new Parcelable.Creator<Vertex>(){
@Override
public Vertex createFromParcel(Parcel source) {
return new Vertex(source);
}
@Override
public Vertex[] newArray(int size) {
return new Vertex[size];
}
};
}
import android.os.Parcel;
import android.os.Parcelable;
import android.widget.EditText;
public class Edge<T> implements Parcelable{
private Vertex<T> f;//if f is final then it will have to be instantiated by every constructor
private Vertex<T> t;
private int amount;
String desc;
String date;
public Vertex<T> getFrom(){
return f;
}
public Vertex<T> getTo(){
return t;
}
public int getAmount(){
return amount;
}
public boolean appendCost(int c){
amount+=c;
return true;
}
public Edge(Vertex<T> from,Vertex<T> to, int cost,String date,String desc){
f=from;
t=to;
amount=cost;
this.desc=desc;
this.date=date;
}
public Edge(Vertex<T>from, Vertex<T> to, int cost){
f=from;
t=to;
amount=cost;
desc=null;
date=null;
}
@Override
public int describeContents() {
return hashCode();
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(amount);
dest.writeString(date);
dest.writeString(desc);
dest.writeParcelable(f, flags);
dest.writeParcelable(t,flags);
}
private Edge(Parcel p){
amount=p.readInt();
date=p.readString();
desc=p.readString();
f=p.readParcelable(Vertex.class.getClassLoader());
t=p.readParcelable(Vertex.class.getClassLoader());
}
public static final Parcelable.Creator<Edge> CREATOR=new Parcelable.Creator<Edge>(){//why cant we do Creator<Edge<T>>
@Override
public Edge createFromParcel(Parcel source) {
return new Edge(source);
}
@Override
public Edge[] newArray(int size) {
return new Edge[size];
}
};
}
但一旦执行getParcelableExtra(“图形”),应用程序就会崩溃。在调试应用程序时,我发现InvocationTargetException存在一个异常,我认为这是由于将包作为null传递,这会给出一个NullPointException
我试图找到更好的方法来将复杂对象从一个活动发送到另一个活动,但失败了。请澄清如何将复杂对象传递给,尤其是那些在表单或列表中有大量引用其他对象的对象,以及类似的对象
Graph.java
public void onSave(View view){
Intent intent=this.getIntent();
g=intent.getParcelableExtra("GRAPH");
import android.os.Parcel;
import android.os.Parcelable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class Graph<T> implements Parcelable {
private final String name;
private final String doc;//date of creation
private final String desc;
private List<Vertex> vertices;//(+)outGoing,inComing(-)
private List<Edge> edges;//getFrom(),getTo()
public Graph(String name,String doc,String desc){
this.name=name;
this.doc=doc;
this.desc=desc;
vertices=new ArrayList<>();
edges=new ArrayList<>();
}
public List<Vertex> getVertices(){return vertices;}
public boolean addToGraph(String from, String to, int amount,String date, String desc){
boolean fP=false,tP=false;
Vertex<T> f=null,t=null;
for(Vertex<T> v:vertices){
if((v.getName()).equals(from)){
fP=true;
f=v;
}
if((v.getName()).equals(to)){
tP=true;
t=v;
}
}
if(fP==true && tP==true){
boolean edgeExists=false;
for(Edge<T> e:edges){//todo-make it iterate in f.outGoing
if((e.getFrom())==f &&(e.getTo())==t) {
e.appendCost(amount);
edgeExists=true;
return true;
}
}
if(edgeExists==false){
Edge<T> e=new Edge<>(f,t,amount,date,desc);
f.addOutGoing(e);
t.addIncoming(e);
edges.add(e);
return true;
}
}
else if(fP==true&& tP==false){
t = new Vertex<>(to);
Edge<T> e=new Edge<>(f,t,amount,date,desc);
vertices.add(t);
edges.add(e);
f.addOutGoing(e);
t.addIncoming(e);
return true;
}
else if(fP==false && tP==true){
f=new Vertex<>(from);
Edge<T> e=new Edge<>(f,t,amount,date,desc);
vertices.add(f);
edges.add(e);
f.addOutGoing(e);
t.addIncoming(e);
return true;
}
else{
f=new Vertex<>(from);
t=new Vertex<>(to);
Edge<T> e=new Edge<>(f,t,amount,date,desc);
vertices.add(f);
vertices.add(t);
edges.add(e);
f.addOutGoing(e);
t.addIncoming(e);
return true;
}
return false;
}
@Override
public int describeContents() {
return hashCode();
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeString(doc);
dest.writeString(desc);
dest.writeTypedList(vertices);
dest.writeTypedList(edges);
}
private Graph(Parcel p){
name=p.readString();
doc=p.readString();
desc=p.readString();
p.readTypedList(vertices,Vertex.CREATOR);
p.readTypedList(edges,Edge.CREATOR);
}
public static final Parcelable.Creator<Graph> CREATOR=new Parcelable.Creator<Graph>(){
@Override
public Graph createFromParcel(Parcel source) {
return new Graph(source);
}
@Override
public Graph[] newArray(int size) {
return new Graph[size];
}
};
}
import android.os.Parcel;
import android.os.Parcelable;
import java.util.ArrayList;
import java.util.List;
public class Vertex<T> implements Parcelable {
private String name=null;
private List<Edge> outGoing;
private List<Edge> inComing;
int am;
public String getName(){
return name;
}
public List<Edge> getOutGoing() {
return outGoing;
}
public List<Edge> getInComing(){
return inComing;
}
public boolean addOutGoing(Edge<T> e){
outGoing.add(e);
return true;
}
public boolean addIncoming(Edge<T> e){
inComing.add(e);
return true;
}
public boolean setAm(int am){this.am=am; return true;}
public int getAm(){return am;}
public Vertex(String name){
this.name=name;
outGoing=new ArrayList<>();
inComing=new ArrayList<>();
am=0;
}
public Vertex(String name, int am){
this.name=name;
outGoing=new ArrayList<>();
inComing=new ArrayList<>();
this.am=am;
}
@Override
public int describeContents() {
return hashCode();
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeInt(am);
dest.writeTypedList(outGoing);
dest.writeTypedList(inComing);
}
private Vertex(Parcel p){
name=p.readString();
am=p.readInt();
p.readTypedList(outGoing,Edge.CREATOR);
p.readTypedList(inComing,Edge.CREATOR);
}
public static final Parcelable.Creator<Vertex>CREATOR=new Parcelable.Creator<Vertex>(){
@Override
public Vertex createFromParcel(Parcel source) {
return new Vertex(source);
}
@Override
public Vertex[] newArray(int size) {
return new Vertex[size];
}
};
}
import android.os.Parcel;
import android.os.Parcelable;
import android.widget.EditText;
public class Edge<T> implements Parcelable{
private Vertex<T> f;//if f is final then it will have to be instantiated by every constructor
private Vertex<T> t;
private int amount;
String desc;
String date;
public Vertex<T> getFrom(){
return f;
}
public Vertex<T> getTo(){
return t;
}
public int getAmount(){
return amount;
}
public boolean appendCost(int c){
amount+=c;
return true;
}
public Edge(Vertex<T> from,Vertex<T> to, int cost,String date,String desc){
f=from;
t=to;
amount=cost;
this.desc=desc;
this.date=date;
}
public Edge(Vertex<T>from, Vertex<T> to, int cost){
f=from;
t=to;
amount=cost;
desc=null;
date=null;
}
@Override
public int describeContents() {
return hashCode();
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(amount);
dest.writeString(date);
dest.writeString(desc);
dest.writeParcelable(f, flags);
dest.writeParcelable(t,flags);
}
private Edge(Parcel p){
amount=p.readInt();
date=p.readString();
desc=p.readString();
f=p.readParcelable(Vertex.class.getClassLoader());
t=p.readParcelable(Vertex.class.getClassLoader());
}
public static final Parcelable.Creator<Edge> CREATOR=new Parcelable.Creator<Edge>(){//why cant we do Creator<Edge<T>>
@Override
public Edge createFromParcel(Parcel source) {
return new Edge(source);
}
@Override
public Edge[] newArray(int size) {
return new Edge[size];
}
};
}
导入android.os.packet;
导入android.os.Parcelable;
导入java.util.ArrayList;
导入java.util.Collections;
导入java.util.Comparator;
导入java.util.List;
公共类图实现了Parcelable{
私有最终字符串名;
私有最终字符串doc;//创建日期
私有最终字符串描述;
私有列表顶点;/(+)传出,传入(-)
私有列表边;//getFrom(),getTo()
公共图(字符串名称、字符串文档、字符串描述){
this.name=name;
this.doc=doc;
this.desc=desc;
顶点=新的ArrayList();
边=新的ArrayList();
}
公共列表getVertices(){返回顶点;}
公共布尔添加图(字符串起始、字符串结束、整数金额、字符串日期、字符串描述){
布尔值fP=false,tP=false;
顶点f=null,t=null;
对于(顶点v:顶点){
如果((v.getName()).equals(from)){
fP=真;
f=v;
}
如果((v.getName()).equals(to)){
tP=真;
t=v;
}
}
如果(fP==true&&tP==true){
布尔边存在=假;
对于(边e:edges){//todo使其在f中迭代
if((e.getFrom())==f&&(e.getTo())==t){
e、 追加成本(金额);
edgeExists=真;
返回true;
}
}
如果(edgeExists==false){
边缘e=新边缘(f、t、金额、日期、描述);
f、 (e);
t、 增补(e);
加入(e);
返回true;
}
}
else if(fP==true&&tP==false){
t=新顶点(到);
边缘e=新边缘(f、t、金额、日期、描述);
顶点。添加(t);
加入(e);
f、 (e);
t、 增补(e);
返回true;
}
else if(fP==false&&tP==true){
f=新顶点(从);
边缘e=新边缘(f、t、金额、日期、描述);
顶点。添加(f);
加入(e);
f、 (e);
t、 增补(e);
返回true;
}
否则{
f=新顶点(从);
t=新顶点(到);
边缘e=新边缘(f、t、金额、日期、描述);
顶点。添加(f);
顶点。添加(t);
加入(e);
f、 (e);
t、 增补(e);
返回true;
}
返回false;
}
@凌驾
公共int描述内容(){
返回hashCode();
}
@凌驾
公共无效写入包裹(包裹目的地,内部标志){
目的地书面记录(名称);
目的地书面资本(doc);
目的地记录(说明);
目标writeTypedList(顶点);
目标写入列表(边缘);
}
专用图(地块p){
name=p.readString();
doc=p.readString();
desc=p.readString();
p、 readTypedList(顶点,Vertex.CREATOR);
p、 readTypedList(edges,Edge.CREATOR);
}
public static final Parcelable.Creator=新建Parcelable.Creator(){
@凌驾
公共图形createFromParcel(地块源){
返回新图形(源);
}
@凌驾
公共图[]新数组(整数大小){
返回新图形[大小];
}
};
}
Vertex.java
public void onSave(View view){
Intent intent=this.getIntent();
g=intent.getParcelableExtra("GRAPH");
import android.os.Parcel;
import android.os.Parcelable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class Graph<T> implements Parcelable {
private final String name;
private final String doc;//date of creation
private final String desc;
private List<Vertex> vertices;//(+)outGoing,inComing(-)
private List<Edge> edges;//getFrom(),getTo()
public Graph(String name,String doc,String desc){
this.name=name;
this.doc=doc;
this.desc=desc;
vertices=new ArrayList<>();
edges=new ArrayList<>();
}
public List<Vertex> getVertices(){return vertices;}
public boolean addToGraph(String from, String to, int amount,String date, String desc){
boolean fP=false,tP=false;
Vertex<T> f=null,t=null;
for(Vertex<T> v:vertices){
if((v.getName()).equals(from)){
fP=true;
f=v;
}
if((v.getName()).equals(to)){
tP=true;
t=v;
}
}
if(fP==true && tP==true){
boolean edgeExists=false;
for(Edge<T> e:edges){//todo-make it iterate in f.outGoing
if((e.getFrom())==f &&(e.getTo())==t) {
e.appendCost(amount);
edgeExists=true;
return true;
}
}
if(edgeExists==false){
Edge<T> e=new Edge<>(f,t,amount,date,desc);
f.addOutGoing(e);
t.addIncoming(e);
edges.add(e);
return true;
}
}
else if(fP==true&& tP==false){
t = new Vertex<>(to);
Edge<T> e=new Edge<>(f,t,amount,date,desc);
vertices.add(t);
edges.add(e);
f.addOutGoing(e);
t.addIncoming(e);
return true;
}
else if(fP==false && tP==true){
f=new Vertex<>(from);
Edge<T> e=new Edge<>(f,t,amount,date,desc);
vertices.add(f);
edges.add(e);
f.addOutGoing(e);
t.addIncoming(e);
return true;
}
else{
f=new Vertex<>(from);
t=new Vertex<>(to);
Edge<T> e=new Edge<>(f,t,amount,date,desc);
vertices.add(f);
vertices.add(t);
edges.add(e);
f.addOutGoing(e);
t.addIncoming(e);
return true;
}
return false;
}
@Override
public int describeContents() {
return hashCode();
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeString(doc);
dest.writeString(desc);
dest.writeTypedList(vertices);
dest.writeTypedList(edges);
}
private Graph(Parcel p){
name=p.readString();
doc=p.readString();
desc=p.readString();
p.readTypedList(vertices,Vertex.CREATOR);
p.readTypedList(edges,Edge.CREATOR);
}
public static final Parcelable.Creator<Graph> CREATOR=new Parcelable.Creator<Graph>(){
@Override
public Graph createFromParcel(Parcel source) {
return new Graph(source);
}
@Override
public Graph[] newArray(int size) {
return new Graph[size];
}
};
}
import android.os.Parcel;
import android.os.Parcelable;
import java.util.ArrayList;
import java.util.List;
public class Vertex<T> implements Parcelable {
private String name=null;
private List<Edge> outGoing;
private List<Edge> inComing;
int am;
public String getName(){
return name;
}
public List<Edge> getOutGoing() {
return outGoing;
}
public List<Edge> getInComing(){
return inComing;
}
public boolean addOutGoing(Edge<T> e){
outGoing.add(e);
return true;
}
public boolean addIncoming(Edge<T> e){
inComing.add(e);
return true;
}
public boolean setAm(int am){this.am=am; return true;}
public int getAm(){return am;}
public Vertex(String name){
this.name=name;
outGoing=new ArrayList<>();
inComing=new ArrayList<>();
am=0;
}
public Vertex(String name, int am){
this.name=name;
outGoing=new ArrayList<>();
inComing=new ArrayList<>();
this.am=am;
}
@Override
public int describeContents() {
return hashCode();
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeInt(am);
dest.writeTypedList(outGoing);
dest.writeTypedList(inComing);
}
private Vertex(Parcel p){
name=p.readString();
am=p.readInt();
p.readTypedList(outGoing,Edge.CREATOR);
p.readTypedList(inComing,Edge.CREATOR);
}
public static final Parcelable.Creator<Vertex>CREATOR=new Parcelable.Creator<Vertex>(){
@Override
public Vertex createFromParcel(Parcel source) {
return new Vertex(source);
}
@Override
public Vertex[] newArray(int size) {
return new Vertex[size];
}
};
}
import android.os.Parcel;
import android.os.Parcelable;
import android.widget.EditText;
public class Edge<T> implements Parcelable{
private Vertex<T> f;//if f is final then it will have to be instantiated by every constructor
private Vertex<T> t;
private int amount;
String desc;
String date;
public Vertex<T> getFrom(){
return f;
}
public Vertex<T> getTo(){
return t;
}
public int getAmount(){
return amount;
}
public boolean appendCost(int c){
amount+=c;
return true;
}
public Edge(Vertex<T> from,Vertex<T> to, int cost,String date,String desc){
f=from;
t=to;
amount=cost;
this.desc=desc;
this.date=date;
}
public Edge(Vertex<T>from, Vertex<T> to, int cost){
f=from;
t=to;
amount=cost;
desc=null;
date=null;
}
@Override
public int describeContents() {
return hashCode();
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(amount);
dest.writeString(date);
dest.writeString(desc);
dest.writeParcelable(f, flags);
dest.writeParcelable(t,flags);
}
private Edge(Parcel p){
amount=p.readInt();
date=p.readString();
desc=p.readString();
f=p.readParcelable(Vertex.class.getClassLoader());
t=p.readParcelable(Vertex.class.getClassLoader());
}
public static final Parcelable.Creator<Edge> CREATOR=new Parcelable.Creator<Edge>(){//why cant we do Creator<Edge<T>>
@Override
public Edge createFromParcel(Parcel source) {
return new Edge(source);
}
@Override
public Edge[] newArray(int size) {
return new Edge[size];
}
};
}
导入android.os.packet;
导入android.os.Parcelable;
导入java.util.ArrayList;
导入java.util.List;
公共类Vertex实现了Parcelable{
私有字符串名称=null;
私有列表输出;
私有列表传入;
国际调幅;
公共字符串getName(){
返回名称;
}
公共列表getOutGoing(){
返出;
}
公共列表getInComing(){
返回输入;
}
公共布尔addouting(边e){
添加(e);
返回true;
}
公共布尔加法(边e){
加入(e);
返回true;
}
公共布尔setAm(intam){this.am=am;返回true;}
public int getAm(){return am;}
公共顶点(字符串名称){
this.name=name;
传出=新的ArrayList();
传入=新的ArrayList();
am=0;
}
公共顶点(字符串名称,int-am){
this.name=name;