首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >安卓应用: NullPointerException

安卓应用: NullPointerException
EN

Stack Overflow用户
提问于 2011-11-23 00:22:22
回答 6查看 927关注 0票数 2

我正在做一个测试,以便在Eclipse中进行Android开发。当按下calculate按钮时,我的应用程序崩溃。看起来问题出在AlertDialog上,但我找到的所有例子都是一样的。

代码语言:javascript
复制
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();
    }
}

错误日志:

代码语言:javascript
复制
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

谢谢你的帮助!

EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2011-11-23 00:36:53

试试这个:

代码语言:javascript
复制
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.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class AndroidTestActivity extends Activity {
    private EditText text1;
    private EditText text2;
    private Button button;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        text1 = (EditText)findViewById(R.id.text1);
        text2 = (EditText)findViewById(R.id.text2);
        button = (Button) findViewById(R.id.calculate);

        button.setOnClickListener(new  OnClickListener() {

            public void onClick(View arg0) {

                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);
            }
        });
    }

    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();
    }
}
票数 1
EN

Stack Overflow用户

发布于 2011-11-23 00:25:41

您从不初始化EditText成员变量,因此在执行此操作时:

代码语言:javascript
复制
text1.getText().length()

你会得到一个空指针异常。在调用setContentView之后,将此代码添加到onCreate方法中

代码语言:javascript
复制
text1 = (EditText)findViewById(R.id.idOfWidget);
text2 = (EditText)findViewById(R.id.idOfWidget2);

(当然,用正确的常量替换这两个ids )。

票数 2
EN

Stack Overflow用户

发布于 2011-11-23 00:26:27

除非您将它们设置在此处未显示的其他位置,否则text1text2不包含任何引用,因此当您尝试对它们调用方法时,它们会崩溃。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8230072

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档