gpt4 book ai didi

android - 在 android studio 中协助处理 MAIN 中的致命异常

转载 作者:行者123 更新时间:2023-12-02 05:03:35 24 4
gpt4 key购买 nike

我是 Android 编程新手,正在尝试运行该程序,但问题是应用程序构建并成功运行,但每当我按下 GO 按钮时就会崩溃。我的教授让我完成这个任务,作为 Android 编程的开始,因为他认为这将是一项具有挑战性的任务,会充满激励性和有趣。我到处寻找解决方案,但找不到。我的教授本人无法调试代码,因此。

代码如下:

**CODE 1**  

package com.example.thispc.myapplication;
import android.annotation.SuppressLint;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.EditText;

import java.util.Scanner;

import edu.princeton.cs.algs4.EdgeWeightedDigraph;
import edu.princeton.cs.algs4.DirectedEdge;
import edu.princeton.cs.algs4.BellmanFordSP;

public class Arbitrage extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {

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

Button Check_Arbitrage = findViewById(R.id.Check_Arbitrage);
Check_Arbitrage.setOnClickListener(new View.OnClickListener() {
@SuppressLint("SetTextI18n")
@Override
public void onClick(View view) {

EditText inputcurrencynumber;
EditText inputdata;
TextView outputarbitrage;
inputcurrencynumber = findViewById(R.id.GetNoCurrencies);
inputdata = findViewById(R.id.currency_table_input);
outputarbitrage = findViewById(R.id.result);

inputcurrencynumber = findViewById(R.id.GetNoCurrencies);
int V;
V = Integer.parseInt(inputcurrencynumber.getText().toString());
outputarbitrage = findViewById(R.id.result);



double rate,stake;

inputdata = findViewById(R.id.currency_table_input);

EdgeWeightedDigraph G = new EdgeWeightedDigraph(V);
outputarbitrage.setText("Hello World");

String [] name = new String[V];
// The error occures here when i try to take an input
for (int v = 0; v < V ; v++)
{`enter code here`
name[v] = inputdata.getText().toString();
for (int w = 0; w < V ; w++)
{
rate = Double.parseDouble(inputdata.getText().toString());
DirectedEdge e = new DirectedEdge(v, w, -Math.log(rate));
G.addEdge(e);

}
}

BellmanFordSP spt = new BellmanFordSP(G,0);


if (spt.hasNegativeCycle())
{
stake = 1000;
for (DirectedEdge e : spt.negativeCycle())
{
//outputarbitrage.setText((Double.toString(stake) + name[e.from()]) + " " + (Double.toString(stake) + name[e.to()]) + "\n");


outputarbitrage.setText("%10.5f %s " + stake + name[e.from()]);
stake = stake * Math.exp(-e.weight());
outputarbitrage.setText("= %10.5f %s\n" + stake + name[e.to()]);
}
}

else
{
outputarbitrage.setText("No Arbitrage Opportunity Found");
}
}
});

}
}

注意:此代码在 java IDE 中完美运行(我使用 netbeans),但在 android studio 中崩溃。

================================================== =================================================== =================================================== ===编辑1

添加了如下日志:

com.example.thispc.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.thispc.myapplication, PID: 4876
java.lang.NumberFormatException: For input string: "USD 1 1.6 4
CAD 2.3 1 1.3
CHF 4 8 1"
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2043)
at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
at java.lang.Double.parseDouble(Double.java:539)
at com.example.thispc.myapplication.Arbitrage$1.onClick(Arbitrage.java:57)
at android.view.View.performClick(View.java:6294)
at android.view.View$PerformClick.run(View.java:24770)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

================================================== =================================================== =================================================== ===编辑2

output

最佳答案

由于以下行而导致崩溃

rate = Double.parseDouble(inputdata.getText().toString());

您正在尝试解析字符串,并且 parseDoubel() 需要仅包含数字值的字符串值,但在您的情况下 inputdata.getText().toString() 包含数字以外的字符。

在传递给 parseDouble() 方法之前,尝试从 inputdata 中提取数字

str.replaceAll("[^0-9]", "");

关于android - 在 android studio 中协助处理 MAIN 中的致命异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47731750/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com