我尝试创建Android应用程序,并认为桌面游戏的骰子工具将是一个不错的项目。但是我继续在第40行得到无效的int错误,而且我对我实际做错的事情感到非常难过。
如果有人能指出我正确的方向,我真的很感激它:)
package com.example.isaac.dicetool;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.ToggleButton;
import java.util.Random;
public class MainActivity extends ActionBarActivity {
EditText et1,et2;
TextView Result;
Button button;
ToggleButton toggle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et1=(EditText) findViewById(R.id.editText);
et2=(EditText) findViewById(R.id.editText2);
Result=(TextView) findViewById(R.id.textView3);
button=(Button) findViewById(R.id.button1);
toggle=(ToggleButton) findViewById(R.id.toggleButton);
button.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View arg0){
int min = 1;
int max = 6;
int rolls = Integer.parseInt(et1.getText().toString());
int win = Integer.parseInt(et2.getText().toString());
Random r = new Random();
int sucess = 0;
int explode = 0;
boolean exp =((ToggleButton)arg0).isChecked();
for(int i=0;i<rolls;i++){
int roll = r.nextInt(max-min+1)+min;
if(roll<win){
sucess++;
}
if(exp==true){
if(roll==6){
rolls++;
explode++;
}
}
}
Result.setText("Number of successes: " + sucess + "n/Number of explosions: " + explode);
}
}
);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, 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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Number of Dice(D6)"
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="@+id/editText"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Number for Success"
android:id="@+id/textView2"
android:layout_below="@+id/editText"
android:layout_centerHorizontal="true" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="@+id/editText2"
android:layout_below="@+id/textView2"
android:layout_centerHorizontal="true"
android:numeric="integer" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ROLL!"
android:id="@+id/button1"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/textView3"
android:layout_alignRight="@+id/textView2"
android:layout_alignEnd="@+id/textView2"
android:layout_alignLeft="@+id/textView"
android:layout_alignStart="@+id/textView"
android:layout_alignParentBottom="true"
android:layout_below="@+id/button1" />
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New ToggleButton"
android:id="@+id/toggleButton"
android:layout_below="@+id/editText2"
android:layout_centerHorizontal="true"
android:textOff="Normal"
android:textOn="Exploding" />
05-20 13:04:15.830 1866-1866/com.example.isaac.dicetool E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.isaac.dicetool, PID: 1866
java.lang.NumberFormatException: Invalid int: ""
at java.lang.Integer.invalidInt(Integer.java:137)
at java.lang.Integer.parseInt(Integer.java:358)
at java.lang.Integer.parseInt(Integer.java:331)
at com.example.isaac.dicetool.MainActivity$1.onClick(MainActivity.java:40)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
问题在于您尝试将空字符串“”值转换为integer
!
int rolls = Integer.parseInt(et1.getText().toString());
如果您的EditText
edt1
没有价值,那么您将获得NumberFormatException
。
与下一行代码相同:
int win = Integer.parseInt(et2.getText().toString());
您可以通过以下方式验证:
et1Val = et1.getText().toString();
if (et1Val.matches("")) {
et1Val = "0";
}
int rolls = Integer.parseInt(et1Val);