我的简单Android应用程序在手机上构建和安装,但在我尝试执行任何操作时崩溃

时间:2017-10-31 00:02:50

标签: java android

我正在编写一个具有以下操作方法的应用程序:一个人将一个字符串输入一个文本框,另一个字符串输入另一个文本框(这两个字符串传递给方法 GetDirections < / em>),按下&#34; GO&#34;按钮,应用程序显示 GetDirections 提供的字符串列表。我相信,你真的不需要考虑第一种方法以外的任何东西; GetDirections只返回一个ArrayList。

这是我的代码:

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import java.util.List;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.HashMap;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import java.util.List;
import org.jgrapht.*;
import org.jgrapht.graph.*;
import org.jgrapht.alg.DijkstraShortestPath;


public class MainActivity extends AppCompatActivity {

ArrayList<String> DirectionsList = new ArrayList<String>();

@Override

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);





    EditText text1 = (EditText)findViewById(R.id.Start);
    final String startLoc = text1.getText().toString();
    EditText text2 = (EditText)findViewById(R.id.End);
    final String endLoc = text2.getText().toString();
    final Button but1 = (Button)findViewById(R.id.button);
    but1.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
        DirectionsList = getDirections(startLoc, endLoc);
            ListView dir1 = (ListView)findViewById(R.id.Directions);
            ArrayAdapter listAdapter = new ArrayAdapter<String>
(MainActivity.this, android.R.layout.simple_list_item_1, DirectionsList);
            dir1.setAdapter(listAdapter);
        }


    });


    }
