Double Value不会从一个活动传递到另一个活动



我正试图将3个计算值从活动1传递到活动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

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

看跌:

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

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

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

然后在你的下一节课上:捆绑包b=this.getIntent().getExtras();

这会从你的INTENT not捆绑包中获得额外的东西。所以你需要说:

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

这应该行得通。

最新更新