Android app crashes after a variable is loaded from component

cambis

I have quite a simple application. However, after I clik on the button, app crashes. Tried to debug it and the problem seems to be in first 3 row of the onClick method. Once I tried to get there values manually, not via those edit boxes, everything went smoothly. Any ideas please?

public class MainActivity extends AppCompatActivity {

    EditText editText_pocetKM;
    EditText editText_spotreba;
    EditText editText_cenaPHM;
    TextView textView_spotrebaO;
    TextView textView_cenaO;

    DecimalFormat df = new DecimalFormat("0.00##");

    @Override
    protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        editText_pocetKM = (EditText) findViewById(R.id.editText1_pocetKM);
        editText_spotreba = (EditText) findViewById(R.id.editText_Spotreba);
        editText_cenaPHM = (EditText) findViewById(R.id.editText1_cenaPHM);
        textView_spotrebaO = (TextView) findViewById(R.id.textView_spotrebaO);
        textView_cenaO = (TextView) findViewById(R.id.textView_cenaO);
    }

    public void onClick(View v) {
        Double pocetKm = Double.parseDouble(editText_pocetKM.getText().toString());
        Double spotreba = Double.parseDouble(editText_spotreba.getText().toString());
        Double cenaPHM = Double.parseDouble(editText_cenaPHM.getText().toString());

        Double spotrebaO = spotreba * pocetKm / 100;
        Double  cenaO = spotrebaO * cenaPHM;

        textView_cenaO.setText("Cena za spotřebované palivo bude "+ df.format(cenaO) + " Kč");
        textView_spotrebaO.setText("Celkem bude spotřebováno "+ df.format(spotrebaO) + " litrů paliva");
    }
}
Reaz Murshed

You didn't provide the logcat of your crash report. If the logcat was provided we could be certain of your exact problem. But anyway, as you've got rid of your crash by removing the first three lines of your onClick function, I suppose you're setting invalid inputs in your EditText.

You're parsing the text entered in the EditText to double which will fail if the input is not a valid double string. For example, it'll parse 11.01 fine when it'll throw an exception while parsing Hello.

So to check if the application is crashing for a parsing error, you might consider surrounding them with a try/catch block like this.

try {
    Double pocetKm = Double.parseDouble(editText_pocetKM.getText().toString());
    Double spotreba = Double.parseDouble(editText_spotreba.getText().toString());
    Double cenaPHM = Double.parseDouble(editText_cenaPHM.getText().toString());
} catch (Exception e) {
    e.printStackTrace();
    Toast.makeText(this, "Parsing error", Toast.LENGTH_LONG).show();
}

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Flutter android app crashes after adding Firebase library

分類Dev

App crashes after upgrade expo sdk from 30 to 31

分類Dev

App crashes on SocketTimeoutException in Android

分類Dev

Android app crashes on startup

分類Dev

Android: App crashes on calling toast from another class

分類Dev

App sometimes crashes with Resources$NotFoundException after switching to android app bundle distribution

分類Dev

Angular 6 - Custom component from shared module not loaded in app module's page view

分類Dev

App crashes with SecurityException on Android M

分類Dev

Xamarin Forms Android app crashes after creating a class object during event handler

分類Dev

Cocos2d-js: Android app crashes after sleeping time (JS_AbortIfWrongThread)

分類Dev

After migration my iOS app from Fabric to Firebase crashes still appearing in Fabric

分類Dev

Xamarin iOS App crashes immediately after startup

分類Dev

App crashes after I run the script

分類Dev

Android Classification App crashes with Tensorflow Lite Model

分類Dev

Android app crashes when creating Listview in fragments

分類Dev

Can no longer launch app from FirebaseMessagingService after Android OS update

分類Dev

Elements are not visible after selecting items from dropdown in android app

分類Dev

After upgrading to google play services 8.4.0 my app crashes on startup

分類Dev

Xamarin IOS app crashes only after putting on itunesconnect and physical device

分類Dev

Flutter IOS app crashes after few day of installation

分類Dev

Android Studio Fails to Build Without Cast, But App Crashes With Cast

分類Dev

android studio app crashes when setting gradient background

分類Dev

Xamarin Android app crashes when it was compiled with r8 shrinking

分類Dev

Release app crashes, debugging on device works fine - android

分類Dev

http get android app crashes no result nor error

分類Dev

android: app stops and then crashes when the screen goes off

分類Dev

Android app crashes when using AsyncTask for fetching an url with jsoup

分類Dev

Android Wear App Crashes when Register a Sensor Listener

分類Dev

Android App not start after click

Related 関連記事

  1. 1

    Flutter android app crashes after adding Firebase library

  2. 2

    App crashes after upgrade expo sdk from 30 to 31

  3. 3

    App crashes on SocketTimeoutException in Android

  4. 4

    Android app crashes on startup

  5. 5

    Android: App crashes on calling toast from another class

  6. 6

    App sometimes crashes with Resources$NotFoundException after switching to android app bundle distribution

  7. 7

    Angular 6 - Custom component from shared module not loaded in app module's page view

  8. 8

    App crashes with SecurityException on Android M

  9. 9

    Xamarin Forms Android app crashes after creating a class object during event handler

  10. 10

    Cocos2d-js: Android app crashes after sleeping time (JS_AbortIfWrongThread)

  11. 11

    After migration my iOS app from Fabric to Firebase crashes still appearing in Fabric

  12. 12

    Xamarin iOS App crashes immediately after startup

  13. 13

    App crashes after I run the script

  14. 14

    Android Classification App crashes with Tensorflow Lite Model

  15. 15

    Android app crashes when creating Listview in fragments

  16. 16

    Can no longer launch app from FirebaseMessagingService after Android OS update

  17. 17

    Elements are not visible after selecting items from dropdown in android app

  18. 18

    After upgrading to google play services 8.4.0 my app crashes on startup

  19. 19

    Xamarin IOS app crashes only after putting on itunesconnect and physical device

  20. 20

    Flutter IOS app crashes after few day of installation

  21. 21

    Android Studio Fails to Build Without Cast, But App Crashes With Cast

  22. 22

    android studio app crashes when setting gradient background

  23. 23

    Xamarin Android app crashes when it was compiled with r8 shrinking

  24. 24

    Release app crashes, debugging on device works fine - android

  25. 25

    http get android app crashes no result nor error

  26. 26

    android: app stops and then crashes when the screen goes off

  27. 27

    Android app crashes when using AsyncTask for fetching an url with jsoup

  28. 28

    Android Wear App Crashes when Register a Sensor Listener

  29. 29

    Android App not start after click

ホットタグ

アーカイブ