Resources$NotFoundException in a simple calculator app

0

I just picked up Android coding and this is the source code for a simple Division, Multiplication, Addition and Subtraction app which is crashing on me and my friends' Android phones (Tested on a Lenovo A390 and a Nexus 4). Below is the MainActivity.java code in case you want to compile the app.

package com.mayukhnair.mathmagic;

    import android.app.Activity;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;

    public class MainActivity extends Activity {

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

    // Initializing buttons, fields etc.
    final TextView resLabel = (TextView) findViewById(R.id.textView3);
    final EditText num1 = (EditText) findViewById(R.id.editText1);
    final EditText num2 = (EditText) findViewById(R.id.editText2);
    Button getAddAnswerButton = (Button) findViewById(R.id.button1);
    Button getSubtractButton = (Button) findViewById(R.id.button2); 
    Button getDivideButton = (Button) findViewById (R.id.button3);
    Button getMultiplyButton = (Button) findViewById (R.id.button4);

    // Multiply button functionality
    getMultiplyButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            int n1=Integer.parseInt(num1.getText().toString());
            int n2=Integer.parseInt(num2.getText().toString());
            resLabel.setText((int) (n1*n2));

        }
    });

    // Divide button functionality
    getDivideButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            int n1=Integer.parseInt(num1.getText().toString());
            int n2=Integer.parseInt(num2.getText().toString());
            resLabel.setText((int) (n1/n2));

        }
    });

    // Subtract button functionality
    getSubtractButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            int n1=Integer.parseInt(num1.getText().toString());
            int n2=Integer.parseInt(num2.getText().toString());
            resLabel.setText((int) (n1-n2));                
        }
    });

    // Addition button functionality
    getAddAnswerButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            int n1=Integer.parseInt(num1.getText().toString());
            int n2=Integer.parseInt(num2.getText().toString());
            resLabel.setText((int) (n1+n2));
        }
    });
}



@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

  }

Now, here's the Activity_main XML File which adds design to the app:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Enter Number 1:" />

<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView1"
    android:layout_below="@+id/textView1"
    android:ems="10"
    android:inputType="number" >

    <requestFocus />
</EditText>

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/editText1"
    android:layout_below="@+id/editText1"
    android:layout_marginTop="15dp"
    android:text="Enter Number 2:" />

<EditText
    android:id="@+id/editText2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView2"
    android:layout_below="@+id/textView2"
    android:ems="10"
    android:inputType="number" />

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView3"
    android:layout_alignRight="@+id/textView3"
    android:layout_below="@+id/button1"
    android:layout_marginRight="36dp"
    android:text="Subtract" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/editText2"
    android:layout_alignRight="@+id/button2"
    android:layout_below="@+id/editText2"
    android:layout_marginTop="24dp"
    android:text="Add" />

<Button
    android:id="@+id/button3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView3"
    android:layout_alignRight="@+id/button2"
    android:layout_below="@+id/button2"
    android:text="Divide" />

<Button
    android:id="@+id/button4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/textView3"
    android:layout_alignLeft="@+id/textView3"
    android:layout_alignRight="@+id/button3"
    android:text="Multiply" />

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/editText2"
    android:layout_alignParentBottom="true"
    android:layout_marginBottom="32dp"
    android:text="This will be replaced with result."
    android:textAppearance="?android:attr/textAppearanceLarge" />

 </RelativeLayout>

Alright, so what happens is: Eclipse does not give any error and the app opens up on my phone. I enter 2 numbers and press any one of the 4 buttons (Add, multiply, divide, subtract). When I press the button, the app crashes saying "Unfortunately, MathMagic has stopped." And I go mad hunting for the error.

Since I heard the LogCat entry helps a lot, here you go(errors marked with **, orange warnings marked with *):

