Reputation: 853
This code I am working on requires the user to press a button after entering a phone number into a text box. The button is made to save the number.
When I run the code I want to have it present the user with an alert dialog asking them to enter a number if they pressed the button with no number present.
Instead of getting the alert dialog the app crashes. But with a number entered the app works just fine.
Any advice on this would be awesome - Here is the code with the stack trace under that. -Thank you!
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
Upvotes: 0
Views: 88
Reputation: 44571
I'm guessing this sharedData
is a TextView
. Which you are still trying to parse the result when it is null, hence the NFE. Put in a check to only run that code if there is a valid number. You are checking if it is null
, which the TextView
may not be but the contents are an empty String
which you are trying to parse
Change
if (sharedData == null)
to something like
if (sharedData == null || sharedData.getText().toString().equals(""))
{
...
I would try to get the text before that line into your stringData
variable then check if that is empty. Also, you will want to put a try/catch
around the parsing code in case they try to enter a non-digit and display a message, unless you've already done that
Upvotes: 1