Java getText()toString()方法无法处理方法参数

Java getText()toString()方法无法处理方法参数,java,android,tostring,Java,Android,Tostring,我在android应用程序的EditText字段中引入输入(字符串,例如:A、B等),然后按下按钮,应用程序停止。我是android应用程序的新手。当我试图在空对象引用上调用方法“时,会出现空指针异常”,但我不明白为什么我的对象为空。。。 如果我把值写在变量中 weight = Main.callMethod("A", "B"); 它工作得很好,但是如果我让应用程序中的值 weight = Main.callMethod(source, destination); 应用程序停止 以下是我使用

我在android应用程序的EditText字段中引入输入(字符串,例如:A、B等),然后按下按钮,应用程序停止。我是android应用程序的新手。当我试图在空对象引用上调用方法“时,会出现空指针异常”,但我不明白为什么我的对象为空。。。 如果我把值写在变量中

weight = Main.callMethod("A", "B");
它工作得很好,但是如果我让应用程序中的值

weight = Main.callMethod(source, destination);
应用程序停止

以下是我使用的两个类:

public class MainActivity extends Activity {

private Button b;
public static EditText currentLoc, selectDestination;
TextView tv;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    currentLoc = (EditText)findViewById(R.id.editText);
    selectDestination = (EditText)findViewById(R.id.editText2);
    tv = (TextView) findViewById(R.id.textView);
    // this.tv.setMovementMethod(new ScrollingMovementMethod());

    currentLoc.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            //tv.setText(s);
            source = s.toString();
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });

    selectDestination.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            //tv.setText(s);
            destination = s.toString();
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });

    //source = currentLoc.getText().toString(); //getHint() instead og getText()
    //destination = selectDestination.getText().toString();

    final Button button = findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // Code here executes on main thread after user presses button

            int weight; //= 0;

            Log.d("Source", "Current loc:" + source + " END");
            Log.d("Destination", "Select destination:" + destination + " END");

            Log.d("Compare", "2 strings " + source.equals("A"));

            Log.d("Source length" ,"is: " + source.length());
            Log.d("Destination length", "is: " + destination.length());

            //weight = Main.callMethod("A", "B");
            weight = Main.callMethod(source, destination); //not working !!!
            tv.setText("" + weight);

            //tv.setText("Your Input: \n" + source + "\n" + destination + "\nEnd.");
        }
    });
}
}

另一个类的方法是:

public class Main {

public static int callMethod (String source, String destination) {

    List<Path> list = new ArrayList();
    list.add(new Path("A", "C", 4));
    list.add(new Path("A", "B", 6));
    list.add(new Path("B", "C", 2));
    list.add(new Path("B", "D", 2));
    list.add(new Path("C", "D", 1));
    list.add(new Path("C", "E", 2));
    list.add(new Path("E", "D", 1));
    list.add(new Path("E", "F", 3));
    list.add(new Path("D", "F", 7));

    DijkstraAlgorithm object = new DijkstraAlgorithm(list);
    list = object.compute(source, destination);

    int weight = 0;
    for (Path path : list) {

        weight = path.getWeight();
        //System.out.println(path.getSource() + " -> " + path.getDestination() + " (" + path.getWeight() + ")");
    }

    return weight;
}
}
Dijkstra算法-在这个类中,问题似乎是路径、对类: 编辑:

public class DijkstraAlgorithm {
private final List<Path> graph;
private List<Path> shortestPath;
private Map distance;

public DijkstraAlgorithm(List<Path> graph)
{
    this.graph = graph;
}

public List<Path> compute(String start, String end)
{
    List<Path> list = new ArrayList();
    list.addAll(graph);
    shortestPath = new ArrayList();
    distance = new HashMap();
    distance.put(start, new Pair(0, "\0"));
    compute(start, end, list);
    generateShortestPath(start, end);

    return shortestPath;
}

private void compute(String source, String destination, List<Path> graph)
{
    Path bestPath = null;

    for (Path path : graph)
    {
        if (path.getSource() == source)
        {
            int pathVal = path.getWeight() + ((Pair) distance.get(source)).getValue();

            if (!distance.containsKey(path.getDestination()))
            {
                distance.put(path.getDestination(), new Pair(pathVal, path.getSource()));
            }
            else
            {
                if (((Pair) distance.get(path.getDestination())).getValue() > pathVal)
                {
                    distance.remove(path.getDestination());
                    distance.put(path.getDestination(), new Pair(pathVal, path.getSource()));
                }
            }

            if (bestPath == null)
            {
                bestPath = path;
            }
            else
            {
                if (((Pair) distance.get(bestPath.getDestination())).getValue() > ((Pair) distance.get(path.getDestination())).getValue())
                {
                    bestPath = path;
                }
            }
        }
    }

    List<Path> remove = new ArrayList();

    for (Path path : graph)
    {
        if (path.getSource() == source || path.getDestination() == source)
        {
            remove.add(path);
        }
    }

    graph.removeAll(remove);

    if (bestPath != null)
    {
        if (bestPath.getDestination() != destination)
        {
            boolean check = false;

            for (Path path : graph)
            {
                if (path.getSource() == bestPath.getDestination())
                {
                    check = true;
                }
            }

            if (check)
            {
                compute(bestPath.getDestination(), destination, graph);
            }
            else if (!graph.isEmpty())
            {
                compute(graph.get(0).getSource(), destination, graph);
            }
        }
    }
}

private void generateShortestPath(String start, String end)
{
    Pair pair = (Pair) distance.get(end);

    Log.d("Start", "is " + start); //this is now visible
    Log.d("End", "is " + end); //this is also visible
    Log.d("Msg1", "is " + "source " + pair.getSource() +   //at this line is the error !!!
                                    " value " + pair.getValue() +"\n");

    shortestPath.add(new Path(pair.getSource(), end, pair.getValue())); 

    while (pair.getSource() != start)
    {
        String c = pair.getSource();
        pair = (Pair) distance.get(pair.getSource());
        shortestPath.add(new Path(pair.getSource(), c, pair.getValue()));
    }

    Collections.reverse(shortestPath);
}
}



