如何将活动中的意图传递给扩展surfaceview的类?
发出意图的代码:
btnCutOutter.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
//Send intent to cut image from user selection path
//See designCanvas.java for details
String optionSelected = "Yes";
Intent myIntent = new Intent(view.getContext(), designCanvas.class);
myIntent.putExtra("id",optionSelected );
}
});
当我尝试检索意图数据时:
public class designCanvas extends SurfaceView implements SurfaceHolder.Callback {
//Create shape using Path
Path mPath = new Path();
private drawThread _thread;
private ArrayList<GraphicObject> _graphics = new ArrayList<GraphicObject>();
public designCanvas(Context context, AttributeSet attributes) {
super(context, attributes);
getHolder().addCallback(this);
setFocusable(true);
_thread = new drawThread(getHolder(), this);
String optionSelected = getIntent().getStringExtra("id");
. . .
eclipse告诉我:
The method getIntent() is undefined
如果有人能给我这样的指导,我将非常感激:)
非常感谢。
答案 0 :(得分:1)
android中的任何视图都包含在一个活动中,因此该活动具有对视图的引用(在本例中为SurfaceView)。所以它只是在Activity中获取intent数据然后使用setter来传递你想要传递给SurfaceView的数据
因此,在您的问题中保存SurfaceView调用getIntent的Activity中,然后在SurfaceView中调用一个setter,例如。 mySurfaceView.setSomeValue(someValue)
修改强>
你在designCanvas类中调用getIntent(),它需要在包含SurfaceView的Activity中调用
public SomeActivity extends Activity{
public void onCreate(Bundle b){
super.onCreate(b);
//give that the constructor for your surface view has an attributeset i assume
//you are creating it in xml
designCanvas dc = findViewById(someid);
String s = getIntent(.getStringExtra("id");
dc.setId(s);
}