简体   繁体   English

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

[英]Double Values are not passing from one activity to another

I am trying to pass 3 calculated values from Activity 1 to activity 2. Every time I see the result as 0.0 in Activity 2. Please help. 我试图将活动1中的3个计算值传递给活动2.每次我在活动2中看到结果为0.0时。请帮忙。

Activity 1: 活动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)
    {

    }
}

Activity 2: 活动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));
}}

Stack trace: 堆栈跟踪:

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

You are using different keys to put and to get your values: 您使用不同的键来放置和获取您的值:

Put: 放:

b.putDouble("gyrerror", gyroerr);

Get: 得到:

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

To avoid that, I recomend you to define static values for keys and use it to put/get extras, so typos won't affect: 为了避免这种情况,我建议您为键定义静态值并使用它来放置/获取额外内容,因此拼写错误不会影响:

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

Put everything in your intent. 把一切都放在你的意图中。 Like: 喜欢:

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

startActivity(i);

Then in your next class you have: Bundle b = this.getIntent().getExtras(); 然后在你的下一课中你有:Bundle b = this.getIntent()。getExtras();

This gets the extras from your INTENT not bundle. 这会从您的INTENT而不是捆绑中获取额外内容。 So you need to say: 所以你需要说:

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

This should work. 这应该工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM