-1

here i am adding two numers by reading a values from edittext but it throws exetion how to resolve it . everything seems correct. please help me package com.example.centum.addition1;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import static android.app.PendingIntent.getActivity;
import static android.widget.Toast.LENGTH_LONG;
import static com.example.centum.addition1.R.id.et1;
import static com.example.centum.addition1.R.id.et2;

public class MainActivity extends AppCompatActivity {
    public EditText etext1,etext2;
    public TextView tview1;
    public Button button1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        etext1=(EditText)findViewById(R.id.et1);
        etext2=(EditText)findViewById(R.id.et2);
        button1=(Button)findViewById(R.id.b1);
        tview1=(TextView)findViewById(R.id.tv1);

        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try{
                    String num1,num2;
                    num1=etext1.getText().toString();
                    num2=etext2.getText().toString();
                    int i = Integer.parseInt(num1);
                    int j = Integer.parseInt(num2);
                    int k=i+j;
                    Toast.makeText(MainActivity.this, k, LENGTH_LONG).show();

                }catch(Exception e)
                {
                    Toast.makeText(MainActivity.this, "error", LENGTH_LONG).show();
                }
            }
        });
    }
}
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
praveen
  • 71
  • 6
  • 1
    share your crash log with question – AskNilesh Sep 12 '17 at 05:24
  • 1
    The very first thing you should do is to step through your code with a debugger. My guess is that it's likely you'll find that you are trying parse strings which can't be marshalled into integers. – Tim Biegeleisen Sep 12 '17 at 05:26
  • 1
    may be error in `Toast.makeText(MainActivity.this, k, LENGTH_LONG).show();` this line because you are trying to display an integer in Toast message, you can try `Toast.makeText(MainActivity.this, k + " ", LENGTH_LONG).show();` – Shreeya Chhatrala Sep 12 '17 at 05:35

2 Answers2

0

Message in the Toast has to be String, not integer. So, as Android Player_Shree said, change your message to k + " "

Yunus Kulyyev
  • 1,022
  • 15
  • 26
0

I found the Exception, it is in Toast in which you are trying to print the value of sum of two number. Toast always shows String value but in your code you provide a int value. This is the cause of exception.

  Toast.makeText(MainActivity.this, k, LENGTH_LONG).show();

you have to replace your above mention line by this:

 Toast.makeText(MainActivity.this, String.valueOf(k),LENGTH_LONG).show();
Sandeep Sankla
  • 1,250
  • 12
  • 21