简体   繁体   English

在Intent中传递对象

[英]Pass object in Intent

I would like pass object with Intent: 我想用Intent传递对象:

public class First extends Activity implements Serializable {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button here = (Button) findViewById(R.id.here);

        here.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
                Intent intent = new Intent(getApplicationContext(), MyClass.class);
                Test test = new Test();
                intent.putExtra("test", test);  
                startActivity(intent);
            }
        });
    }

} }

but this in line with intent.putExtra("test", test); 但这符合intent.putExtra(“ test”,test); return me error: 返回我错误:

The method putExtra(String, boolean) in the type Intent is not applicable for the arguments (String, Test) Intent类型的方法putExtra(String,boolean)不适用于参数(String,Test)

How can i make it? 我该怎么做?

Change the class header of Test to this 将Test的类头更改为此

public Test implements Parcelable ...

use the next examples and create the Test class to implemet the Parcelable correcly. 使用以下示例并创建Test类以正确实现Parcelable。 Parcelable Example Parcelable Android Developers SDK 可打包的示例可 打包的Android开发者SDK

and it should work., 它应该工作。,

ok new Solution change it to 好的新解决方案将其更改为

public Test implements Serializable ...

no need to implement anything 无需执行任何操作

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

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