2013-03-19 135 views
0

我將下面的代碼添加到我的應用程序中,以添加視頻鏈接到我的原始文件夾中的視頻,但我得到的錯誤 VideoView說VideoView cannot be resolved or is not a field我已經包括了所有相關的imports.Is在我的語法中的某處出現錯誤?對於videoview設置視頻視圖

VideoView StudentLife = (VideoView) findViewById(R.id.VideoView); 

     Uri uri = Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.learningatgmi); 

     videoview.setVideoURI(uri); 
     videoview.start(); 

我的XML佈局如下:

<VideoView 
     android:id="@+id/videoView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/textView2" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="53dp" /> 
+0

運行時是編譯時錯誤嗎?你能顯示xml佈局文件嗎? – 2013-03-19 12:36:47

+0

這是一個編譯時錯誤,我只是添加了xml佈局。 – 2013-03-19 12:39:32

+0

檢查我的答案。 – 2013-03-19 12:43:37

回答

4

所以你的變量名是StudentLife

VideoView StudentLife = (VideoView) findViewById(R.id.VideoView); 

和你正在呼喚其他人OD對videoview這是不確定的變量..

所以下面的代碼:

 videoview.setVideoURI(uri); 
     videoview.start(); 

應該是:

 StudentLife.setVideoURI(uri); 
     StudentLife.start(); 

EDIT1:

按照你XML中的線來獲得實例的視頻視圖應該如下

VideoView StudentLife = (VideoView) findViewById(R.id.videoView1); 

完整的工作代碼應該如下:

VideoView StudentLife = (VideoView) findViewById(R.id.videoView1); 
Uri uri = Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.learningatgmi); 
StudentLife.setVideoURI(uri); 
StudentLife.start(); 

在側面沒有;你不應該使用類名作爲變量名......在java中,變量的第一個字符也不應該是大寫字母...因此,讓'videoView'作爲變量名。因此,下面應該是右邊的工作代碼變種名稱..

VideoView videoView = (VideoView) findViewById(R.id.videoView1); 
Uri uri = Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.learningatgmi); 
videoView.setVideoURI(uri); 
videoView.start(); 
+0

確實其實Myclass的名字是StudentLife.Is這仍然是正確的定義還是應該創建一個變量?謝謝 – 2013-03-19 12:45:56

+0

你應該創建一個其他名稱的變量..''viewView'可以代替'StudentLife'在代碼中,我張貼在答案.... – 2013-03-19 12:47:30

+0

非常感謝我剛剛意識到我正在做的錯誤R.id. – 2013-03-19 12:48:33

0

我的代碼工作::

mc = new MediaController(this); 
vd.setMediaController(mc); 
vd.setVideoURI(intentUri); 
vd.start(); 
setContentView(vd); 
1

您的VideoView的ID是videoView1。所以,你應該使用R.id.videoView1得到從視圖層次結構的VideoView對象的引用:

VideoView StudentLife = (VideoView) findViewById(R.id.videoView1); 
Uri uri = Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.learningatgmi); 
StudentLife.setVideoURI(uri); 
StudentLife.start(); 
0

其實當你使用這個代碼,

Uri uri = Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.learningatgmi); 
StudentLife.setVideoURI(uri); 

它通過URI爲空,因此它會顯示錯誤 所以而不是你不能使用下面的代碼。

StudentLife=(VideoView)findViewById(R.id.videoplayer); 
StudentLife.setVideoURI(Uri.parse("android.resource://" +getPackageName()+ "/" +R.raw.sample)); 
StudentLife.requestFocus(); 
StudentLife.start(); 

不是在設置之前解析URI,而是在設置過程中通過它會更好。

+0

如果'uri'爲null **如你所說**,那麼'Uri.parse()'返回null。在第二種情況下它也將是'StudentLife.setVideoURI(null)'。 – Nizam 2013-11-26 07:25:36

+0

我早些時候遇到同樣的問題,當我調試它時,我開始知道它返回null,然後我嘗試給你的代碼,它爲我工作。 – Rohit 2013-11-26 08:28:13