gpt4 book ai didi

java.lang.NumberFormatException : unable to parse '' as integer one more time 异常

转载 作者:行者123 更新时间:2023-11-29 03:17:50 24 4
gpt4 key购买 nike

我将从用户那里获取两个数字,但是这个来自 EditText 的数字必须转换为 int。我认为它应该可以工作,但我在 Android Studio 中的编译代码仍然有问题。

CatLog 显示错误符合:

int wiek = Integer.parseInt(wiekEditText.getText().toString());

下面是我完整的 Android 代码:

public class MyActivity extends ActionBarActivity {

int Wynik;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);

int Tmax, RT;
EditText wiekEditText = (EditText) findViewById(R.id.inWiek);
EditText tspoczEditText = (EditText) findViewById(R.id.inTspocz);
int wiek = Integer.parseInt(wiekEditText.getText().toString());
int tspocz = Integer.parseInt(tspoczEditText.getText().toString());

Tmax = 220 - wiek;
RT = Tmax - tspocz;
Wynik = 70*RT/100 + tspocz;

final EditText tempWiekEdit = wiekEditText;
TabHost tabHost = (TabHost) findViewById(R.id.tabHost); //Do TabHost'a z layoutu

tabHost.setup();

TabHost.TabSpec tabSpec = tabHost.newTabSpec("Calc");
tabSpec.setContent(R.id.Calc);
tabSpec.setIndicator("Calc");
tabHost.addTab(tabSpec);

tabSpec = tabHost.newTabSpec("Hints");
tabSpec.setContent(R.id.Hints);
tabSpec.setIndicator("Hints");
tabHost.addTab(tabSpec);

final Button Btn = (Button) findViewById(R.id.Btn);
Btn.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),"blablabla"+ "Wynik",Toast.LENGTH_SHORT).show();
}
});

wiekEditText.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) {

Btn.setEnabled(!(tempWiekEdit.getText().toString().trim().isEmpty()));
}

@Override
public void afterTextChanged(Editable s) {

}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.my, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}

最佳答案

在不查看布局 XML 的情况下,通过阅读你想从用户那里获取 2 个数字的问题描述,我只能假设那 2 个 EditText 是空的 Activity 开始时。

@Override
protected void onCreate(Bundle savedInstanceState) {

...

EditText wiekEditText = (EditText) findViewById(R.id.inWiek);
EditText tspoczEditText = (EditText) findViewById(R.id.inTspocz);

// edit text are still empty...
int wiek = Integer.parseInt(wiekEditText.getText().toString()); // error occurs!
int tspocz = Integer.parseInt(tspoczEditText.getText().toString());

...

}

您可能希望在用户交互后解析数字,可能是将代码放入OnClickListenerTextWatcher

此示例使用 OnClickListener。计算将在用户点击按钮后完成。

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);

// add 'final'
final EditText wiekEditText = (EditText) findViewById(R.id.inWiek);
final EditText tspoczEditText = (EditText) findViewById(R.id.inTspocz);

// remove offending code

final EditText tempWiekEdit = wiekEditText;
TabHost tabHost = (TabHost) findViewById(R.id.tabHost);

...

final Button Btn = (Button) findViewById(R.id.Btn);
Btn.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// move the parsing code here
int Tmax, RT;
int wiek = Integer.parseInt(wiekEditText.getText().toString());
int tspocz = Integer.parseInt(tspoczEditText.getText().toString());

Tmax = 220 - wiek;
RT = Tmax - tspocz;
Wynik = 70*RT/100 + tspocz;

Toast.makeText(getApplicationContext(),"blablabla"+ Wynik,Toast.LENGTH_SHORT).show();
}
});

...

}

-或-

为防止错误,第一次通过在布局 XML 上添加 android:text="0"设置默认数字

<EditText android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="@+id/inTspocz"
android:layout_marginTop="25dp"
android:hint="Your resting heart rate (Hint)"
android:text="0" />

关于java.lang.NumberFormatException : unable to parse '' as integer one more time 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25478857/

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