从可编辑(EditText)到整数的问题
我在从编辑文本框中获取简单数学程序的整数值时遇到问题。我可以从中获取一个字符串,但是当我尝试将其转换为整数时,它崩溃了。我已经尝试过:
int a = Intger.parseInt(X2.getText().toString());
但它仍然崩溃。任何想法将不胜感激。 这是源代码。
package siu.edu.cs215;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
public class Quadratic extends Activity {
private TextView answer;
private EditText X2, X, K;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.quad);
Button1 = (Button)findViewById(R.id.quad);
Button2 = (Button)findViewById(R.id.backwards);
answer = (TextView)findViewById(R.id.B1);
X2 = (EditText)findViewById(R.id.xsquared);
X = (EditText)findViewById(R.id.xcoeff);
K = (EditText)findViewById(R.id.kconst);
}
public Button Button1 = null;
public Button Button2 = null;
public void QA (View view){
//double sol1=0.0, sol2=0.0, disc=0.0;
int a = Integer.parseInt(X2.getText().toString());
//int b = Integer.getInteger(X.getText().toString());
//int c = Integer.getInteger(K.getText().toString());
//disc = Math.pow(b, 2) - 4.0*a*c;
//sol1 = (-b + Math.sqrt(disc)) / (2.0 * a);
//sol2 = (-b - Math.sqrt(disc)) / (2.0 * a);
answer.setText(a);
};
public void BACK (View view){
Intent i = new Intent(this, ExtraCreditActivity.class);
startActivity(i);
}
}
这是 xml 文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TableLayout
android:id="@+id/tableLayout1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
<TableRow
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight=".6" >
</TableRow>
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1" >
<EditText
android:id="@+id/xsquared"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_weight="1"
android:inputType="number" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="0dp"
android:layout_weight="1"
android:gravity="center"
android:text="X^2 +"
android:textSize="20dp" />
<EditText
android:id="@+id/xcoeff"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="number" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_weight="1"
android:gravity="center"
android:text="X + "
android:textSize="20dp" />
<EditText
android:id="@+id/kconst"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:inputType="number" >
<requestFocus />
</EditText>
</TableRow>
<TableRow
android:id="@+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1.2" >
<Button
android:id="@+id/quad"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="QA"
android:text="Quadratic" />
</TableRow>
<TableRow
android:id="@+id/tableRow4"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight=".8" >
<TextView
android:id="@+id/B1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="2pt"
android:text="@string/app_name" />
</TableRow>
<TableRow
android:id="@+id/tableRow5"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="@+id/backwards"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Back"
android:onClick="BACK" />
</TableRow>
</TableLayout>
</LinearLayout>
I am having problems getting the integer values for a simple math program out of the edittext boxes. I can get a string out of it but when I try to convert it to a integer it crashes. I have tried:
int a = Intger.parseInt(X2.getText().toString());
and it still crashes. Any idea's would be greatly appreciated.
Here is source code.
package siu.edu.cs215;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
public class Quadratic extends Activity {
private TextView answer;
private EditText X2, X, K;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.quad);
Button1 = (Button)findViewById(R.id.quad);
Button2 = (Button)findViewById(R.id.backwards);
answer = (TextView)findViewById(R.id.B1);
X2 = (EditText)findViewById(R.id.xsquared);
X = (EditText)findViewById(R.id.xcoeff);
K = (EditText)findViewById(R.id.kconst);
}
public Button Button1 = null;
public Button Button2 = null;
public void QA (View view){
//double sol1=0.0, sol2=0.0, disc=0.0;
int a = Integer.parseInt(X2.getText().toString());
//int b = Integer.getInteger(X.getText().toString());
//int c = Integer.getInteger(K.getText().toString());
//disc = Math.pow(b, 2) - 4.0*a*c;
//sol1 = (-b + Math.sqrt(disc)) / (2.0 * a);
//sol2 = (-b - Math.sqrt(disc)) / (2.0 * a);
answer.setText(a);
};
public void BACK (View view){
Intent i = new Intent(this, ExtraCreditActivity.class);
startActivity(i);
}
}
Here is the xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TableLayout
android:id="@+id/tableLayout1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
<TableRow
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight=".6" >
</TableRow>
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1" >
<EditText
android:id="@+id/xsquared"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_weight="1"
android:inputType="number" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="0dp"
android:layout_weight="1"
android:gravity="center"
android:text="X^2 +"
android:textSize="20dp" />
<EditText
android:id="@+id/xcoeff"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="number" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_weight="1"
android:gravity="center"
android:text="X + "
android:textSize="20dp" />
<EditText
android:id="@+id/kconst"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:inputType="number" >
<requestFocus />
</EditText>
</TableRow>
<TableRow
android:id="@+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1.2" >
<Button
android:id="@+id/quad"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="QA"
android:text="Quadratic" />
</TableRow>
<TableRow
android:id="@+id/tableRow4"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight=".8" >
<TextView
android:id="@+id/B1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="2pt"
android:text="@string/app_name" />
</TableRow>
<TableRow
android:id="@+id/tableRow5"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="@+id/backwards"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Back"
android:onClick="BACK" />
</TableRow>
</TableLayout>
</LinearLayout>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的问题很可能是尝试解析空字符串。也许您可以简单地避免执行任何计算,除非可以正确解析所有字段。将您的解析放入 try/catch 块中。
Most likely you're problem is in trying to parse an empty string. Perhaps you can simply avoid performing any calculations unless all fields can be correctly parsed. Put your parsing into a try/catch block.