我正在处理的这段代码要求用户在文本框中输入电话号码后按下按钮。该按钮用于保存号码。
当我运行代码时,我希望它向用户显示一个警告对话框,如果他们按下没有数字的按钮,则要求他们输入一个数字。
应用程序崩溃,而不是获得警报对话框。但是输入数字后,该应用程序就可以正常工作。
任何关于这方面的建议都会很棒 - 这是下面带有堆栈跟踪的代码。-谢谢!
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