-2

Alright, I am newer to the forums here so I am not sure if this question will be specific enough, but my code is below.

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

        Button button = (Button) findViewById(R.id.button);

        button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                String firstNo = ((EditText) findViewById(R.id.txtRent)).getText().toString();
                int ApartmentCost = Integer.parseInt(firstNo);

                TextView txtTotal = (TextView) findViewById(R.id.lblTotal);
                txtTotal.setText(ApartmentCost);

            }
        });
    }

Whenever I try to place a value into the textbox to see if it displays i get the following error codes.

02-04 10:42:19.496    1072-1072/com.example.itc201.appartmentsharing E/AndroidRuntime﹕ FATAL EXCEPTION: main
    android.content.res.Resources$NotFoundException: String resource ID #0x1f4
            at android.content.res.Resources.getText(Resources.java:239)
            at android.widget.TextView.setText(TextView.java:3844)
            at 

com.example.itc201.appartmentsharing.MainActivity$1.onClick(MainActivity.java:34)
            at android.view.View.performClick(View.java:4240)
            at android.view.View$PerformClick.run(View.java:17721)
            at android.os.Handler.handleCallback(Handler.java:730)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5103)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)

Any help would be great!

  • 2
    Use `txtTotal.setText(String.valueOf(ApartmentCost));` issue is occurring because you are passing `int` to `seText` which system considering as resource id – ρяσѕρєя K Feb 04 '15 at 15:52
  • 1
    possible duplicate of [android.content.res.Resources$NotFoundException: String resource ID Fatal Exception in Main](http://stackoverflow.com/questions/15191092/android-content-res-resourcesnotfoundexception-string-resource-id-fatal-except) – njzk2 Feb 04 '15 at 15:55

2 Answers2

-1

Change this line

 txtTotal.setText(ApartmentCost);

to this line

txtTotal.setText(ApartmentCost+"");

Since ApartmentCost is an int type it gets confused with Resoure id.

Muthu
  • 1,022
  • 1
  • 7
  • 17
-1

There are 3 errors in your code

first you can't get view inside onClick as you did in code, you did it for edittext and textview. By stacktrace your code crashs when you try to get data from resource without valid resourceid.

second, put a int into a string will return a error again

to fix

TextView txtTotal;
EditText edtFirst;

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

    Button button = (Button) findViewById(R.id.button);
    txtTotal = (TextView) findViewById(R.id.lblTotal);
    edtFirst = (EditText) findViewById(R.id.txtRent);

    button.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            String firstNo = edtFirst.getText().toString();
            txtTotal.setText(firstNo);
            int ApartmentCost = Integer.parseInt(firstNo); //if you need a int after
        }
    });
}

Another point, be sure that edittext with this resource (R.id.txtRent) exists

Ivan
  • 978
  • 6
  • 13