双值不会从一个活动传递到另一个活动

时间:2015-02-24 14:58:48

标签: java android android-activity android-studio

我正在尝试将活动1中的3个计算值传递给活动2.每次我在活动2中看到结果为0.0时。请帮助。

活动1:

submit = (Button)findViewById(R.id.submit);
    try {
        submit.setOnClickListener(new Button.OnClickListener() {

            @Override
            public void onClick
                    (View v) {
 Intent i = new Intent(MainActivity.this, output.class);
                Bundle b = new Bundle();
                b.putDouble("gyrerror", gyroerr);
                b.putDouble("compasserror", comperr);
                b.putDouble("deviation", dev);

                i.putExtras(b);

                startActivity(i);
            }
        });
    }
    catch( Exception e)
    {

    }
}

活动2:

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.output);


        TextView gyroerror = (TextView) findViewById(R.id.gyerr1);
        TextView comperror = (TextView) findViewById(R.id.comperr1);
        TextView deviation = (TextView) findViewById(R.id.dev1);

        Bundle b = getIntent().getExtras();
        double gyroerr =b.getDouble("gyroerr");
        double comperr= b.getDouble("comperr");
        double dev = b.getDouble("dev");

        gyroerror.setText(Double.toString(gyroerr));
        comperror.setText(Double.toString(comperr));
        deviation.setText(Double.toString(dev));
}}

堆栈跟踪:

02-24 09:38:49.775      997-997/com.example.supriyaraman.sample I/dalvikvm﹕ Could not find method android.view.ViewGroup.onNestedScrollAccepted, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onNestedScrollAccepted
02-24 09:38:49.785      997-997/com.example.supriyaraman.sample W/dalvikvm﹕ VFY: unable to resolve virtual method 11349: Landroid/view/ViewGroup;.onNestedScrollAccepted (Landroid/view/View;Landroid/view/View;I)V
02-24 09:38:49.785      997-997/com.example.supriyaraman.sample I/dalvikvm﹕ Could not find method android.view.ViewGroup.onStopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onStopNestedScroll
02-24 09:38:49.785      997-997/com.example.supriyaraman.sample W/dalvikvm﹕ VFY: unable to resolve virtual method 11355: Landroid/view/ViewGroup;.onStopNestedScroll (Landroid/view/View;)V
02-24 09:38:49.805      997-997/com.example.supriyaraman.sample I/dalvikvm﹕ Could not find method android.support.v7.internal.widget.ActionBarOverlayLayout.stopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.setHideOnContentScrollEnabled
02-24 09:38:49.805      997-997/com.example.supriyaraman.sample W/dalvikvm﹕ VFY: unable to resolve virtual method 9043: Landroid/support/v7/internal/widget/ActionBarOverlayLayout;.stopNestedScroll ()V
02-24 09:38:49.825      997-997/com.example.supriyaraman.sample I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
02-24 09:38:49.825      997-997/com.example.supriyaraman.sample W/dalvikvm﹕ VFY: unable to resolve virtual method 366: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
02-24 09:38:49.835      997-997/com.example.supriyaraman.sample I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
02-24 09:38:49.835      997-997/com.example.supriyaraman.sample W/dalvikvm﹕ VFY: unable to resolve virtual method 388: Landroid/content/res/TypedArray;.getType (I)I
02-24 09:38:50.015      997-997/com.example.supriyaraman.sample I/dalvikvm-heap﹕ Grow heap (frag case) to 4.427MB for 1127536-byte allocation
02-24 09:38:50.195      997-997/com.example.supriyaraman.sample I/dalvikvm-heap﹕ Grow heap (frag case) to 6.846MB for 2536936-byte allocation
02-24 09:38:55.756      997-997/com.example.supriyaraman.sample W/EGL_emulation﹕ eglSurfaceAttrib not implemented
02-24 09:38:56.015      997-997/com.example.supriyaraman.sample I/Choreographer﹕ Skipped 293 frames!  The application may be doing too much work on its main thread.
02-24 09:41:38.537      997-997/com.example.supriyaraman.sample W/EGL_emulation﹕ eglSurfaceAttrib not implemented

2 个答案:

答案 0 :(得分:0)

您使用不同的键来放置和获取您的值:

把:

b.putDouble("gyrerror", gyroerr);

得到:

double gyroerr =b.getDouble("gyroerr");

为了避免这种情况,我建议您为键定义静态值并使用它来放置/获取额外内容,因此拼写错误不会影响:

final static String GYRO_ERROR = "gyroerr";
...
b.putDouble(GYRO_ERROR, gyroerr);
...
double gyroerr =b.getDouble(GYRO_ERROR);

答案 1 :(得分:0)

把一切都放在你的意图中。像:

Intent i = new Intent(this, NextActivity.class);
i.put("gyrerror", gyroerr);
i.put("compasserror", comperr);
i.put("deviation", dev);

startActivity(i);

然后在下一堂课中你有:     Bundle b = this.getIntent()。getExtras();

这会从您的INTENT而非捆绑中获取额外内容。所以你需要说:

double d = b.getDouble("gyrerror"); 

这应该有效。