Why does my activity crash when I try to get a number?

Gabriela Marinho

I have an activity, called AddItem, which contains a couple fields that the user fills out and I am now trying to pass them to another activity. I was able to get the first two fields by doing this:

String messageText = ((EditText) findViewById(R.id.inputName)).getText().toString();
String discriptionText = ((EditText) findViewById(R.id.description)).getText().toString();

The above code worked fun, but then I tried to get another value which I then cast to a double like so:

double Latitude = Double.parseDouble(((EditText) findViewById(R.id.Latitude)).getText().toString());

It's kind of long and complicated but I'm basically doing the same thing with the exception of parsing the String and converting it to a double value. I determined that this is the problem code because when I comment it out the rest of the app runs fine.

Here is my Activity:

public class AddItem extends AppCompatActivity {

  EditText inputedTask, inputedDescription, inputedLatitude;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add_item);
    inputedTask = (EditText) findViewById(R.id.inputName);
    inputedDescription = (EditText) findViewById(R.id.description);
    inputedLatitude = (EditText) findViewById(R.id.Latitude);
  }

  public void onSaveItemButton(View view) {
    String messageText = ((EditText) findViewById(R.id.inputName)).getText().toString();
    String discriptionText = ((EditText) findViewById(R.id.description)).getText().toString();
    double Latitude = Double.parseDouble(((EditText) findViewById(R.id.Latitude)).getText().toString());

    if (messageText.equals(""));
    else {
      Intent intent = new Intent();
      intent.putExtra(Intent_Constant.INTENT_MESSAGE_FIELD, messageText);
      setResult(Intent_Constant.INTENT_RESULT_CODE, intent);
      finish();
    }
  }
}
ישו אוהב אותך

You need to make a public method for the onClick, from the documentation:

Within the Activity that hosts this layout, the following method handles the click event:

/** Called when the user touches the button */ 
public void sendMessage(View view) {
  // Do something in response to button click 
}

The method you declare in the android:onClick attribute must have a signature exactly as shown above. Specifically, the method must:

  • Be public
  • Return void
  • Define a View as its only parameter (this will be the View that was clicked)

So you need to change the method to public:

public void onSaveItemButton(View view) {
  ...
}

UPDATE:
As the error log says:

at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) Caused by: java.lang.NumberFormatException: Invalid double: "" at java.lang.StringToReal.invalidReal(StringToReal.java:63) at java.lang.StringToReal.parseDouble(StringToReal.java:267) at java.lang.Double.parseDouble(Double.java:301) at cs4720.cs.virginia.edu.duysalahandroidminiproject02.AddItem.onSaveItemButton(AddItem.java:33)

You need to catch for empty string in the following code:

double Latitude = Double.parseDouble(((EditText) findViewById(R.id.Latitude)).getText().toString());

so, check it first:

String val = ((EditText) findViewById(R.id.Latitude)).getText().toString();
if(!val.equals("") {
  double Latitude = Double.parseDouble(val);
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why does my app crash when I try to display my ip usig Inet?

Why does my C program crash when I try to realloc() a pointer array of structs?

Why does my Android studio App crash when I spam click at the beginning of an activity?

Why am I getting a crash when exiting my Activity?

Why does Python crash when I try to sum this numpy array?

Why does tkinter crash when i try using the keyboard module?

How to use an activity method in a non - activity class ? My program crash when I try use a context

Why does my app crash when I change my namespace?

Why is it when I'm downloading my array and try to get the length of it, it just spits out the number of bytes the array is

Why does my program crash when I increment a pointer and then delete it?

Why does my computer crash when I minimise the last window?

Why does my variable (winning) not get reassigned when I try to reassign it?

My app crash when try to pass data from an activity to a fragment

Why does my program break when I try to refresh the usestate?

Why does my activity reset when I tap on the back button?

android application crash when i try to open activity

why my app crash when i try to post some data into web service?

Why my QTextEdit inherited rich text editor always crash when I try to call textCursor()?

Why is my .load() not working when I try to get specific div

Why I obtain this RuntimeException when I try to set a different content view into the onCreate() method of my activity?

Why does my spring webapp using Freemarker get slower when I increase CPU core number?

Why does my express router crash only when i send a JSON and not when i send a text ?

Why does my WPF application crash when I bump my mousewheel?

When I use [[]] as my index number, why does it return this?

Why does the following code crash when I input a 12 digit number?

Why does my Android app crash when my threads exit?

Why does my C program crash when i add any statement to the main function?

Why does my RSA key look different on my server than when I try to connect?

Why does my Timer crashes my android activity when I stop it?

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive