我正在创建一个使用这些类的Android应用程序 - Graph,Vertex和Edge 。 图表类具有列表顶点和列表边。 Vertex类具有 List inComing 和 List outGoing 。 vertices是Vertex,edges,inComing和outGoing类型Edge
的列表处理android代码的类是 AddNewGroup.java 和 AddGroupData.java 。
我在 AddNewGroup.java 中创建一个Graph对象,然后使用下面给出的cose将其发送到 AddGroupData.java :
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");
但是,只要执行 getParcelableExtra(" GRAPH"),应用就会崩溃。在调试应用程序时,我发现 InvocationTargetException 有一个例外,我认为由于Parcel作为null传递,它给出了 NullPointException 。
我试图找到更好的方法来找到将复杂对象从一个活动发送到另一个活动的方法,但失败了。请澄清复杂对象的传递,特别是那些在形式或列表中有很多对其他对象的引用的对象。
Graph.java
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];
}
};
}
Vertex.java
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];
}
};
}
Edge.java
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];
}
};
}