2013-03-09 98 views
0

嗨我正在我的android應用程序中播放視頻,現在我想控制我的視頻播放全屏我有一個代碼,但它包含錯誤,它說,void是無效的變量類型onmeasure我不知道如何糾正錯誤覆蓋OnMeasure方法

public class video extends Activity { 


String SrcPath = "rtsp://v8.cache3.c.youtube.com/CjYLENy73wIaLQltaP8vg4qMsBMYDSANFEIJbXYtZ29vZ2xlSARSBXdhdGNoYOL6qv2DoMPrUAw=/0/0/0/video.3gp"; 

/** Called when the activity is first created. */ 

public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.video1); 


protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec){ 
    int parentWidth = MeasureSpec.getSize(widthMeasureSpec); 
    int parentHeight = MeasureSpec.getSize(heightMeasureSpec); 
    this.setMeasuredDimension(parentWidth/2, parentHeight); 
    this.setLayoutParams(new *ParentLayoutType*.LayoutParams(parentWidth/2,parentHeight)); 
    super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
} 


VideoView myVideoView = (VideoView)findViewById(R.id.vview); 
myVideoView.setVideoURI(Uri.parse(SrcPath)); 
myVideoView.setMediaController(new MediaController(this)); 
myVideoView.requestFocus(); 

} 
} 

感謝

回答

0

編譯的問題是,有支架的問題。移動

protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec){ 
    int parentWidth = MeasureSpec.getSize(widthMeasureSpec); 
    int parentHeight = MeasureSpec.getSize(heightMeasureSpec); 
    this.setMeasuredDimension(parentWidth/2, parentHeight); 
    this.setLayoutParams(new *ParentLayoutType*.LayoutParams(parentWidth/2,parentHeight)); 
    super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
} 

所以它onCreate()。目前,方法不能直接包含其他方法。

另外,onMeasure()不是在Activity中的方法,而是在View中。您應該查看文檔,以便確定您想要執行的操作。

+0

好,我把它拿走了斷的OnCreate並把它放在那的OnCreate之後,但在此仍收到錯誤新。 setlayoutparams ....我想我應該用我的佈局替換它.....這就是我做的,但它不能被解析爲變量, – user1794499 2013-03-09 03:50:48

+0

@ user1794499請記住,'onMeasure()'是一個View方法。你在「活動」中工作。至於佈局參數的設置,因爲這個方法是用於View類的,所以它使用'this'。你想要的是引用你的視圖,我認爲你想要的是你的VideoView。只需將參考變爲實例變量即可。 – 2013-03-09 04:06:39

+0

好了,但我的變量應該是什麼類型,int?對不起,打擾好友 – user1794499 2013-03-09 04:08:50

0

好像你可能希望有這兩個功能在你的類方法:

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.video1); 

    // Set up the video when you create the activity 
    VideoView myVideoView = (VideoView)findViewById(R.id.vview); 
    myVideoView.setVideoURI(Uri.parse(SrcPath)); 
    myVideoView.setMediaController(new MediaController(this)); 
    myVideoView.requestFocus(); 
} 

// On measure should be a different method also in the class video 
protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec) { 
    int parentWidth = MeasureSpec.getSize(widthMeasureSpec); 
    int parentHeight = MeasureSpec.getSize(heightMeasureSpec); 
    this.setMeasuredDimension(parentWidth/2, parentHeight); 
    this.setLayoutParams(new *ParentLayoutType*.LayoutParams(parentWidth/2,parentHeight)); 
    super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
} 
+0

你可以用.setlayoutparams中的新參數來確認我嗎?它應該是我的xml文件名嗎? – user1794499 2013-03-09 04:00:10

+0

我不確定,但可以通過查找xml找到。 video1.xml中的頂級視圖很可能會被稱爲____Layout。例如:它可能是LinearLayout,在這種情況下,我認爲它會使參數'new LinearLayout.LayoutParams(parentWidth/2,parentHeight)' – silleknarf 2013-03-12 20:42:28