public ArrayList getDirections(String string, String string2)
{
    final DirectedWeightedMultigraph<String, DefaultWeightedEdge> map = new 
DirectedWeightedMultigraph<String, DefaultWeightedEdge>
(DefaultWeightedEdge.class);







    String vertexList = "A1S1 A110 A111 A130 A1S2 A114 A113 A1H1 A1COPIER A116 A117A A117B A103 A102 A101 A1H2 A120 A121 A122 ENGLISH SOCSTUD A1S3 A1S4 A1HA";
    StringTokenizer TSS = new StringTokenizer(vertexList);
    while(TSS.hasMoreTokens()) { map.addVertex(TSS.nextToken()); }
    DefaultWeightedEdge A1S1toA1H1 = map.addEdge("A1S1", "A1H1");
    DefaultWeightedEdge A1H1toA1S1 = map.addEdge("A1H1", "A1S1");
    map.setEdgeWeight(A1S1toA1H1, 1);
    map.setEdgeWeight(A1H1toA1S1, 1);
    DefaultWeightedEdge A1S1toA1H2 = map.addEdge("A1S1", "A1H2");
    DefaultWeightedEdge A1H2toA1S1 = map.addEdge("A1H2", "A1S1");
    map.setEdgeWeight(A1S1toA1H2, 1);
    map.setEdgeWeight(A1H2toA1S1, 1);
    DefaultWeightedEdge A1S1toCOPIER = map.addEdge("A1S1", "A1COPIER");
    DefaultWeightedEdge COPIERtoA1S1 = map.addEdge("A1COPIER", "A1S1");
    map.setEdgeWeight(A1S1toCOPIER, 10);
    map.setEdgeWeight(COPIERtoA1S1, 10);
    DefaultWeightedEdge A1H1toA110 = map.addEdge("A1H1", "A110");
    DefaultWeightedEdge A110toA1H1 = map.addEdge("A110", "A1H1");
    map.setEdgeWeight(A1H1toA110, 10);
    map.setEdgeWeight(A110toA1H1, 10);
    DefaultWeightedEdge A1H1toA111 = map.addEdge("A1H1", "A111");
    DefaultWeightedEdge A111toA1H1 = map.addEdge("A111", "A1H1");
    map.setEdgeWeight(A1H1toA111, 10);
    map.setEdgeWeight(A111toA1H1, 10);
    DefaultWeightedEdge A1H1toA130 = map.addEdge("A1H1", "A130");
    DefaultWeightedEdge A130toA1H1 = map.addEdge("A130", "A1H1");
    map.setEdgeWeight(A1H1toA130, 10);
    map.setEdgeWeight(A130toA1H1, 10);
    DefaultWeightedEdge A1H1toA114 = map.addEdge("A1H1", "A114");
    DefaultWeightedEdge A114toA1H1 = map.addEdge("A114", "A1H1");
    map.setEdgeWeight(A1H1toA114, 10);
    map.setEdgeWeight(A114toA1H1, 10);
    DefaultWeightedEdge A1H1toA113 = map.addEdge("A1H1", "A113");
    DefaultWeightedEdge A113toA1H1 = map.addEdge("A113", "A1H1");
    map.setEdgeWeight(A1H1toA113, 10);
    map.setEdgeWeight(A113toA1H1, 10);
    DefaultWeightedEdge A1H1toA1S2 = map.addEdge("A1H1", "A1S2");
    DefaultWeightedEdge A1S2toA1H1 = map.addEdge("A1S2", "A1H1");
    map.setEdgeWeight(A1H1toA1S2, 1);
    map.setEdgeWeight(A1S2toA1H1, 1);
    DefaultWeightedEdge A1S2toA110 = map.addEdge("A1S2", "A110");
    DefaultWeightedEdge A110toA1S2 = map.addEdge("A110", "A1S2");
    map.setEdgeWeight(A1S2toA110, 10);
    map.setEdgeWeight(A110toA1S2, 10);
    DefaultWeightedEdge A1S2toA111 = map.addEdge("A1S2", "A111");
    DefaultWeightedEdge A111toA1S2 = map.addEdge("A111", "A1S2");
    map.setEdgeWeight(A1S2toA111, 10);
    map.setEdgeWeight(A111toA1S2, 10);
    DefaultWeightedEdge A1S2toA130 = map.addEdge("A1S2", "A130");
    DefaultWeightedEdge A130toA1S2 = map.addEdge("A130", "A1S2");
    map.setEdgeWeight(A1S2toA130, 10);
    map.setEdgeWeight(A130toA1S2, 10);
    DefaultWeightedEdge A1S2toA114 = map.addEdge("A1S2", "A114");
    DefaultWeightedEdge A114toA1S2 = map.addEdge("A114", "A1S2");
    map.setEdgeWeight(A1S2toA114, 10);
    map.setEdgeWeight(A114toA1S2, 10);
    DefaultWeightedEdge A1S2toA113 = map.addEdge("A1S2", "A113");
    DefaultWeightedEdge A113toA1S2 = map.addEdge("A113", "A1S2");
    map.setEdgeWeight(A1S2toA113, 10);
    map.setEdgeWeight(A113toA1S2, 10);
    DefaultWeightedEdge A1H1toA1HA = map.addEdge("A1H1", "A1HA");
    DefaultWeightedEdge A1HAtoA1H1 = map.addEdge("A1HA", "A1H1");
    map.setEdgeWeight(A1H1toA1HA, 10);
    map.setEdgeWeight(A1HAtoA1H1, 10);
    DefaultWeightedEdge A1H2toA1HA = map.addEdge("A1H2", "A1HA");
    DefaultWeightedEdge A1HAtoA1H2 = map.addEdge("A1HA", "A1H2");
    map.setEdgeWeight(A1H2toA1HA, 10);
    map.setEdgeWeight(A1HAtoA1H2, 10);
    DefaultWeightedEdge A1HAtoA116 = map.addEdge("A1HA", "A116");
    DefaultWeightedEdge A116toA1HA = map.addEdge("A116", "A1HA");
    map.setEdgeWeight(A1HAtoA116, 1);
    map.setEdgeWeight(A116toA1HA, 1);
    DefaultWeightedEdge A1HAtoA177A = map.addEdge("A1HA", "A117A");
    DefaultWeightedEdge A177AtoA1HA = map.addEdge("A117A", "A1HA");
    map.setEdgeWeight(A1HAtoA177A, 1);
    map.setEdgeWeight(A177AtoA1HA, 1);
    DefaultWeightedEdge A117AtoA117B = map.addEdge("A117A", "A117B");
    DefaultWeightedEdge A117BtoA117A = map.addEdge("A117B", "A117A");
    map.setEdgeWeight(A117AtoA117B, 1);
    map.setEdgeWeight(A117BtoA117A, 1);
    DefaultWeightedEdge A1H2toA103 = map.addEdge("A1H2", "A103");
    DefaultWeightedEdge A103toA1H2 = map.addEdge("A103", "A1H2");
    map.setEdgeWeight(A1H2toA103, 10);
    map.setEdgeWeight(A103toA1H2, 10);
    DefaultWeightedEdge A1H2toA102 = map.addEdge("A1H2", "A102");
    DefaultWeightedEdge A102toA1H2 = map.addEdge("A102", "A1H2");
    map.setEdgeWeight(A1H2toA102, 10);
    map.setEdgeWeight(A102toA1H2, 10);
    DefaultWeightedEdge A1H2toA101 = map.addEdge("A1H2", "A101");
    DefaultWeightedEdge A101toA1H2 = map.addEdge("A101", "A1H2");
    map.setEdgeWeight(A1H2toA101, 10);
    map.setEdgeWeight(A101toA1H2, 10);
    DefaultWeightedEdge A1S3toA103 = map.addEdge("A1S3", "A103");
    DefaultWeightedEdge A103toA1S3 = map.addEdge("A103", "A1S3");
    map.setEdgeWeight(A1S3toA103, 10);
    map.setEdgeWeight(A103toA1S3, 10);
    DefaultWeightedEdge A1S3toA102 = map.addEdge("A1S3", "A102");
    DefaultWeightedEdge A102toA1S3 = map.addEdge("A102", "A1S3");
    map.setEdgeWeight(A1S3toA102, 10);
    map.setEdgeWeight(A102toA1S3, 10);
    DefaultWeightedEdge A1S3toA101 = map.addEdge("A1S3", "A101");
    DefaultWeightedEdge A101toA1S3 = map.addEdge("A101", "A1S3");
    map.setEdgeWeight(A1S3toA101, 10);
    map.setEdgeWeight(A101toA1S3, 10);
    DefaultWeightedEdge A1H2toA1S4 = map.addEdge("A1H2", "A1S4");
    DefaultWeightedEdge A1S4toA1H2 = map.addEdge("A1S4", "A1H2");
    map.setEdgeWeight(A1H2toA1S4, 1);
    map.setEdgeWeight(A1S4toA1H2, 1);
    DefaultWeightedEdge A1H2toA120 = map.addEdge("A1H2", "A120");
    DefaultWeightedEdge A120toA1H2 = map.addEdge("A120", "A1H2");
    map.setEdgeWeight(A1H2toA120, 10);
    map.setEdgeWeight(A120toA1H2, 10);
    DefaultWeightedEdge A1H2toA121 = map.addEdge("A1H2", "A121");
    DefaultWeightedEdge A121toA1H2 = map.addEdge("A121", "A1H2");
    map.setEdgeWeight(A1H2toA121, 10);
    map.setEdgeWeight(A121toA1H2, 10);
    DefaultWeightedEdge A1H2toA122 = map.addEdge("A1H2", "A122");
    DefaultWeightedEdge A122toA1H2 = map.addEdge("A122", "A1H2");
    map.setEdgeWeight(A1H2toA122, 10);
    map.setEdgeWeight(A122toA1H2, 10);
    DefaultWeightedEdge A1H2toENGLISH = map.addEdge("A1H2", "ENGLISH");
    DefaultWeightedEdge ENGLISHtoA1H2 = map.addEdge("ENGLISH", "A1H2");
    map.setEdgeWeight(A1H2toENGLISH, 10);
    map.setEdgeWeight(ENGLISHtoA1H2, 10);
    DefaultWeightedEdge A1H2toSOCSTUD = map.addEdge("A1H2", "SOCSTUD");
    DefaultWeightedEdge SOCSTUDtoA1H2 = map.addEdge("SOCSTUD", "A1H2");
    map.setEdgeWeight(A1H2toSOCSTUD, 10);
    map.setEdgeWeight(SOCSTUDtoA1H2, 10);
    DefaultWeightedEdge A1S4toA120 = map.addEdge("A1S4", "A120");
    DefaultWeightedEdge A120toA1S4 = map.addEdge("A120", "A1S4");
    map.setEdgeWeight(A1S4toA120, 10);
    map.setEdgeWeight(A120toA1S4, 10);
    DefaultWeightedEdge A1S4toA121 = map.addEdge("A1S4", "A121");
    DefaultWeightedEdge A121toA1S4 = map.addEdge("A121", "A1S4");
    map.setEdgeWeight(A1S4toA121, 10);
    map.setEdgeWeight(A121toA1S4, 10);
    DefaultWeightedEdge A1S4toA122 = map.addEdge("A1S4", "A122");
    DefaultWeightedEdge A122toA1S4 = map.addEdge("A122", "A1S4");
    map.setEdgeWeight(A1S4toA122, 10);
    map.setEdgeWeight(A122toA1S4, 10);
    DefaultWeightedEdge A1S4toENGLISH = map.addEdge("A1S4", "ENGLISH");
    DefaultWeightedEdge ENGLISHtoA1S4 = map.addEdge("ENGLISH", "A1S4");
    map.setEdgeWeight(A1S4toENGLISH, 10);
    map.setEdgeWeight(ENGLISHtoA1S4, 10);
    DefaultWeightedEdge A1S4toSOCSTUD = map.addEdge("A1S4", "SOCSTUD");
    DefaultWeightedEdge SOCSTUDtoA1S4 = map.addEdge("SOCSTUD", "A1S4");
    map.setEdgeWeight(A1S4toSOCSTUD, 10);
    map.setEdgeWeight(SOCSTUDtoA1S4, 10);
    DefaultWeightedEdge A1S3toCOPIER = map.addEdge("A1S3", "A1COPIER");
    DefaultWeightedEdge COPIERtoA1S3 = map.addEdge("A1COPIER", "A1S3");
    map.setEdgeWeight(A1S3toCOPIER, 10);
    map.setEdgeWeight(COPIERtoA1S3, 10);
    DefaultWeightedEdge A1LtoR = map.addEdge("A1H1", "A1H2");
    DefaultWeightedEdge A1RtoL = map.addEdge("A1H2", "A1H1");
    map.setEdgeWeight(A1LtoR, 1);
    map.setEdgeWeight(A1RtoL, 1);






    List shortest_path = (List) DijkstraShortestPath.findPathBetween(map, 
string, string2);
    DijkstraShortestPath<String, DefaultWeightedEdge>shortest =
            new DijkstraShortestPath<String, DefaultWeightedEdge>(map, 
string, string2);

    String robustString = clearSpecChars(shortest_path.toString());
    // System.out.println(robustString);
    StringTokenizer st = new StringTokenizer(robustString);


    ArrayList connectorList = new ArrayList();
    while (st.hasMoreTokens()) {
        connectorList.add(st.nextToken());
    }
    int i = 0;
    String Rstring2 = "";
    while(!connectorList.isEmpty())
    {
        Rstring2 = Rstring2.concat(connectorList.get(0).toString());
        i++;
        if(i == 2)
        {
            i = 0;
            Rstring2 = Rstring2.concat(" ");
        }
        connectorList.remove(0);
    }
    //  System.out.println(Rstring2);
    //String RString = clearDupes(connectorList).toString();
    //RString = clearSpecChars(RString);*/
    StringTokenizer tokenizeRstring2 = new StringTokenizer(Rstring2);
    connectorList.clear();
    while (tokenizeRstring2.hasMoreTokens()) {
        connectorList.add(tokenizeRstring2.nextToken());
    }
    HashMap instructions = new HashMap<String, String>();
    instructions.put("A110A1H1", "Go into the hallway.");
    instructions.put("A1H1A1H2", "Go down the hallway.");
    instructions.put("A1H2A102", "Turn left and go down the hall. A102 will 
be on your left.");
    ArrayList<String> DirectionsList = new ArrayList<String>();
    String dataAll;

    for(int j = 0; j < connectorList.size(); j++){
        DirectionsList.add((String) 
   instructions.get(connectorList.get(0).toString()));
        connectorList.remove(0);
    }
    String[] DirectionsArray = (String[]) DirectionsList.toArray();
    return DirectionsList;


}
public static String clearSpecChars (String robustString)
{
    robustString = robustString.replaceAll("[(]", "");
    robustString = robustString.replaceAll("[)]", "");
    robustString = robustString.replaceAll("[:]", "");
    robustString = robustString.replaceAll("[,]", "");
    robustString = robustString.substring(0, robustString.length()-1);
    robustString = robustString.substring(1, robustString.length());

    return robustString;
}

}

1 个答案:

答案 0 :(得分:1)

我认为问题很简单,您需要在按下按钮后读取文本并在初始化块中读取值。将代码移动到单击侦听器代码中。

final String startLoc = text1.getText().toString();
final String endLoc = text2.getText().toString();
DirectionsList = getDirections(startLoc, endLoc);
            ListView dir1 = (ListView)findViewById(R.id.Directions);
            ArrayAdapter listAdapter = new ArrayAdapter<String>
(MainActivity.this, android.R.layout.simple_list_item_1, DirectionsList);
            dir1.setAdapter(listAdapter);
        }