警报对话框无法正常运行

时间:2013-05-14 16:44:05

标签: java android button android-alertdialog

我正在处理的此代码要求用户在将文本框输入电话号码后按下按钮。该按钮用于保存号码。

当我运行代码时,我希望它为用户提供一个警告对话框,要求他们输入一个数字,如果他们按下没有数字的按钮。

应用程序崩溃,而不是获取警报对话框。但是输入一个数字应用就可以了。

对此的任何建议都很棒 - 这是下面的堆栈跟踪代码。 - 谢谢!

        b.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            //Create an if statement that brings up an alert dialog if there is no number entered into the field.
            switch (v.getId()) {
            case R.id.btnwidgetconfig: {
                if (sharedData == null)
                {
                    AlertDialog.Builder alert = new AlertDialog.Builder(c);
                    alert.setTitle("No number entered");
                    alert.setMessage("Please enter a phone number using the the text box");
                    alert.setCancelable(false);

                    alert.setPositiveButton("Okay", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            startActivityForResult(
                                    new Intent(
                                            android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS),
                                    0);
                        }
                    });


                }
                else
                {
                String stringData = sharedData.getText().toString();
                Long l = Long.parseLong(stringData);
                SharedPreferences.Editor editor = prefs.edit();
                prefs.edit().putLong(constants.KEY, l).commit();
                Toast.makeText(c, "Your number has been saved!",
                        Toast.LENGTH_LONG).show();
                svNum.setText("Saved Number: " + prefs.getLong(constants.KEY, 411));
                break;
                }
            }
            }
        }
    });

//// Stack Trace:

05-14 12:29:07.679: W/dalvikvm(30842): threadid=1: thread exiting with uncaught      exception (group=0x40018560)
05-14 12:29:07.679: E/AndroidRuntime(30842): FATAL EXCEPTION: main
05-14 12:29:07.679: E/AndroidRuntime(30842): java.lang.NumberFormatException: 
05-14 12:29:07.679: E/AndroidRuntime(30842):    at   java.lang.Long.parseLong(Long.java:337)
05-14 12:29:07.679: E/AndroidRuntime(30842):    at java.lang.Long.parseLong(Long.java:311)
05-14 12:29:07.679: E/AndroidRuntime(30842):    at example.save.phonenum.Settings$1.onClick(WWSettings.java:106)
05-14 12:29:07.679: E/AndroidRuntime(30842):    at android.view.View.performClick(View.java:2485)
05-14 12:29:07.679: E/AndroidRuntime(30842):    at android.view.View$PerformClick.run(View.java:9080)
05-14 12:29:07.679: E/AndroidRuntime(30842):    at android.os.Handler.handleCallback(Handler.java:587)
05-14 12:29:07.679: E/AndroidRuntime(30842):    at android.os.Handler.dispatchMessage(Handler.java:92)
05-14 12:29:07.679: E/AndroidRuntime(30842):    at android.os.Looper.loop(Looper.java:130)
05-14 12:29:07.679: E/AndroidRuntime(30842):    at android.app.ActivityThread.main(ActivityThread.java:3683)
05-14 12:29:07.679: E/AndroidRuntime(30842):    at java.lang.reflect.Method.invokeNative(Native Method)
05-14 12:29:07.679: E/AndroidRuntime(30842):    at java.lang.reflect.Method.invoke(Method.java:507)
05-14 12:29:07.679: E/AndroidRuntime(30842):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:864)
05-14 12:29:07.679: E/AndroidRuntime(30842):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
05-14 12:29:07.679: E/AndroidRuntime(30842):    at dalvik.system.NativeStart.main(Native Method)
05-14 12:29:15.569: W/IInputConnectionWrapper(30870): showStatusIcon on inactive InputConnection

1 个答案:

答案 0 :(得分:1)

我猜这个sharedDataTextView。当你为null时,你仍然试图解析结果,因此是NFE。如果有有效数字,请选中仅运行该代码的检查。您正在检查它是nullTextView可能不是,但内容是空的String,您尝试parse

更改

if (sharedData == null)

类似

if (sharedData == null || sharedData.getText().toString().equals(""))
{
    ...

我会尝试将该行前的文本放入stringData变量,然后检查是否为空。此外,您将需要在解析代码周围放置一个try/catch,以防他们尝试输入非数字并显示消息,除非您已经这样做了