0

I am getting problem with my application.it is crashing on pressing any of the button.

I have used float conversion of string object.

My MainActivity.java file:

package com.example.admin.myapplication;

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;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    TextView tv1,tv2;
    EditText et1;
    Button btn1,btn2,btn3,btn4;

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

        tv1=findViewById(R.id.tv1);
        tv2=findViewById(R.id.tv2);
        et1=findViewById(R.id.et1);
        btn1=findViewById(R.id.btn1);
        btn2=findViewById(R.id.btn2);
        btn3=findViewById(R.id.btn3);
        btn4=findViewById(R.id.btn4);
        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        btn3.setOnClickListener(this);
        btn4.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        tv2.findViewById(R.id.tv2);
        String value=et1.getText().toString();
        int newvalue=Integer.parseInt(value);
        int thirdvalue=0;

        switch (v.getId()){

            case R.id.btn1:
                thirdvalue = (int) (newvalue*37.98f);
                tv2.setText(thirdvalue);
                break;

            case R.id.btn2:
                thirdvalue = (int) (newvalue*45.47f);
                tv2.setText(thirdvalue);
                break;

            case R.id.btn3:
                thirdvalue = (int) (newvalue*50.52f);
                tv2.setText(thirdvalue);
                break;

            case R.id.btn4:
                thirdvalue = (int) (newvalue*63.16f);
                tv2.setText(thirdvalue);
                break;
        }
    }
}

I want to get result on pressing any of the button and give result as per pressed button.

It is giving this error in my logcat:

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.admin.myapplication, PID: 14892 android.content.res.Resources$NotFoundException: String resource ID #0x3b5 at android.content.res.Resources.getText(Resources.java:328) at android.content.res.MiuiResources.getText(MiuiResources.java:125) at android.widget.TextView.setText(TextView.java:4432) at com.example.admin.myapplication.MainActivity.onClick(MainActivity.java:45) at android.view.View.performClick(View.java:5215) at android.view.View$PerformClick.run(View.java:21196) at android.os.Handler.handleCallback(Handler.java:742) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:157) at android.app.ActivityThread.main(ActivityThread.java:5603) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:774) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:652) I/Process: Sending signal. PID: 14892 SIG: 9 Application terminated.

Minas Mina
  • 2,058
  • 3
  • 21
  • 35

4 Answers4

2

you cant add integer value in setText method. Try with tv2.setText(thirdvalue + ""); or tv2.setText(String.valueOf(thirdvalue));

Read up on setText() method here

isaaaaame
  • 460
  • 4
  • 9
2

replace all tv2.setText(thirdvalue); with tv2.setText(String.valueOf(thirdvalue));

and you don't need to add this(tv2.findViewById(R.id.tv2);) in onClick method

Khaled Qasem
  • 879
  • 7
  • 20
1

add String.valueOf(thirdValue) because probably he is searching for id instead of setting the text

tv2.setText(String.valueOf(thirdvalue));
Ahmad Riyal
  • 21
  • 1
  • 7
0

You should pass the argument of setText() as String so change your code to

tv2.setText(thirdvalue.toString); 

and same for the remaining buttons as well. This will make it as a String.

Siva Perumal
  • 457
  • 2
  • 8
  • 23