我正在尝试从editText中获取数字,并且不断出现错误。我该怎么办?
我在这里搜索,发现需要通过以下方式进行: getText()。toString(); 接着: Integer.parseInt(X); 但这不起作用
.xml中editText的inputType是“数字”
这是我的相关代码:
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:无法启动活动 ComponentInfo {johnny.tip2 / johnny.tip2.MainActivity}: java.lang.NumberFormatException:无效的int:“” 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 在android.app.ActivityThread.-wrap11(ActivityThread.java) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1344) 在android.os.Handler.dispatchMessage(Handler.java:102) 在android.os.Looper.loop(Looper.java:148) 在android.app.ActivityThread.main(ActivityThread.java:5417) 在java.lang.reflect.Method.invoke(本机方法) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 引起原因:java.lang.NumberFormatException:无效的int:“”
答案 0 :(得分:0)
在.xml文件的edittext中使用此代码
android:inputType="number"
并在.java文件中使用此代码
String perc = "2158";
int p = Integer.valueOf(perc);
如果此代码对您的代码有效,则可能是您从edittext收到的数字不是整数。
请尝试以下代码。
String perc = percent.getText().toString();
long p = Long.valueOf(perc);
Java中的“ long ”是数字变量