2015-11-13 3 views
-1

Я получаю сообщение об ошибке:нестатический метод getIntent() не может ссылаться из статического контекста

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) error: нестатический метод 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(); 


} 
+0

Возможный дубликат [нестационарного метода нельзя ссылаться из статического контекста] (http://stackoverflow.com/questions/2694566/non-static-method-cannot-be-referenced-from-a-static-context) –

ответ

6

What is the problem?

Вы звоните getIntent() на Fragment

How to fix it?

Вы должны позвонить getActivity() перед getIntent()

Например:

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

Спасибо, что решил – Alex

+0

Это сработало ... У меня также есть еще одна ошибка: (120, 21): нестатический метод runOnUiThread (Runnable) нельзя ссылаться из статического контекста – Alex

+0

Ahh Я не понял, что вы называете 'getActivity()' также после, и он будет работать –

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

Пожалуйста, рассмотрите возможность редактирования сообщения, чтобы добавить больше объяснений о том, что делает ваш код, и почему он решит проблему. Ответ, который в основном содержит только код (даже если он работает), обычно не помогает OP понять их проблему. – SuperBiasedMan

+0

Спасибо за руководство –

Смежные вопросы