【问题标题】:non-static method getIntent() cannot be referenced from a static context不能从静态上下文引用非静态方法 getIntent()
【发布时间】:2015-11-13 14:03:13
【问题描述】:

我收到错误:

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

上线:

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

这是我的代码:

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

有什么问题?如何解决?

这是下一个代码的另一个错误错误:(120、21)错误:无法从静态上下文引用非静态方法runOnUiThread(Runnable)。我该如何纠正它。请帮忙

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();


 }

【问题讨论】:

标签: android


【解决方案1】:

有什么问题?

您正在通过Fragment 呼叫getIntent()

如何解决?

你必须在getIntent()之前调用getActivity()

例如:

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

【讨论】:

  • 它工作了......我还有另一个相同的错误:(120、21)错误:无法从静态上下文引用非静态方法runOnUiThread(Runnable)
  • 啊,我不明白你也打电话给getActivity(),它会起作用的
【解决方案2】:

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

【讨论】:

  • 请考虑编辑您的帖子,以添加更多关于您的代码的作用以及它为何能解决问题的说明。一个大部分只包含代码的答案(即使它正在工作)通常不会帮助 OP 理解他们的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-22
  • 2023-03-31
  • 2013-04-05
相关资源
最近更新 更多