Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Start an activity. Non-static method cannot be referenced from a static context

I'm trying to make an android app using gridview(http://www.rogcg.com/blog/2013/11/01/gridview-with-auto-resized-images-on-android) and mediaplayer(http://examples.javacodegeeks.com/android/android-mediaplayer-example/) trying to combine the two tutorials I found so it would be possible to select an image from GridView acitivity launching a MediaPlayer activity that would play a song.

GridView tutorial suggested this to enable the clicking:

    gridView.setOnItemClickListener(new AdapterView.OnItemClickListener()
    {
        public void onItemClick(AdapterView<?> parent, View v, int position, long id)
        {
            // this 'mActivity' parameter is Activity object, you can send the current activity.
            Intent i = new Intent(MainActivity.this, AndroidMediaPlayer.class);
            MainActivity.startActivity(i);
        }
    });

But I could not get it to work out and got warning:

Non-static method 'startActivity(android.content.Intent)' cannot be referenced from a static context
like image 259
Sthlm Avatar asked Dec 04 '25 14:12

Sthlm


1 Answers

v.getContext().startActivity(i);

Get context from your view before calling startActivity or use global context According to your tutorial you adapter class placed in MainActivity.class Thats why you also can use next type of calling

MainActivity.this.startActivity(i);
like image 135
once2go Avatar answered Dec 07 '25 02:12

once2go



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!