将输入值添加到不同 activity 中的现有值

Adding an input value onto an existing value in a different activity

我想在我的 MainActivity.java 上创建一个 运行 总计,这将通过添加在 ActivityAdd.java 中计算的整数然后将它们通过 onClick 发送到 TextView 中来完成MainActivity.java。

由于堆栈流错误指出无效 int“”,目前无法打开应用程序。对此有什么建议吗?

AddActivity.Java

 OnClickListener button02OnClickListener =
            new OnClickListener(){

                @Override
                public void onClick(View v) {

                    String theText = result.getText().toString();

                Intent intent = new Intent(getApplicationContext(),MainActivity.class);
                intent.putExtra("calorie", theText);
                startActivity(intent);

                }};

MainActivity.Java

   String calorie = getIntent().getStringExtra("calorie");
    TextView textView1 = (TextView)findViewById(R.id.textView1);
    Integer oldValue = Integer.parseInt(textView1.getText().toString());
    Integer newValue = Integer.parseInt(calorie);

    textView1.setText((oldValue + newValue));

StackFlow 错误

 02-24 09:42:39.873    2535-2535/com.example.student.neillapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.student.neillapp, PID: 2535
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.student.neillapp/com.example.student.neillapp.MainActivity}: java.lang.NumberFormatException: Invalid int: ""
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access0(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
     Caused by: java.lang.NumberFormatException: Invalid int: ""
            at java.lang.Integer.invalidInt(Integer.java:138)
            at java.lang.Integer.parseInt(Integer.java:358)
            at java.lang.Integer.parseInt(Integer.java:334)
            at com.example.student.neillapp.MainActivity.onCreate(MainActivity.java:30)
            at android.app.Activity.performCreate(Activity.java:5933)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access0(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

如果我明白了,你正在寻找的是开始 activity 结果...

因此,要在您的主要 activity 中执行此操作,您必须像这样启动 ActivityAdd 以获得结果..

Intent intent = new Intent(getActivity(), ActivityAdd.class);

startActivityForResult(intent,1);

同样在主 activity 中,然后你像这样覆盖方法 onActivityResult :

 @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if(resultCode == getActivity().RESULT_OK){
        //UPDATE TEXTVIEWS HERE
        //DATA IS THE INTENT
    }
  }

现在在我们的添加 activity 中,我们必须 return 得到它后的结果..

Intent intent = new Intent(getApplicationContext(),MainActivity.class);
intent.putExtra("calorie", theText);

setResult(RESULT_OK, intent);

finish(); 

在你MainActivity.java

String calorie = getIntent().getStringExtra("calorie");
TextView textView1 = (TextView)findViewById(R.id.TextView1);
Integer oldValue = Integer.parseInt(textView1.getText().toString());
Integer newValue = Integer.parseInt(calorie);

//setText() needs a String to set so you can do anyone of the following

textView1.setText(""+(oldValue + newValue));
or

Integer total = oldValue + newValue;
textView1.setText(total.toString());

由于 textView1 中的空字符串 "" 而发生错误。

您没有检查字符串是否为空。

所以,我添加了额外的 2 行。

String calorie = getIntent().getStringExtra("calorie");
TextView textView1 = (TextView)findViewById(R.id.textView1);

String strOldValue = textView1.getText().toString();
//Integer oldValue = StringUtils.isNotBlank(myString) ? Integer.parseInt(myString) : 0;

//UPDATE: Replace above with
//Integer oldValue = (myString != null && !myString.isEmpty()) ? Integer.parseInt(myString) : 0;

//UPDATE:
Integer oldValue = 0;
try {
    oldValue = Integer.parseInt(myString);
} catch(Exception e) {
    //oldValue = 0;
}

Integer newValue = Integer.parseInt(calorie);

textView1.setText((oldValue + newValue));

我认为这可能对您有所帮助。