我正在做一个测试,以便在 Eclipse 中进行 Android 开发。当按下计算按钮时,我的应用程序崩溃了。问题似乎出在 AlertDialog 上,但我发现的所有示例都有相同的方式。
package mobile.ildu.AndroidTest;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class AndroidTestActivity extends Activity {
private EditText text1;
private EditText text2;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void myClickHandler(View view)
{
switch(view.getId())
{
case R.id.calculate:
if(text1.getText().length()==0)
{
callDialog("Error","You must enter a number in the first box");
return;
}
if(text2.getText().length()==0)
{
callDialog("Error","You must enter a number in the second box");
return;
}
float number1 = Float.parseFloat(text1.getText().toString());
float number2 = Float.parseFloat(text2.getText().toString());
float answer = number1*number2;
callDialog("Answer",number1+"*"+number2+" = "+answer);
break;
}
}
private void callDialog(String title, String message)
{
AlertDialog alertDialog= new AlertDialog.Builder(AndroidTestActivity.this).create();
alertDialog.setTitle(title);
alertDialog.setMessage(message);
alertDialog.setButton("Close", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
dialog.dismiss();
}
});
alertDialog.show();
}
}
错误日志:
11-22 10:41:45.092: E/AndroidRuntime(675): FATAL EXCEPTION: main
11-22 10:41:45.092: E/AndroidRuntime(675): java.lang.IllegalStateException: Could not execute method of the activity
11-22 10:41:45.092: E/AndroidRuntime(675): at android.view.View$1.onClick(View.java:3019)
11-22 10:41:45.092: E/AndroidRuntime(675): at android.view.View.performClick(View.java:3460)
11-22 10:41:45.092: E/AndroidRuntime(675): at android.view.View$PerformClick.run(View.java:13955)
11-22 10:41:45.092: E/AndroidRuntime(675): at android.os.Handler.handleCallback(Handler.java:605)
11-22 10:41:45.092: E/AndroidRuntime(675): at android.os.Handler.dispatchMessage(Handler.java:92)
11-22 10:41:45.092: E/AndroidRuntime(675): at android.os.Looper.loop(Looper.java:137)
11-22 10:41:45.092: E/AndroidRuntime(675): at android.app.ActivityThread.main(ActivityThread.java:4340)
11-22 10:41:45.092: E/AndroidRuntime(675): at java.lang.reflect.Method.invokeNative(Native Method)
11-22 10:41:45.092: E/AndroidRuntime(675): at java.lang.reflect.Method.invoke(Method.java:511)
11-22 10:41:45.092: E/AndroidRuntime(675): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
11-22 10:41:45.092: E/AndroidRuntime(675): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
11-22 10:41:45.092: E/AndroidRuntime(675): at dalvik.system.NativeStart.main(Native Method)
11-22 10:41:45.092: E/AndroidRuntime(675): Caused by: java.lang.reflect.InvocationTargetException
11-22 10:41:45.092: E/AndroidRuntime(675): at java.lang.reflect.Method.invokeNative(Native Method)
11-22 10:41:45.092: E/AndroidRuntime(675): at java.lang.reflect.Method.invoke(Method.java:511)
11-22 10:41:45.092: E/AndroidRuntime(675): at android.view.View$1.onClick(View.java:3014)
11-22 10:41:45.092: E/AndroidRuntime(675): ... 11 more
11-22 10:41:45.092: E/AndroidRuntime(675): Caused by: java.lang.NullPointerException
11-22 10:41:45.092: E/AndroidRuntime(675): at mobile.ildu.AndroidTest.AndroidTestActivity.myClickHandler(AndroidTestActivity.java:27)
11-22 10:41:45.092: E/AndroidRuntime(675): ... 14 more
感谢您的帮助!
最佳答案
你永远不会初始化你的 EditText 成员变量,所以当你这样做时:
text1.getText().length()
你得到一个空指针异常。在调用 setContentView
后将其添加到 onCreate 方法中:
text1 = (EditText)findViewById(R.id.idOfWidget);
text2 = (EditText)findViewById(R.id.idOfWidget2);
(当然,用正确的常量替换这 2 个 id)。
关于Android应用程序: NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8230072/