我是Android新手,我正在为Android 2.3.3做一个小游戏 但是当我在AVD上运行时,它会自动关闭
代码就像这样
package dam.moviles.adivinaminumero;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.*;
import android.widget.*;
import java.util.*;
public class MainActivity extends ActionBarActivity
{
int intentos;
Random dado=new Random();
int numAdivinar=dado.nextInt(100)+1;
Button boton=(Button) findViewById(R.id.button1);
EditText et=(EditText)findViewById(R.id.cajaNumero);
TextView tv=(TextView)findViewById(R.id.textView1);
int numero=Integer.parseInt(et.getText().toString());
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et.setOnKeyListener(new android.view.View.OnKeyListener()
{
public boolean onKey(View v, int keyCode, android.view.KeyEvent event)
{
if ((event.getAction() == android.view.KeyEvent.ACTION_DOWN) && (keyCode == android.view.KeyEvent.KEYCODE_ENTER))
{
if(numero<numAdivinar)
{
String format=getResources().getString(R.string.mensajeMayor);
String cadFinal=String.format(format, numero);
tv.setText(cadFinal);
}
else if(numero>numAdivinar)
{
String format=getResources().getString(R.string.mensajeMenor);
String cadFinal=String.format(format, numero);
tv.setText(cadFinal);
}
else
{
tv.setText(R.string.acierto);
}
return true;
}
else
{
return false;
}
}
});
}
}
并抛出NullPointerException
如果我把代码放在OnCreate中,就像这样
package dam.moviles.adivinaminumero;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.*;
import android.widget.*;
import java.util.*;
public class MainActivity extends ActionBarActivity
{
int intentos;
Random dado=new Random();
int numAdivinar=dado.nextInt(100)+1;
Button boton;
EditText et;
TextView tv;
int numero;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
boton=(Button) findViewById(R.id.button1);
et=(EditText)findViewById(R.id.cajaNumero);
tv=(TextView)findViewById(R.id.textView1);
numero=Integer.parseInt(et.getText().toString());
et.setOnKeyListener(new android.view.View.OnKeyListener()
{
public boolean onKey(View v, int keyCode, android.view.KeyEvent event)
{
if ((event.getAction() == android.view.KeyEvent.ACTION_DOWN) && (keyCode == android.view.KeyEvent.KEYCODE_ENTER))
{
if(numero<numAdivinar)
{
String format=getResources().getString(R.string.mensajeMayor);
String cadFinal=String.format(format, numero);
tv.setText(cadFinal);
}
else if(numero>numAdivinar)
{
String format=getResources().getString(R.string.mensajeMenor);
String cadFinal=String.format(format, numero);
tv.setText(cadFinal);
}
else
{
tv.setText(R.string.acierto);
}
return true;
}
else
{
return false;
}
}
});
}
使用此LogCat
抛出NumberFormatException11-18 17:00:22.621: D/AndroidRuntime(595): Shutting down VM
11-18 17:00:22.621: W/dalvikvm(595): threadid=1: thread exiting with uncaught exception (group=0x40015560)
11-18 17:00:22.641: E/AndroidRuntime(595): FATAL EXCEPTION: main
11-18 17:00:22.641: E/AndroidRuntime(595): java.lang.RuntimeException: Unable to start activity ComponentInfo{dam.moviles.adivinaminumero/dam.moviles.adivinaminumero.MainActivity}: java.lang.NumberFormatException: unable to parse '' as integer
11-18 17:00:22.641: E/AndroidRuntime(595): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
11-18 17:00:22.641: E/AndroidRuntime(595): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
11-18 17:00:22.641: E/AndroidRuntime(595): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
11-18 17:00:22.641: E/AndroidRuntime(595): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
11-18 17:00:22.641: E/AndroidRuntime(595): at android.os.Handler.dispatchMessage(Handler.java:99)
11-18 17:00:22.641: E/AndroidRuntime(595): at android.os.Looper.loop(Looper.java:123)
11-18 17:00:22.641: E/AndroidRuntime(595): at android.app.ActivityThread.main(ActivityThread.java:3683)
11-18 17:00:22.641: E/AndroidRuntime(595): at java.lang.reflect.Method.invokeNative(Native Method)
11-18 17:00:22.641: E/AndroidRuntime(595): at java.lang.reflect.Method.invoke(Method.java:507)
11-18 17:00:22.641: E/AndroidRuntime(595): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
11-18 17:00:22.641: E/AndroidRuntime(595): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
11-18 17:00:22.641: E/AndroidRuntime(595): at dalvik.system.NativeStart.main(Native Method)
11-18 17:00:22.641: E/AndroidRuntime(595): Caused by: java.lang.NumberFormatException: unable to parse '' as integer
11-18 17:00:22.641: E/AndroidRuntime(595): at java.lang.Integer.parseInt(Integer.java:362)
11-18 17:00:22.641: E/AndroidRuntime(595): at java.lang.Integer.parseInt(Integer.java:332)
11-18 17:00:22.641: E/AndroidRuntime(595): at dam.moviles.adivinaminumero.MainActivity.onCreate(MainActivity.java:28)
11-18 17:00:22.641: E/AndroidRuntime(595): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-18 17:00:22.641: E/AndroidRuntime(595): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
11-18 17:00:22.641: E/AndroidRuntime(595): ... 11 more
11-18 17:00:25.701: I/Process(595): Sending signal. PID: 595 SIG: 9
非常感谢
答案 0 :(得分:0)
问题在于:
numero=Integer.parseInt(et.getText().toString());
您的EditText
已空,您无法执行此操作。
答案 1 :(得分:0)
第二种方式(抛出NumberFormatException
的方式是我在这个答案中提到的方式,因为这是更正确的方法。)
将et
声明为final
并将numero=Integer.parseInt(et.getText().toString());
移至您的onKey
方法中。您收到错误的原因是,您的应用首次创建时EditText
为空,并且由于您还在首次启动应用时尝试分配numero
,因此无需解析。
将numero=Integer.parseInt(et.getText().toString());
移至onKey
内,您告诉它只在输入内容时解析。
如果用户在EditText
中输入了无法从中解析出数字的内容,这仍然会给您一个错误,因此您可以指定只有数字可以输入EditText
,或者您可以使用try/catch
块包围解析方法。
在您的XML中,使用android:inputType="number"
强制只能输入数字。
答案 2 :(得分:0)
您的edittext et
为null,因为您在活动生命周期方法之外设置了findViewById
,并且您尝试将null值转换为int.So change
Button boton = (Button) findViewById(R.id.button1);
EditText et = (EditText) findViewById(R.id.cajaNumero);
TextView tv = (TextView) findViewById(R.id.textView1);
int numero = Integer.parseInt(et.getText().toString());
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
到
Button boton;
EditText et;
TextView tv;
int numero = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
boton = (Button) findViewById(R.id.button1);
et = (EditText) findViewById(R.id.cajaNumero);
tv = (TextView) findViewById(R.id.textView1);
if (et.getText().toString().length() != 0) {
numero = Integer.parseInt(et.getText().toString());
}