有()之间的 Activityname.this任何区别
&放大器; 这
在Android的?
我试图打开从同一活动的一项活动,在对话框中的按钮?我的意图使用 getApplicationContext()
。在一些手机它的工作原理,但在另强制关闭?
在 ActivityName.this 和这我应该使用哪一个和放大器;为什么呢?
解决方案有Activityname.this()及任何区别;这在Android中?
这取决于你在哪里调用它。如果您是活动里面
,里面没有一个听众或内部类的像的onCreate
则没有。他们指的是活动方面
。
如果你说的内 onClickListener
则是肯定的。 这
指监听器
,你需要使用 ActivityName.this
或类似的东西。
someButton.setOnClickListener(新OnClickListener()
{
@覆盖
公共无效的onClick(视图v)
{
意图I =(v.getContext(),NextActivity.class);使用按钮上下文,这将是相同的活性上下文
startActivity(ⅰ);
}
});
这将是一样的使用对话框
或 AlertDialog
,你会想用 ActivityName.this
This一个答案,讨论上下文
取值但有之差一个更好的我会看看我能找到
A伟大的语境解释
修改为更完整
AFAIK, getApplicationContext()
或 ActivityName.this
是罚款敬酒
。该在文档使用例如 getApplicationContext()
。但是,吐司文档说:
参数 上下文的上下文中使用。通常您的应用程序或活动的对象。
因此,有可能出现这样的情况,其中一个是更好,但我一直使用活动上下文
,我想我会的,直到我对这个很纠正。
Is there any difference between Activityname.this()
& this
in Android?
I am trying to open an activity from same activity with button in dialog box? I am using getApplicationContext()
in intent. In some mobiles it works, but in others it force closes?
Between ActivityName.this and this which one I should use & why?
解决方案Is there any difference between Activityname.this() & this in Android ?
This depends on where you are calling it from. If you are inside the Activity
, not inside of a listener or inner class like in onCreate
then no. They both refer to the Activity context
.
If you are say inside of an onClickListener
then yes. this
refers to the listener
and you need to use ActivityName.this
or something like
someButton.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
Intent i = (v.getContext(), NextActivity.class); use the button context which will be the same as the activity context
startActivity(i);
}
});
This will be the same as when using a Dialog
or AlertDialog
, you will want to use ActivityName.this
This is an answer that talks about the difference of Context
s but there's a better one I will see if I can find
A great Context explanation
Edit for more completeness
AFAIK, getApplicationContext()
or ActivityName.this
is fine for Toasts
. The example in the docs uses getApplicationContext()
. But the Toast Docs says
Parameters context The context to use. Usually your Application or Activity object.
So there may be certain instances where one is better but I have always used Activity Context
and I guess I will until I am corrected on this.