public class Pair {
private int value;
private String source;

public Pair(int value, String source)
{
    this.value = value;
    this.source = source;
}

public int getValue()
{
    return value;
}

public String getSource()
{
    return source;
}

public void setValue(int value)
{
    this.value = value;
}

public void setSource(String source)
{
    this.source = source;
}
}


public class Path {
private String source;
private String destination;
private int weight;

public Path(String source, String destination, int weight)
{
    this.source = source;
    this.destination = destination;
    this.weight = weight;
}

public String getSource()
{
    return source;
}

public String getDestination()
{
    return destination;
}

public int getWeight()
{
    return weight;
}

public void setSource(String source)
{
    this.source = source;
}

public void setDestination(String destination)
{
    this.destination = destination;
}

public void setWeight(int weight)
{
    this.weight = weight;
}
}
公共类dijkstra算法{
私有最终列表图;
私有列表最短路径;
私有地图距离;
公共Dijkstra算法(列表图)
{
this.graph=图形;
}
公共列表计算(字符串开始、字符串结束)
{
列表=新的ArrayList();
list.addAll(图);
shortestPath=newarraylist();
距离=新HashMap();
距离。放置(开始,新对(0,“\0”);
计算(开始、结束、列表);
GenerateShortStPath(开始、结束);
返回最短路径;
}
私有void计算(字符串源、字符串目标、列表图)
{
路径bestPath=null;
用于(路径:图形)
{
if(path.getSource()==source)
{
int pathVal=path.getWeight()+((对)distance.get(source)).getValue();
如果(!distance.containsKey(path.getDestination()))
{
distance.put(path.getDestination(),新对(pathVal,path.getSource());
}
其他的
{
if(((对)distance.get(path.getDestination()).getValue()>pathVal)
{
remove(path.getDestination());
distance.put(path.getDestination(),新对(pathVal,path.getSource());
}
}
if(最佳路径==null)
{
最佳路径=路径;
}
其他的
{
if(((对)distance.get(bestPath.getDestination()).getValue()>((对)distance.get(path.getDestination()).getValue())
{
最佳路径=路径;
}
}
}
}
List remove=new ArrayList();
用于(路径:图形)
{
if(path.getSource()==source | | path.getDestination()==source)
{
删除。添加(路径);
}
}
图.removeAll(移除);
if(最佳路径!=null)
{
if(bestPath.getDestination()!=目的地)
{
布尔检查=假;
用于(路径:图形)
{
if(path.getSource()==bestPath.getDestination())
{
检查=正确;
}
}
如果(检查)
{
计算(bestPath.getDestination(),destination,graph);
}
else如果(!graph.isEmpty())
{
计算(graph.get(0.getSource(),目标,graph);
}
}
}
}
私有void generateShortestPath(字符串开始、字符串结束)
{
配对=(配对)距离。获取(结束);
Log.d(“Start”、“is”+Start);//这现在是可见的
Log.d(“End”,“is”+End);//这也是可见的
Log.d(“Msg1”,“is”+“source”+pair.getSource()++//这一行是错误!!!
“value”+pair.getValue()+“\n”);
添加(新路径(pair.getSource(),end,pair.getValue());
while(pair.getSource()!=start)
{
字符串c=pair.getSource();
pair=(pair)distance.get(pair.getSource());
添加(新路径(pair.getSource(),c,pair.getValue());
}
集合。反向(最短路径);
}
}
公共类对{
私有int值;
私有字符串源;
公共对(int值,字符串源)
{
这个值=值;
this.source=源;
}
public int getValue()
{
返回值;
}
公共字符串getSource()
{
返回源;
}
公共无效设置值(int值)
{
这个值=值;
}
公共void集合源(字符串源)
{
this.source=源;
}
}
公共类路径{
私有字符串源;
专用字符串目的地;
私有整数权重;
公共路径(字符串源、字符串目标、整数权重)
{
this.source=源;
this.destination=目的地;
重量=重量;
}
公共字符串getSource()
{
返回源;
}
公共字符串getDestination()
{
返回目的地;
}
公共整数getWeight()
{
返回重量;
}
公共void集合源(字符串源)
{
this.source=源;
}
public void setDestination(字符串目的地)
{
this.destination=目的地;
}
公共空隙设定重量(内部重量)
{
重量=重量;
}
}

确保
编辑文本
当前位置
选择目的地
不是
最终
。并在
按钮之外初始化它们。setOnClickListener()
这意味着,在statent之后初始化它们

final Button button = findViewById(R.id.button);
甚至更多

并始终检查
目的
长度
在语句之前是否明显大于
0

weight = Main.callMethod(source, destination);

source
destination
有哪些值?请分享您的堆栈跟踪日志猫的错误。提供您的日志猫字符串@luk2302感谢您的帮助!这就是问题所在!源和目标的长度始终为0。希望我能找出原因。您是否声明currentLoc并选择Destination作为最终目的地?如果它们是final,则不会为它们设置新值。不,不,它们不是final(仅为public static),并且我已经按照您所说的那样移动了初始化。是否会出现.xml文件的问题?我解决了长度0的问题。问题是代码中的stll
weight = Main.callMethod(source, destination);