09-25 16:45:18.861: D/ActivityThread(24302): BIND_APPLICATION handled : 0 /     AppBindData{appInfo=ApplicationInfo{4166a240 com.mayukhnair.mathmagic}}
09-25 16:45:18.872: D/jdwp(24302): processIncoming
09-25 16:45:18.881: D/jdwp(24302): processIncoming
09-25 16:45:18.881: D/jdwp(24302): handlePacket : cmd=0x1, cmdSet=0xC7, len=0x13, id=0x400000ED, flags=0x0, dataLen=0x8
09-25 16:45:18.881: D/jdwp(24302): REQ: DDM.Chunk (cmd=199/1 dataLen=8 id=0x400000ed)
09-25 16:45:18.881: D/jdwp(24302): reply: dataLen=9 err=NONE(0)
09-25 16:45:18.888: D/jdwp(24302): processIncoming
09-25 16:45:18.888: D/jdwp(24302): handlePacket : cmd=0x1, cmdSet=0xC7, len=0x17, id=0x400000EE, flags=0x0, dataLen=0xC
09-25 16:45:18.888: D/jdwp(24302): REQ: DDM.Chunk (cmd=199/1 dataLen=12 id=0x400000ee)
09-25 16:45:18.888: D/jdwp(24302): reply: dataLen=98 err=NONE(0)
09-25 16:45:18.888: D/jdwp(24302): processIncoming
09-25 16:45:18.888: D/jdwp(24302): handlePacket : cmd=0x1, cmdSet=0xC7, len=0x13, id=0x400000EF, flags=0x0, dataLen=0x8
09-25 16:45:18.888: D/jdwp(24302): REQ: DDM.Chunk (cmd=199/1 dataLen=8 id=0x400000ef)
09-25 16:45:18.889: D/jdwp(24302): reply: dataLen=216 err=NONE(0)
09-25 16:45:18.889: D/jdwp(24302): processIncoming
09-25 16:45:18.889: D/jdwp(24302): handlePacket : cmd=0x1, cmdSet=0xC7, len=0x13, id=0x400000F0, flags=0x0, dataLen=0x8
09-25 16:45:18.889: D/jdwp(24302): REQ: DDM.Chunk (cmd=199/1 dataLen=8 id=0x400000f0)
09-25 16:45:18.889: D/jdwp(24302): reply: dataLen=9 err=NONE(0)
09-25 16:45:18.991: D/ActivityThread(24302): ACT-AM_ON_RESUME_CALLED ActivityRecord{4166b780 token=android.os.BinderProxy@4166af40 {com.mayukhnair.mathmagic/com.mayukhnair.mathmagic.MainActivity}}
09-25 16:45:18.998: D/ActivityThread(24302): ACT-LAUNCH_ACTIVITY handled : 0 / ActivityRecord{4166b780 token=android.os.BinderProxy@4166af40 {com.mayukhnair.mathmagic/com.mayukhnair.mathmagic.MainActivity}}
09-25 16:45:18.999: D/ActivityThread(24302): ACT-AM_ON_PAUSE_CALLED ActivityRecord{4166b780 token=android.os.BinderProxy@4166af40 {com.mayukhnair.mathmagic/com.mayukhnair.mathmagic.MainActivity}}
09-25 16:45:19.002: D/ActivityThread(24302): ACT-PAUSE_ACTIVITY handled : 0 / android.os.BinderProxy@4166af40
09-25 16:45:19.004: V/Provider/Setting(24302): invalidate [system]: current 14 != cached 0
09-25 16:45:19.056: D/libEGL(24302): loaded /system/lib/egl/libGLES_android.so
09-25 16:45:19.063: D/libEGL(24302): loaded /vendor/lib/egl/libEGL_mtk.so
09-25 16:45:19.078: D/libEGL(24302): loaded /vendor/lib/egl/libGLESv1_CM_mtk.so
09-25 16:45:19.088: D/libEGL(24302): loaded /vendor/lib/egl/libGLESv2_mtk.so
09-25 16:45:19.127: D/OpenGLRenderer(24302): Enabling debug mode 0
09-25 16:45:19.131: D/ActivityThread(24302): ACT-STOP_ACTIVITY_SHOW handled : 0 / android.os.BinderProxy@4166af40
09-25 16:45:21.581: D/ActivityThread(24302): ACT-AM_ON_RESUME_CALLED ActivityRecord{4166b780 token=android.os.BinderProxy@4166af40 {com.mayukhnair.mathmagic/com.mayukhnair.mathmagic.MainActivity}}
09-25 16:45:21.581: D/ActivityThread(24302): ACT-RESUME_ACTIVITY handled : 0 / android.os.BinderProxy@4166af40
*09-25 16:45:28.368: W/ResourceType(24302): No package identifier when getting value for resource number 0x0000003c*
09-25 16:45:28.369: D/AndroidRuntime(24302): Shutting down VM
*09-25 16:45:28.369: W/dalvikvm(24302): threadid=1: thread exiting with uncaught exception (group=0x40d5f258)*
**09-25 16:45:28.389: E/AndroidRuntime(24302): FATAL EXCEPTION: main
09-25 16:45:28.389: E/AndroidRuntime(24302): android.content.res.Resources$NotFoundException: String resource ID #0x3c
09-25 16:45:28.389: E/AndroidRuntime(24302):    at android.content.res.Resources.getText(Resources.java:253)
09-25 16:45:28.389: E/AndroidRuntime(24302):    at android.widget.TextView.setText(TextView.java:3495)
09-25 16:45:28.389: E/AndroidRuntime(24302):    at com.mayukhnair.mathmagic.MainActivity$4.onClick(MainActivity.java:69)
09-25 16:45:28.389: E/AndroidRuntime(24302):    at android.view.View.performClick(View.java:3517)
09-25 16:45:28.389: E/AndroidRuntime(24302):    at android.view.View$PerformClick.run(View.java:14155)
09-25 16:45:28.389: E/AndroidRuntime(24302):    at android.os.Handler.handleCallback(Handler.java:605)
09-25 16:45:28.389: E/AndroidRuntime(24302):    at android.os.Handler.dispatchMessage(Handler.java:92)
09-25 16:45:28.389: E/AndroidRuntime(24302):    at android.os.Looper.loop(Looper.java:154)
09-25 16:45:28.389: E/AndroidRuntime(24302):    at android.app.ActivityThread.main(ActivityThread.java:4624)
09-25 16:45:28.389: E/AndroidRuntime(24302):    at java.lang.reflect.Method.invokeNative(Native Method)
09-25 16:45:28.389: E/AndroidRuntime(24302):    at java.lang.reflect.Method.invoke(Method.java:511)
09-25 16:45:28.389: E/AndroidRuntime(24302):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
09-25 16:45:28.389: E/AndroidRuntime(24302):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
09-25 16:45:28.389: E/AndroidRuntime(24302):    at dalvik.system.NativeStart.main(Native Method)**

So, that's it. I suspect the errors are in the XML Design file, but I can't find out. Help will be appreciated. Thanks.

java
android
xml
eclipse
logcat
asked on Stack Overflow Sep 25, 2013 by Mayukh Nair • edited Sep 25, 2013 by STT LCU

2 Answers

1

When calling set text on a TextView, and you want to set the text to a string, rather than a reference to a line in string.xml, you must make sure the type you're putting in there is a String.

Edit all you're onClick method to append a + "" to the end. That will turn the int to a string. Example:

resLabel.setText(((int) (n1-n2)) + "");

Hope this solves your problems :)

answered on Stack Overflow Sep 25, 2013 by Gil Moshayof
0

Can you please highlight the java line no. 69 so the error will be more clear.

Mostly the error is in

 resLabel.setText((int) (n1-n2)); 

You can only set a String variable to the label. Please check that.

answered on Stack Overflow Sep 25, 2013 by Droid

User contributions licensed under CC BY-SA 3.0