我在哪里放置事件处理程序?

时间:2012-03-19 18:21:23

标签: android

我有一个事件处理程序的代码:

rbM.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            public void onCheckedChanged(CompoundButton buttonView,
                    boolean isChecked) {
                // TODO Auto-generated method stub
                if (isChecked) {
                    try {
                        Double.parseDouble(etAmm.getText().toString());
                        if (etPerc.getText().toString() != null
                                && etPerc.getText().toString() != "") {
                            etRis.setText(new Double(
                                    Double.parseDouble(etAmm.getText()
                                            .toString()) - percentuale())
                                    .toString());
                            etPercTot.setText(new Double(percentuale())
                                    .toString() + "%");
                        }
                        et30.setText(new Double(Double.parseDouble(etAmm
                                .getText().toString()) - percentuale(30))
                                .toString());
                        etPerc30.setText(new Double(percentuale(30))
                                .toString() + "%");
                        et50.setText(new Double(Double.parseDouble(etAmm
                                .getText().toString()) - percentuale(50))
                                .toString());
                        etPerc50.setText(new Double(percentuale(50))
                                .toString() + "%");
                        et70.setText(new Double(Double.parseDouble(etAmm
                                .getText().toString()) - percentuale(70))
                                .toString());
                        etPerc70.setText(new Double(percentuale(70))
                                .toString() + "%");
                    } catch (Exception ex) {
                    }
                }
            }

        });

我在onCreate方法中有它,但是当我启动应用程序时它会崩溃。为什么??? Eclipse没有给我任何错误,如果我放一个try catch块来尝试该方法中的所有内容,我将null作为异常。我的布局是正确的,因为如果我在没有编辑任何java代码的情况下启动应用程序,它将完美地加载到屏幕上。请帮帮我!!!!!

P.S。 我有其他事件处理程序,但这是最短的,我不认为它可能会从处理程序更改为处理程序,它始终只是一个声明。

这是有人问的logcat:

03-20 15:03:13.775: D/AndroidRuntime(262): Shutting down VM

03-20 15:03:13.775: W/dalvikvm(262): threadid=1: thread exiting with uncaught exception (group=0x4001d800)

03-20 15:03:13.895: E/AndroidRuntime(262): FATAL EXCEPTION: main

03-20 15:03:13.895: E/AndroidRuntime(262): java.lang.RuntimeException: Unable to start activity ComponentInfo{ciprianis.genius.percentuali/ciprianis.genius.percentuali.PercentualiActivity}: java.lang.NullPointerException

03-20 15:03:13.895: E/AndroidRuntime(262):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)

03-20 15:03:13.895: E/AndroidRuntime(262):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)

03-20 15:03:13.895: E/AndroidRuntime(262):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)

03-20 15:03:13.895: E/AndroidRuntime(262):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)

03-20 15:03:13.895: E/AndroidRuntime(262):  at android.os.Handler.dispatchMessage(Handler.java:99)

03-20 15:03:13.895: E/AndroidRuntime(262):  at android.os.Looper.loop(Looper.java:123)

03-20 15:03:13.895: E/AndroidRuntime(262):  at android.app.ActivityThread.main(ActivityThread.java:4627)

03-20 15:03:13.895: E/AndroidRuntime(262):  at java.lang.reflect.Method.invokeNative(Native Method)

03-20 15:03:13.895: E/AndroidRuntime(262):  at java.lang.reflect.Method.invoke(Method.java:521)

03-20 15:03:13.895: E/AndroidRuntime(262):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)

03-20 15:03:13.895: E/AndroidRuntime(262):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)

03-20 15:03:13.895: E/AndroidRuntime(262):  at dalvik.system.NativeStart.main(Native Method)

03-20 15:03:13.895: E/AndroidRuntime(262): Caused by: java.lang.NullPointerException

03-20 15:03:13.895: E/AndroidRuntime(262):  at ciprianis.genius.percentuali.PercentualiActivity.onCreate(PercentualiActivity.java:62)

03-20 15:03:13.895: E/AndroidRuntime(262):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)

03-20 15:03:13.895: E/AndroidRuntime(262):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)

03-20 15:03:13.895: E/AndroidRuntime(262):  ... 11 more

2 个答案:

答案 0 :(得分:0)

我的猜测是你的处理程序是null。但没有代码,它只是一个合格的猜测。

答案 1 :(得分:0)

根据您对第62行的说法,听起来etAmm为空。要么您没有初始化它,要么FindViewById()无法在您的布局中找到它。