从editText到number的getText()不起作用



我试图从editText中获取数字,但一直收到一个错误。我该怎么办?

我在这里搜索了一下,发现我需要通过以下方式来完成:getText().toString();然后:Integer.parseInt(X);但不起作用

.xml中editText的inputType是"number">

这是我的相关代码:

package johnny.tip2;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
EditText percent, guys, meal;
TextView textView;
String guy, houm, perc;
Button how_much;
int p, g, hm, tip, tipR, oneTip, oneTip2, addTip;
boolean flag = false;
AlertDialog.Builder builder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
percent = (EditText) findViewById(R.id.percent);
guys = (EditText) findViewById(R.id.guys);
meal = (EditText) findViewById(R.id.meal);
how_much = (Button) findViewById(R.id.how_much);
textView = (TextView) findViewById(R.id.textView);

how_much.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
guy = guys.getText().toString();
g = Integer.valueOf(guy);
houm = how_much.getText().toString();
hm = Integer.valueOf(houm);
perc = percent.getText().toString();
p = Integer.valueOf(perc);
tip = hm * p / 100;
tipR = Math.round(tip) + 1;
oneTip = tipR / g;
oneTip2 = Math.round(oneTip) + 1;
if ((oneTip2 * g) < tip){
addTip = Math.round(tip - (oneTip2 * g)) + 1;
oneTip2 = oneTip2 + addTip;
}

if (percent.getText().length() == 1) {
if (flag = false)
openDialog();
flag = true;
}
/* else{
textView.setText("שומע? הטיפ יצא " + tip + "מה אתם אומרים? נעגל ל " + tipR + "זה אומר שכל אחד משלם " + oneTip2 + "וגם פינקתם את המלצר, איך אני?");
}*/
}
});
}
public void openDialog() {
builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("יא קמצן!")
.setMessage("נהוג לתת 10%, אל תהיה לוזר")
.setNegativeButton("עזובתי אני מרושש וקמצן", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.setPositiveButton("יודע מה, מפנק ב10%", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
percent.setText("10");
p = 10;
}
});
builder.show();
}
}

这是一个错误:

java.lang.RuntimeException:无法启动活动组件信息{johnny.tip2/johnny.tip 2.MainActivity}:java.lang.NumberFormatException:无效的int:"在android.app.ActivityThread.performLaunchActivity(ActivityThreads.java:2416)在android.app.ActivityThread.handleLaunchActivity(ActivityThreads.java:2476)在android.app.ActivityThread.-wrap11(ActivityThreads.java)在android.app.ActivityThread$H.handleMessage(ActivityThreads.java:1344)在android.os.Handler.dispatchMessage(Handler.java:102)在android.os.Looper.loop(Looper.java:148)在android.app.ActivityThread.main(ActivityThreads.java:5417)位于java.lang.reflect.Method.ioke(本机方法)网址:com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)网址:com.android.internal.os.ZygoteInit.main(ZygoteNit.java:616)由以下原因引起:java.lang.NumberFormatException:无效的int:">

在.xml文件上的edittext中使用此代码

android:inputType="number"

并在.java文件中使用此代码

String perc = "2158";
int p = Integer.valueOf(perc);

如果这个代码对你的代码有效,也许你从edittext收到的数字不是int。

请试试这个代码。

String perc = percent.getText().toString();
long p = Long.valueOf(perc);

Java中的">long"是数字变量

最新更新