从意图转换String到int

时间:2014-12-25 22:06:54

标签: java android android-intent string-conversion

我有两个活动,我正在将编辑号editText数据传递给第二个活动。但在第二个活动中,我无法使用任何函数将数据从String转换为int。这是我的代码:

这是主要功能:

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);
            }
        });
    }
}

这是传递数据的第二个活动:

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);
            }
         });
    }
}

这是日志:

  `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)                 

2 个答案:

答案 0 :(得分:2)

有几种TextView.setText(...)。一个需要CharSequence,这可能是你期望使用的,而另一个常用的变体需要an int。 int表单要求参数为资源ID。

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

鉴于(i/2)+7可能无法解析为字符串资源,您可以这样做:

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

或更好:

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

答案 1 :(得分:2)

  

MainActivity .java

中使用此代码
int age = Integer.parseInt(mage.getText().toString());
Intent i = new Intent(MainActivity.this,FinalActivity.class);
        i.putExtra("age", age);
        startActivity(i);
  

FinalActivity的代码 .java

     Bundle extras = getIntent().getExtras();  
     int age = extras.getInt("age");
  

通过这种方式,您可以将整数值发送到其他活动。   EnJoY编码:)