我无法将int值从一个活动转移到另一个活动

时间:2013-07-13 23:30:06

标签: android android-activity

在第一个活动中我有一个代数运算,我需要将结果从第一个活动传递给第二个活动。我骗了我发现的一切,但我是一个初学者,所以我找不到真正的问题。

这是第一项活动:

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class Contar extends MainActivity{

public EditText et1, et2, et3;
public TextView tv1;

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

    et1=(EditText)findViewById(R.id.et1);
    et2=(EditText)findViewById(R.id.et2);
    et3=(EditText)findViewById(R.id.et3);
    tv1=(TextView)findViewById(R.id.tv1);
}

public void contarbesos(View view){

    String meses=et1.getText().toString();
    String bd=et2.getText().toString();
    String dsvs=et3.getText().toString();

    int nro1=Integer.parseInt(meses);
    int nro2=Integer.parseInt(bd);
    int nro3=Integer.parseInt(dsvs);

    int sm=4;
    int dsvm;
    int dvm;
    int dr;
    int besos;
    int dsv;

    dr=nro1*30;

    dsv=nro3*sm;

    dsvm=nro1*dsv;
    dvm=dr-dsvm;
    besos=dvm*nro2;

    Intent i = new Intent(this, Resultado.class );
    i.putExtra("besos", besos);
    startActivity(i);


    }
}

第二项活动:

import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class Resultado extends Contar {

public TextView tv1;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.resultado);

    tv1=(TextView)findViewById(R.id.tv1);

    Bundle bundle = getIntent().getExtras();
    String  intValue = bundle.getString("besos");
    tv1.setText(intValue);


}

}

和logcat中的错误(UPDATED):

07-13 23:40:14.084: E/AndroidRuntime(768): FATAL EXCEPTION: main
07-13 23:40:14.084: E/AndroidRuntime(768): java.lang.RuntimeException: Unable to start activity ComponentInfo{whao.whaoapps.kisscounter/whao.whaoapps.kisscounter.Resultado}: android.content.res.Resources$NotFoundException: String resource ID #0x30c
07-13 23:40:14.084: E/AndroidRuntime(768):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
07-13 23:40:14.084: E/AndroidRuntime(768):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
07-13 23:40:14.084: E/AndroidRuntime(768):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-13 23:40:14.084: E/AndroidRuntime(768):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
07-13 23:40:14.084: E/AndroidRuntime(768):  at android.os.Handler.dispatchMessage(Handler.java:99)
07-13 23:40:14.084: E/AndroidRuntime(768):  at android.os.Looper.loop(Looper.java:137)
07-13 23:40:14.084: E/AndroidRuntime(768):  at android.app.ActivityThread.main(ActivityThread.java:5039)
07-13 23:40:14.084: E/AndroidRuntime(768):  at java.lang.reflect.Method.invokeNative(Native Method)
07-13 23:40:14.084: E/AndroidRuntime(768):  at java.lang.reflect.Method.invoke(Method.java:511)
07-13 23:40:14.084: E/AndroidRuntime(768):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-13 23:40:14.084: E/AndroidRuntime(768):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-13 23:40:14.084: E/AndroidRuntime(768):  at dalvik.system.NativeStart.main(Native Method)
07-13 23:40:14.084: E/AndroidRuntime(768): Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x30c
07-13 23:40:14.084: E/AndroidRuntime(768):  at android.content.res.Resources.getText(Resources.java:230)
07-13 23:40:14.084: E/AndroidRuntime(768):  at android.widget.TextView.setText(TextView.java:3640)
07-13 23:40:14.084: E/AndroidRuntime(768):  at whao.whaoapps.kisscounter.Resultado.onCreate(Resultado.java:20)
07-13 23:40:14.084: E/AndroidRuntime(768):  at android.app.Activity.performCreate(Activity.java:5104)
07-13 23:40:14.084: E/AndroidRuntime(768):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
07-13 23:40:14.084: E/AndroidRuntime(768):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
07-13 23:40:14.084: E/AndroidRuntime(768):  ... 11 more

1 个答案:

答案 0 :(得分:1)

“besos”是一个int。您必须使用getInt (String key, int defaultValue)来获取其值。

int intValue = bundle.getInt("besos", 0);
tv1.setText(String.valueOf(intValue));