0

I'm getting error:

Error:(109, 18) error: non-static method getIntent() cannot be referenced from a static context

on the line:

init(getIntent().getSerializableExtra(Const.EXTRA_DATA)); 

Here is my code:

public static class Upper_fragment extends Fragment {

        private static final String TAG = "PlayActivity";

        private Video vid;
        int mSavedVideoPosition;
        protected VideoPlayerInterface vidp;
        private LocalSingleHttpServer mServer;


        // to be implemented in concrete activities
        public Cipher getCipher() throws GeneralSecurityException {
            final Cipher c = Cipher.getInstance("AES");    // NoSuchAlgorithmException, NoSuchPaddingException
            c.init(Cipher.DECRYPT_MODE, new SecretKeySpec("abcdef1234567890".getBytes(), "AES"));    // InvalidKeyException
            return c;
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View upperView = inflater.inflate(R.layout.upper_fragment, container, false);

            vidp = (VideoPlayerInterface) upperView.findViewById(R.id.vid);
            getRTSPUrl();

            init(getIntent().getSerializableExtra(Const.EXTRA_DATA));
            return upperView;
        }
}

What is the problem? How to fix it?

Here is another error on the next code Error:(120, 21) error: non-static method runOnUiThread(Runnable) cannot be referenced from a static context. how do i correct it. please help

public static class Upper_fragment extends Fragment {

        private static final String TAG = "PlayActivity";

        private Video vid;
        int mSavedVideoPosition;
        protected VideoPlayerInterface vidp;
        private LocalSingleHttpServer mServer;


        // to be implemented in concrete activities
        public Cipher getCipher() throws GeneralSecurityException {
            final Cipher c = Cipher.getInstance("AES");    // NoSuchAlgorithmException, NoSuchPaddingException
            c.init(Cipher.DECRYPT_MODE, new SecretKeySpec("abcdef1234567890".getBytes(), "AES"));    // InvalidKeyException
            return c;
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View upperView = inflater.inflate(R.layout.upper_fragment, container, false);

            vidp = (VideoPlayerInterface) upperView.findViewById(R.id.vid);
            getRTSPUrl();
            init(getActivity().getIntent().getSerializableExtra(Const.EXTRA_DATA));
            return upperView;
        }


        public void getRTSPUrl() {
            final ProgressDialog dia = ProgressDialog
                    .show(getActivity(), null, "Loading...");
            new Thread(new Runnable() {

                public void run() {
                    runOnUiThread(new Runnable() {

                        public void run() {
                            dia.dismiss();
                            try {

                                mServer = new LocalSingleHttpServer();
                                final Cipher c = getCipher();
                                if (c != null) {// null means a clear video ; no need to set a decryption processing
                                    mServer.setCipher(c);
                                }
                                mServer.start();
                                String path = getPath();
                                path = mServer.getURL(path);
                                vidp.setVideoPath(path);
                                vidp.play();


                            } catch (Exception e) {
                                startActivity(new Intent(getActivity(), MainActivity.class));
                            }
                        }
                    });

                }
            }).start();


 }
NaijaProgrammer
  • 2,892
  • 2
  • 24
  • 33
Alex
  • 63
  • 2
  • 9
  • Possible duplicate of [non static method cannot be referenced from a static context](http://stackoverflow.com/questions/2694566/non-static-method-cannot-be-referenced-from-a-static-context) – Ottavio Campana Nov 13 '15 at 15:10

2 Answers2

6

What is the problem?

You are calling getIntent() on a Fragment

How to fix it?

You have to call getActivity() before the getIntent()

For example :

init(getActivity().getIntent().getSerializableExtra(Const.EXTRA_DATA));
Skizo-ozᴉʞS ツ
  • 19,464
  • 18
  • 81
  • 148
  • it worked ... I also have another one of the same Error:(120, 21) error: non-static method runOnUiThread(Runnable) cannot be referenced from a static context – Alex Nov 13 '15 at 14:33
  • Ahh I didn't understand you call `getActivity()` also after and it will work – Skizo-ozᴉʞS ツ Nov 13 '15 at 14:43
-1

remvoe static ;

public class Upper_fragment extends Fragment {

        private static final String TAG = "PlayActivity";

        private Video vid;
        int mSavedVideoPosition;
        protected VideoPlayerInterface vidp;
        private LocalSingleHttpServer mServer;


        // to be implemented in concrete activities
        public Cipher getCipher() throws GeneralSecurityException {
            final Cipher c = Cipher.getInstance("AES");    // NoSuchAlgorithmException, NoSuchPaddingException
            c.init(Cipher.DECRYPT_MODE, new SecretKeySpec("abcdef1234567890".getBytes(), "AES"));    // InvalidKeyException
            return c;
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View upperView = inflater.inflate(R.layout.upper_fragment, container, false);

            vidp = (VideoPlayerInterface) upperView.findViewById(R.id.vid);
            getRTSPUrl();

            init(getIntent().getSerializableExtra(Const.EXTRA_DATA));
            return upperView;
        }
tiny sunlight
  • 6,231
  • 3
  • 21
  • 42
  • Please consider editing your post to add more explanation about what your code does and why it will solve the problem. An answer that mostly just contains code (even if it's working) usually wont help the OP to understand their problem. – SuperBiasedMan Nov 13 '15 at 16:36