我正在尝试调试这个。我通过 json 解析一些节点,并尝试找到运行 Dijkstra 算法的最短路径:
class Vertex implements Comparable<Vertex>
{
public final String name;
public Edge[] adjacencies;
public double minDistance = Double.POSITIVE_INFINITY;
public Vertex previous;
public Vertex(String argName) { name = argName; }
public String toString() { return name; }
public int compareTo(Vertex other)
{
return Double.compare(minDistance, other.minDistance);
}
}
class Edge
{
public final Vertex target;
public final double weight;
public Edge(Vertex argTarget, double argWeight)
{ target = argTarget; weight = argWeight; }
}
@SuppressLint("NewApi")
public class Dijkstra
{
public static void computePaths(Vertex source)
{
source.minDistance = 0.;
PriorityQueue<Vertex> vertexQueue = new PriorityQueue<Vertex>();
vertexQueue.add(source);
while (!vertexQueue.isEmpty()) {
Vertex u = vertexQueue.poll();
// Visit each edge exiting u
for (Edge e : u.adjacencies)
{
Vertex v = e.target;
double weight = e.weight;
double distanceThroughU = u.minDistance + weight;
if (distanceThroughU < v.minDistance) {
vertexQueue.remove(v);
v.minDistance = distanceThroughU ;
v.previous = u;
vertexQueue.add(v);
}
}
}
}
public static List<Vertex> getShortestPathTo(Vertex target)
{
List<Vertex> path = new ArrayList<Vertex>();
for (Vertex vertex = target; vertex != null; vertex = vertex.previous)
path.add(vertex);
Collections.reverse(path);
return path;
}
@SuppressLint("NewApi")
public void mainb()
{
final String url = "http://dynamicsoftworks.gr/pacman-srv/admin/json.php";
// JSON Node names
ArrayList<Vertex> ar2vertex = new ArrayList<Vertex>();
final String TAG_LEVEL = "Level";
final String TAG_MAIN = "main";
final String TAG_CONN = "conn";
final String TAG_ID = "id";
final String TAG_DISTANCE = "distance";
// contacts JSONArray
JSONArray contacts = null;
JSONArray conn = null;
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
// Hashmap for ListView
//ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>();
// Creating JSON Parser instance
JSONParser jParser = new JSONParser();
// getting JSON string from URL
JSONObject json = jParser.getJSONFromUrl(url);
try {
// Getting Array of Contacts
contacts = json.getJSONArray(TAG_LEVEL);
// looping through All Contacts
for(int i = 0; i < contacts.length(); i++){
JSONObject c = contacts.getJSONObject(i);
// Storing each json item in variable
String main = c.getString(TAG_MAIN);
conn = c.getJSONArray(TAG_CONN);
ArrayList<Edge> ar2edge = new ArrayList<Edge>();
ArrayList<Edge> armainedge = new ArrayList<Edge>();
ArrayList<Vertex> armainedge2 = new ArrayList<Vertex>();
ArrayList<Vertex> allvertexes = new ArrayList<Vertex>();
ArrayList<String> ar = new ArrayList<String>();
ArrayList<Double> ar2 = new ArrayList<Double>();
for(int is = 0; is < conn.length(); is++){
JSONObject d = conn.getJSONObject(is);
String ida = d.getString(TAG_ID);
String dinstance = d.getString(TAG_DISTANCE);
ar.add(ida);
ar2.add(Double.parseDouble(dinstance));
}
ArrayList<Vertex> arvertex = new ArrayList<Vertex>();
// creating new HashMap
ar2vertex.add(new Vertex(main.toString()));
allvertexes.add(new Vertex(main.toString()));
for (int kk=0;kk<ar.size();kk++){
//Vertex nodes[];
arvertex.add(new Vertex(ar.get(kk)));
}
// Edge[] aListOfEdges = null;
//EDWWWWW PROBL!!!
for (int kk=0;kk<arvertex.size();kk++){
// v0.adjacencies = new Edge[]{ new Edge[]{(nodes[kk], distanses[kk])}};
Edge edges = new Edge(arvertex.get(kk),ar2.get(kk));
ar2edge.add(edges);
}
Edge[] edges3 = ar2edge.toArray(new Edge[ar2edge.size()]);
for (int kp=0;kp<arvertex.size();kp++){
ar2vertex.get(i).adjacencies = edges3 ;
// System.out.println("nodes: " + ar2vertex.get(i).adjacencies);
}
computePaths(ar2vertex.get(0));
for(int mm=0;mm<ar2vertex.size();mm++){
List<Vertex> path = getShortestPathTo(ar2vertex.get(mm));
//Log.d("Test", "TAB: " + "\t" + "AAAAAAAA" + "\t" + "AAAAAAAAA");
System.out.println("Path: " + path);
}
//Arrays.fill(ids, null);
//Arrays.fill(distanses, 0);
}
} catch (JSONException e) {
e.printStackTrace();
}
/* Vertex v0 = new Vertex("par1");
Vertex v1 = new Vertex("par2");
Vertex v2 = new Vertex("par3");
Vertex v3 = new Vertex("par4");
Vertex v4 = new Vertex("par5");
Vertex v5 = new Vertex("par6");
Vertex v6 = new Vertex("par7");
v0.adjacencies = new Edge[]{ new Edge(v1, 79.83),
new Edge(v5, 81.15) };
v1.adjacencies = new Edge[]{ new Edge(v0, 79.75),
new Edge(v2, 39.42),
new Edge(v3, 103.00) };
v2.adjacencies = new Edge[]{ new Edge(v1, 38.65) };
v3.adjacencies = new Edge[]{ new Edge(v1, 102.53),
new Edge(v5, 61.44),
new Edge(v6, 96.79) };
v4.adjacencies = new Edge[]{ new Edge(v5, 133.04) };
v5.adjacencies = new Edge[]{ new Edge(v0, 81.77),
new Edge(v3, 62.05),
new Edge(v4, 134.47),
new Edge(v6, 91.63) };
v6.adjacencies = new Edge[]{ new Edge(v3, 97.24),
new Edge(v5, 87.94) };*/
}
public static String fff(){
return "hi";
}
}
但我收到此错误:10-12 00:33:22.834: E/AndroidRuntime(26772): FATAL EXCEPTION: main 10-12 00:33:22.834: E/AndroidRuntime(26772): java.lang.RuntimeException: Unable to开始活动 ComponentInfo{com.pvsgames.pacman/com.pvsgames.pacman.MainActivity}: java.lang.NullPointerException 10-12 00:33:22.834: E/AndroidRuntime(26772): at android.app.ActivityThread.performLaunchActivity(ActivityThread .java:2247) 10-12 00:33:22.834: E/AndroidRuntime(26772): 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2297) 10-12 00:33:22.834: E/AndroidRuntime(26772 ): 在 android.app.ActivityThread.access$700(ActivityThread.java:152) 10-12 00:33:22.834: E/AndroidRuntime(26772): 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1282 ) 10-12 00:33:22.834: E/AndroidRuntime(26772): 在 android.os.Handler。dispatchMessage(Handler.java:99) 10-12 00:33:22.834: E/AndroidRuntime(26772): 在 android.os.Looper.loop(Looper.java:137) 10-12 00:33:22.834: E/ AndroidRuntime(26772): 在 android.app.ActivityThread.main(ActivityThread.java:5328) 10-12 00:33:22.834: E/AndroidRuntime(26772): 在 java.lang.reflect.Method.invokeNative(Native Method) 10-12 00:33:22.834: E/AndroidRuntime(26772): 在 java.lang.reflect.Method.invoke(Method.java:511) 10-12 00:33:22.834: E/AndroidRuntime(26772): 在com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102) 10-12 00:33:22.834: E/AndroidRuntime(26772): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit .java:869) 10-12 00:33:22.834: E/AndroidRuntime(26772): at dalvik.system.NativeStart.main(Native Method) 10-12 00:33:22.834: E/AndroidRuntime(26772): Caused作者:java.lang。NullPointerException 10-12 00:33:22.834: E/AndroidRuntime(26772): 在 com.pvsgames.pacman.Dijkstra.computePaths(Dijkstra.java:53) 10-12 00:33:22.834: E/AndroidRuntime(26772):在 com.pvsgames.pacman.Dijkstra.mainb(Dijkstra.java:169) 10-12 00:33:22.834: E/AndroidRuntime(26772): 在 com.pvsgames.pacman.MainActivity.onCreate(MainActivity.java:20) 10-12 00:33:22.834: E/AndroidRuntime(26772): 在 android.app.Activity.performCreate(Activity.java:5250) 10-12 00:33:22.834: E/AndroidRuntime(26772): 在 android. app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097) 10-12 00:33:22.834: E/AndroidRuntime(26772): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)E/AndroidRuntime(26772): 在 com.pvsgames.pacman.Dijkstra.mainb(Dijkstra.java:169) 10-12 00:33:22.834: E/AndroidRuntime(26772): 在 com.pvsgames.pacman.MainActivity.onCreate (MainActivity.java:20) 10-12 00:33:22.834: E/AndroidRuntime(26772): 在 android.app.Activity.performCreate(Activity.java:5250) 10-12 00:33:22.834: E/AndroidRuntime (26772): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097) 10-12 00:33:22.834: E/AndroidRuntime(26772): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)E/AndroidRuntime(26772): 在 com.pvsgames.pacman.Dijkstra.mainb(Dijkstra.java:169) 10-12 00:33:22.834: E/AndroidRuntime(26772): 在 com.pvsgames.pacman.MainActivity.onCreate (MainActivity.java:20) 10-12 00:33:22.834: E/AndroidRuntime(26772): 在 android.app.Activity.performCreate(Activity.java:5250) 10-12 00:33:22.834: E/AndroidRuntime (26772): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097) 10-12 00:33:22.834: E/AndroidRuntime(26772): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)33:22.834: E/AndroidRuntime(26772): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097) 10-12 00:33:22.834: E/AndroidRuntime(26772): 在 android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2211)33:22.834: E/AndroidRuntime(26772): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097) 10-12 00:33:22.834: E/AndroidRuntime(26772): 在 android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2211)