android传递参数到新活动是null

时间:2013-02-19 22:42:32

标签: android parameter-passing

我有一个搜索按钮。

当我点击它时,我想将当前活动的值发送到另一个Activity(在我的情况下是ProductActivity)

以下是搜索按钮的代码

 Button nextActivity = (Button) findViewById(R.id.btnSearch);
                nextActivity.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View view) {
                        TextView tvFileNumber = (TextView)findViewById(R.id.tv1);
                        Spinner spAttachement = (Spinner)findViewById(R.id.spinner1) ;
                        Spinner spExecution = (Spinner)findViewById(R.id.spinner2) ;
                        Spinner spCategory = (Spinner)findViewById(R.id.spinner3) ;
                        Spinner spOperation = (Spinner)findViewById(R.id.spinner4) ;
                        Spinner spCollection = (Spinner)findViewById(R.id.spinner5) ;

                        SpinnerData daAttachement =(SpinnerData)spAttachement.getItemAtPosition(spAttachement.getSelectedItemPosition());
                        SpinnerData daExecution =(SpinnerData)spAttachement.getItemAtPosition(spExecution.getSelectedItemPosition());
                        SpinnerData daCategory =(SpinnerData)spAttachement.getItemAtPosition(spCategory.getSelectedItemPosition());
                        SpinnerData daOperation =(SpinnerData)spAttachement.getItemAtPosition(spOperation.getSelectedItemPosition());
                        SpinnerData daCollection =(SpinnerData)spAttachement.getItemAtPosition(spCollection.getSelectedItemPosition());

                        Intent myIntent = new Intent(view.getContext(), ProductActivity.class);
                        //myIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        myIntent.putExtra("col_id",6);
                        myIntent.putExtra("site_id",site_id);
                        myIntent.putExtra("brand_id",brand_id);
                        myIntent.putExtra("attachement_id",daAttachement.getValue());
                        myIntent.putExtra("execution_id",daExecution.getValue());
                        myIntent.putExtra("operation_id",daOperation.getValue());
                        myIntent.putExtra("category_id",daCategory.getValue());
                        startActivity(myIntent);

                    }
                });

我的ProductActivity

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_product);
        Intent myIntent= getIntent(); 
        int brand_id = myIntent.getIntExtra("brand_id", 0); 
        int col_id = myIntent.getIntExtra("col_id", 0); 
        int site_id = myIntent.getIntExtra("site_id", 0); 
        int attachement_id = myIntent.getIntExtra("attachement_id", -1); 
        int execution_id = myIntent.getIntExtra("execution_id", -1); 
        int category_id = myIntent.getIntExtra("category_id", -1); 
        int operation_id = myIntent.getIntExtra("operation_id", -1); 
        int eol = myIntent.getIntExtra("eol", -1); 

        Log.v("TESTCOLLECTION",Integer.toString(col_id));

结果
日志结果显示TESTCOLLECTION:0

在我的例子中,它假设为6 myIntent.putExtra("col_id",6);

请帮忙。 我尝试了很多东西而且找不到原因。

非常感谢。

1 个答案:

答案 0 :(得分:0)

您的活动启动模式似乎是singleTask或类似,如果是这种情况,您必须覆盖onNewIntent(Intent intent)

这是因为onCreate()仅在创建活动实例时被调用一次,之后将调用onNewIntent()个词。