0

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

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

应用程序崩溃,而不是获得警报对话框。但是输入数字后,该应用程序就可以正常工作。

任何关于这方面的建议都会很棒 - 这是下面带有堆栈跟踪的代码。-谢谢!

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

////堆栈跟踪:

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
4

1 回答 1

1

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

改变

if (sharedData == null)

类似于

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

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

于 2013-05-14T16:46:29.960 回答