您好,我是一名学习Java的大学生。我正在开发一个Android应用程序。它编译并显示没有错误。但是当它运行时,会出现意外的关闭错误。
这是我的申请。当您单击计算ws时,您打算更改为此屏幕。
这是我的代码:
package com.warhammerdicerrolleralpha;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
public class myMain extends Activity
{
EditText enternumberofdice;
final TextView textGenerateNumber = (TextView) findViewById(R.id.text4);
private EditText text, text2, text3;
private Button btutorial1;
int number1 = Integer.parseInt(text.getText().toString());
int number2 = Integer.parseInt(text2.getText().toString());
ImageView i = new ImageView(this);
{
i.setAdjustViewBounds(true);
}
private int myFaceValue;
int myNum;
/**
* Called when the activity is first created.
*
* @return
*/
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void go()
{
while (myNum > 0)
{
// TODO Auto-generated method stub
textGenerateNumber.setText(String.valueOf(enternumberofdice));
--myNum;
return;
}
}
public int roll()
{
int val = (int) (6 * Math.random() + 1); // Range 1-6
setValue(val);
return val;
}
{
try
{
myNum = Integer.parseInt(enternumberofdice.getText().toString());
}
catch (NumberFormatException nfe)
{
enternumberofdice.setText("Does not work");
}
}
public int getValue()
{
return myFaceValue;
}
public void setValue(int myFaceValue)
{
this.myFaceValue = myFaceValue;
}
{
switch (myFaceValue)
{
case 5:
i.setImageResource(R.drawable.dicefive);
break;
case 1:
i.setImageResource(R.drawable.diceone);
break;
case 3:
i.setImageResource(R.drawable.dicethree);
break;
case 2:
i.setImageResource(R.drawable.dicetwo);
break;
case 4:
i.setImageResource(R.drawable.dicefour);
break;
case 6:
i.setImageResource(R.drawable.dicesix);
break;
default:
i.setImageResource(R.drawable.error);
break;
}
text = (EditText) findViewById(R.id.editText1);
text2 =(EditText) findViewById(R.id.editText2);
text3 = (EditText) findViewById(R.id.editText3);
btutorial1 = (Button) findViewById(R.id.button1);
btutorial1.setOnClickListener((OnClickListener) this);
Button buttonGenerate = (Button) findViewById(R.id.button1);
enternumberofdice = (EditText) findViewById(R.id.enternumberofdice);
Button buttonGenerate2 = (Button) findViewById(R.id.battlecalculate);
buttonGenerate2.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
setContentView(R.layout.main2);
}
});
buttonGenerate.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
go();
roll();
}
});
}
public void onClick(View view)
{
switch (view.getId())
{
case R.id.button1:
if (number1 > number2)
{
text3.setText("Three and above");
return;
}
else if (number1 < number2)
{
text3.setText("Five and above");
return;
}
else if (number1 == number2)
{
text3.setText("Four and above");
return;
}
else
{
text3.setText("Not Working");
return;
}
}
}
}
答案 0 :(得分:0)
查看堆栈跟踪,但我认为可能是因为这个块:
private EditText text, text2, text3;
private Button btutorial1;
int number1 = Integer.parseInt(text.getText().toString());
int number2 = Integer.parseInt(text2.getText().toString());
看起来好像是在尝试对空值进行parseInt。您需要使用类似的代码指定表单中text,text2,text3的位置:
TextView textGenerateNumber = (TextView) findViewById(R.id.text4);
希望有所帮助。