1. Are you ready for the Galaxy S20? Here is everything we know so far!

HELP NEEDED IN THE CODE

Discussion in 'Android Development' started by vishalbhardwaj0411, Nov 22, 2020.

  1. vishalbhardwaj0411

    Thread Starter

    I just started learning android development and was trying to build a basic currency conversion android app with the code.

    -----------------------------------------------------------CODE----------------------------------------------------------------------------
    package com.example.currencyconversion;

    import androidx.appcompat.app.AppCompatActivity;

    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;
    import android.widget.Toast;

    import org.w3c.dom.Text;

    public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    /*-----------Creating objects of buttons and text field-----------*/
    private TextView input;
    private TextView output;

    private int result;
    private int value = 0;

    private Button toInr;
    private Button toUsd;
    /*-----------Creating objects of buttons and text field-----------*/
    @override
    protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    /*---------------Refrence for XML---------------------------*/
    input = findViewById(R.id.currency_input);
    output = findViewById(R.id.currency_output);

    toInr = findViewById(R.id.to_inr);
    toUsd = findViewById(R.id.to_usd);
    /*----xxx-----------Refrence for XML----------------------xxx-----*/
    value = Integer.parseInt(input.getText().toString());
    /*---------------On click listener---------------------------*/
    toInr.setOnClickListener(this);
    toUsd.setOnClickListener(this);
    /*---xxx-----------On click listener-----------------------xxx----*/
    }
    /*---------------On click listener switch case---------------------------*/

    public void onClick(View view) {
    switch (view.getId())
    {
    case R.id.to_inr:
    Toast.makeText(MainActivity.this , "to inr" , Toast.LENGTH_SHORT).show();
    result = value * 74;
    output.setText(Integer.toString(result));


    break;
    case R.id.to_usd:
    Toast.makeText(MainActivity.this , "to usd" , Toast.LENGTH_SHORT).show();
    }
    }
    /*-----xxx----------On click listener switch case-----------------xxx----------*/
    }
    ----------------------------------------------------------END OF-CODE-----------------------------------------------------------------
    Every time I run the following code it crashes the app AND problem is in following line
    value = Integer.parseInt(input.getText().toString());
     



    1. Download the Forums for Android™ app!


      Download

       
  2. wseng92

    wseng92 Android Enthusiast

    I guess you should put that line in here

    Code (Java):
    1. case R.id.to_inr:
    2. Toast.makeText(MainActivity.this , "to inr" , Toast.LENGTH_SHORT).show();
    3. value = Integer.parseInt(input.getText().toString());  // put here
    4. result = value * 74;
    5. output.setText(Integer.toString(result));
    Do remember paste the stack trace whenever you app is crashed. This will easier for us to help.
     
    #2 wseng92, Nov 23, 2020
    Last edited: Nov 23, 2020
    codesplice likes this.
Loading...

Share This Page

Loading...