Search code examples
javaandroidandroid-intentstring-conversion

Converting String to int from intent


I have 2 activities, and I'm passing edit number editText data to 2nd activity with intent. But in second activity I can't convert data from String to int with any functions. Here is my code:

This is main function:

public class MainActivity extends ActionBarActivity {

    public EditText mAge;
    public Button mCalculate;

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

        mAge = (EditText) findViewById(R.id.numberImputEditText);
        mCalculate = (Button) findViewById(R.id.calculateButton);

        mCalculate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String age = mAge.getText().toString();

                Intent mIntent = new Intent(getApplicationContext(),FinalActivity.class);
                mIntent.putExtra("age", age);
                startActivity(mIntent);
            }
        });
    }
}

and this is 2nd activity where is data passed:

public class FinalActivity extends Activity {

    public Button mCalculateAgain;
    public TextView mMinAge;
    public TextView mMaxAge;


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

        Intent mIntent = getIntent();
        String age = mIntent.getStringExtra("age");

        mCalculateAgain = (Button) findViewById(R.id.calculateAgainButton);
        mMinAge = (TextView) findViewById(R.id.minAgeTextView);
        mMaxAge = (TextView) findViewById(R.id.maxAgeTextView);

        Integer i = Integer.parseInt(age);

        mMinAge.setText((i/2)+7);
        mMaxAge.setText((i-7)*2);

        Toast.makeText(getApplicationContext(), age, Toast.LENGTH_LONG).show();

         mCalculateAgain.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent mIntent = new Intent(getApplicationContext(),MainActivity.class);
                startActivity(mIntent);
            }
         });
    }
}

here's the log:

  `java.lang.RuntimeException: Unable to start activity ComponentInfo{com.robigroza.halfyourage/com.robigroza.halfyourage.FinalActivity}: android.content.res.Resources$NotFoundException: String resource ID #0x12
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2114)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2139)
        at android.app.ActivityThread.access$700(ActivityThread.java:143)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1241)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4960)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
        at dalvik.system.NativeStart.main(Native Method)
     Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x12
        at android.content.res.Resources.getText(Resources.java:1058)
        at android.widget.TextView.setText(TextView.java:3857)
        at com.robigroza.halfyourage.FinalActivity.onCreate(FinalActivity.java:35)
        at android.app.Activity.performCreate(Activity.java:5203)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2078)                 

Solution

  • There's a few types of TextView.setText(...). One takes a CharSequence, which is probably what you expected to use, and another commonly used variant takes an int. The int form expects the parameter to be a resource ID.

    mMinAge.setText((i/2)+7);
    mMaxAge.setText((i-7)*2);
    

    Given that (i/2)+7 isn't likely to resolve to a string resource, you could do:

    mMinAge.setText("" + (i/2)+7);
    mMaxAge.setText("" + (i-7)*2);
    

    or better:

    mMinAge.setText(String.valueOf((i/2)+7));
    mMaxAge.setText(String.valueOf((i-7)*2));