[英]Android: How to start an activity from LazyAdapter in onClick
I have a LazyAdapter that extends from BaseAsapter, in which i have implemented a clickListener. 我有一个从BaseAsapter扩展的LazyAdapter,在其中我实现了clickListener。 If the user click on Facebook it should open the facebook and if the user clicks on Twitter it should take to the next activity. 如果用户单击Facebook,则应打开Facebook;如果用户单击Twitter,则应进行下一个活动。
My Code in LazyAdapter is 我在LazyAdapter中的代码是
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(item.equalsIgnoreCase(Constants.Facebook))
{
SocialActivity obj1=new SocialActivity();
obj1.startFB(context);
}
else if(item.equalsIgnoreCase(Constants.Twitter))
{
SocialActivity obj=new SocialActivity();
obj.startTwitter(context);
}
}
});
and this is the Code in SocialActivity
这是SocialActivity
的代码
Twitter Method Twitter方法
void startTwitter(Context con)
{
Intent intent = new Intent(con,TwitterInterface.class);
startActivity(intent);
}
Facebook Method Facebook方法
void startFb(Context con)
{
Intent intent = new Intent(con,FaceBook.class);
startActivity(intent);
}
Instead of this I have also tried this code in LazyAdapter
, but every time it gives me Null Pointer Exception
. 取而代之的是,我也在LazyAdapter
尝试了这段代码,但是每次它给我Null Pointer Exception
。 So any help to solve this problem will be highly appreciated and thanks in advance. 因此,对于解决此问题的任何帮助,我们将深表感谢,并在此先感谢您。
if(item.equalsIgnoreCase(Constants.Facebook))
{
SocialActivity obj1=new SocialActivity();
Intent in = new Intent(context,Facebook.class);
obj1.startActivity(in);
}
else if(item.equalsIgnoreCase(Constants.Twitter))
{
SocialActivity obj=new SocialActivity();
Intent intent = new Intent(context,TwitterInterface.class);
obj.startActivity(intent);
}
I have solved by using 我已经解决了
v.getContext().startActivity(intent)
Intent intent = new Intent(context,TwitterInterface.class);
v.getContext().startActivity(intent);
Try this it's more simple and a good practice of coding 试试看,它更简单,也是编码的好习惯
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(item.equalsIgnoreCase(Constants.Facebook))
{
SocialActivity obj1=new SocialActivity();
obj1.startFB(mContext);
}
else if(item.equalsIgnoreCase(Constants.Twitter))
{
SocialActivity obj=new SocialActivity();
obj.startTwitter(mContext);
}
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.