片段和活动之间未传递的值

时间:2013-12-10 04:04:58

标签: android android-intent android-fragments tabs android-activity

我有一个带有2个标签的标签式布局。每个选项卡都可以以双精度的形式收集一些数据,例如52.363。当在任何一个选项卡中按下按钮时,它们会打开一个新活动,显示在上一个选项卡中输入的数字。但是,当我按下提交按钮时,不会显示值。关于为什么的任何想法?

片段标签代码(两个标签现在都是相同的,但也省略了不言自明的部分):

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view;
        view = inflater.inflate(R.layout.advfrag_layout, container, false);
        return view;
    }

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    inSnow = (EditText) getView().findViewById(R.id.inchesSnow);
    calc = (Button) getView().findViewById(R.id.button);
    results = (TextView) getView().findViewById(R.id.resultView);
    calc.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    try{
                        inchSnow = Double.parseDouble(inSnow.getText().toString());
                    }catch (NumberFormatException ex){
                        results.setText("Field Blank!");

                    }
                Intent intent = new Intent(getActivity().getBaseContext(), ResultPost.class);
                intent.putExtra("inchSnow", inchSnow);
                getActivity().startActivity(intent);
        }
    });

}

将数据发送到的活动,ResultPost.Class(同样,只是必不可少的部分):

public class ResultPost extends ActionBarActivity {

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_result_post);
        AdvancedFragment advancedFragment= new AdvancedFragment();

        Intent intent = getIntent();
        double inchesn = intent.getDoubleExtra("inchSnow", 0);
    }
}

ResultPost.Class继续更改textview以显示此数据,但textview的值保持为“0”,这是默认值,即使我给它一个不同的值,如6。

1 个答案:

答案 0 :(得分:0)

试试这样:

public class ResultPost extends ActionBarActivity {

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_result_post);
        AdvancedFragment advancedFragment= new AdvancedFragment();

        Intent intent = getIntent();
        double inchesn = intent.getDoubleExtra("inchSnow",0.0);
    }
}