简体   繁体   中英

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. 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.

My Code in LazyAdapter is

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

Twitter Method

void startTwitter(Context con)
    {
        Intent intent = new Intent(con,TwitterInterface.class);
        startActivity(intent);
    }

Facebook Method

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 . 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);  
            }

        }
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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