从自定义对话框调用活动
我想这只是一个简单的问题(我真是个菜鸟......) 我有一个自定义对话框,其中有 3 个按钮。
现在我想从其中一个按钮调用一项活动,所以 我尝试了这个:
public class picturedialog extends Dialog implements OnClickListener {
Button Camera;
public picturedialog (Context context){
super (context);
setContentView(R.layout.picturedialog);
Camera = (Button) this.findViewById(R.id.pdButton1);
Camera.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
dismiss();
Intent myIntent = new Intent(view.getContext(), CameraActivity.class);
startActivity(myIntent);
}
});
...
}
然后红色的波浪线出现在startActivity(myIntent)上。 将鼠标悬停在其上时,Eclipse 告诉我:“对于 new View.OnClickListener(){} 类型,未定义方法 startActivity(Intent)” 呃?请指导我如何正确执行此操作。 任何帮助将不胜感激。
I guess this is just a simple question (I’m such a noob…)
I have this custom dialog box that has 3 buttons in it.
Now I want to call an activity from one of the buttons so
I tried this:
public class picturedialog extends Dialog implements OnClickListener {
Button Camera;
public picturedialog (Context context){
super (context);
setContentView(R.layout.picturedialog);
Camera = (Button) this.findViewById(R.id.pdButton1);
Camera.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
dismiss();
Intent myIntent = new Intent(view.getContext(), CameraActivity.class);
startActivity(myIntent);
}
});
...
}
Then the red squiggly line appears on startActivity(myIntent).
Upon hovering on it, eclipse tells me this: “The method startActivity(Intent) is undefined for the type new View.OnClickListener(){}”
Ehhh? Please orient me on how to do this properly.
Any help would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设您的 Activity 的名称是 A,那么您只需这样做:
出现问题是因为您的内部类中的“this”引用了该内部类的对象,而您想要的是封闭 Activity 的对象。 A.this 指的是 that。
如果您没有将此类包含在 Activity 中,请尝试使用传递到方法中的上下文从方法调用 startActivity,例如 context.startActivty(myIntent)。
startActivity 方法属于 Context 类。
Suppose the name of your Activity is A, then you just do:
The problem arises because "this" inside your inner class refers to the object of that inner class, when what you want is the object of the enclosing Activity. A.this will refer to that.
If you aren't enclosing this class in an Activity, then try calling the startActivity from method using the context that you passed into the method, e.g. context.startActivty(myIntent).
The startActivity method belongs to the Context class.
我确信您对
Dialog
的子类化过于复杂了。尝试按照对话框教程进行操作 - https://developer.android。 com/guide/topics/ui/dialogs.html#ShowingADialog请注意,对话框是动态创建的(在
Activity.onCreateDialog()
中),无需拥有自己的自定义对话框类。由于您在包装活动内部设置了一个侦听器(如果您按照教程进行操作),那么您就可以调用startActivity(myIntent)
,因为包装类实例的所有字段/方法都可用对于内部类的实例。I am sure you are overcomplicating with subclassing from
Dialog
. Try to follow to the dialog tutorial - https://developer.android.com/guide/topics/ui/dialogs.html#ShowingADialogNote that dialogs are created on the fly (in
Activity.onCreateDialog()
) without the need to have your own custom dialog classes. Since you set a listener being inside of the wrapping activity (if you follow the tutorial), then you are able to callstartActivity(myIntent)
, because all the fields/methods of a wrapping class instance are available for an instance of an inner class.