2014-06-23 67 views
0

我正在使用自定義videoView,並且無法啓動它。這是非常基本的,我可能只是缺少一些小東西。在此先感謝如何設置自定義videoView類

這裏是我的主類:

public class MainActivity extends Activity { 

myVideoView v; 

private static final String TAG = "MEDIA"; 
private TextView tv; 


MediaPlayer.OnCompletionListener videoListener = new MediaPlayer.OnCompletionListener() { 

    @Override 
    public void onCompletion(MediaPlayer mp) { 
     Log.d("onCompletion", "Total time paused: " + v.getTotalTimeMillis());   
    } 
}; 


@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    v = new myVideoView(this); 
    setContentView(R.layout.fragment_main); 

    String fileName = "android.resource://" + getPackageName() + "/" + R.raw.leftwrist;  
    v = (myVideoView) findViewById(R.id.myVideoView); 
    v.setVideoURI(Uri.parse(fileName)); 
    v.start(); 


    v.setOnCompletionListener(videoListener); 

} 

public void PlayPause(View view) { 
    //Do something in response to button press 
    if(v.isPlaying()){ 
     v.pause(); 
    } else { 
     v.start(); 
    } 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 

    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 
    if (id == R.id.action_settings) { 
     return true; 
    } 
    return super.onOptionsItemSelected(item); 
} 

/** 
* A placeholder fragment containing a simple view. 
*/ 
public static class PlaceholderFragment extends Fragment { 

    public PlaceholderFragment() { 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.fragment_main, container, false); 
     return rootView; 
    } 
}} 

這裏是我的自定義videoView類:

public class myVideoView extends VideoView{ 

long lastPausedTime = 0; // The time of the last pause (milliseconds) 
long totalPausedTime = 0; // The total time paused (milliseconds) 

public myVideoView(Context context) { 
    super(context); 
} 

@Override 
public void pause() { 
    lastPausedTime = System.currentTimeMillis(); 

    super.pause(); 
} 

@Override 
public void start() { 
    if (lastPausedTime != 0) { 
     totalPausedTime += System.currentTimeMillis() - lastPausedTime;   
    }  
    super.start(); 
} 

public long getTotalTimeMillis() { 
    return totalPausedTime; 
} 

} 

這是我的錯誤日誌:

06-23 10:30:33.620: E/AndroidRuntime(1183): FATAL EXCEPTION: main 
06-23 10:30:33.620: E/AndroidRuntime(1183): Process: com.example.watchvideo, PID: 1183 
06-23 10:30:33.620: E/AndroidRuntime(1183): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.watchvideo/com.example.watchvideo.MainActivity}: java.lang.ClassCastException: android.widget.VideoView cannot be cast to com.example.watchvideo.myVideoView 
06-23 10:30:33.620: E/AndroidRuntime(1183):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195) 
06-23 10:30:33.620: E/AndroidRuntime(1183):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 
06-23 10:30:33.620: E/AndroidRuntime(1183):  at android.app.ActivityThread.access$800(ActivityThread.java:135) 
06-23 10:30:33.620: E/AndroidRuntime(1183):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
06-23 10:30:33.620: E/AndroidRuntime(1183):  at android.os.Handler.dispatchMessage(Handler.java:102) 
06-23 10:30:33.620: E/AndroidRuntime(1183):  at android.os.Looper.loop(Looper.java:136) 
06-23 10:30:33.620: E/AndroidRuntime(1183):  at android.app.ActivityThread.main(ActivityThread.java:5017) 
06-23 10:30:33.620: E/AndroidRuntime(1183):  at java.lang.reflect.Method.invokeNative(Native Method) 
06-23 10:30:33.620: E/AndroidRuntime(1183):  at java.lang.reflect.Method.invoke(Method.java:515) 
06-23 10:30:33.620: E/AndroidRuntime(1183):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
06-23 10:30:33.620: E/AndroidRuntime(1183):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
06-23 10:30:33.620: E/AndroidRuntime(1183):  at dalvik.system.NativeStart.main(Native Method) 
06-23 10:30:33.620: E/AndroidRuntime(1183): Caused by: java.lang.ClassCastException: android.widget.VideoView cannot be cast to com.example.watchvideo.myVideoView 
06-23 10:30:33.620: E/AndroidRuntime(1183):  at com.example.watchvideo.MainActivity.onCreate(MainActivity.java:53) 
06-23 10:30:33.620: E/AndroidRuntime(1183):  at android.app.Activity.performCreate(Activity.java:5231) 
06-23 10:30:33.620: E/AndroidRuntime(1183):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 
06-23 10:30:33.620: E/AndroidRuntime(1183):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159) 
06-23 10:30:33.620: E/AndroidRuntime(1183):  ... 11 more 

編輯:新錯誤日誌

06-23 10:45:34.150: E/AndroidRuntime(1254): FATAL EXCEPTION: main 
06-23 10:45:34.150: E/AndroidRuntime(1254): Process: com.example.watchvideo, PID: 1254 
06-23 10:45:34.150: E/AndroidRuntime(1254): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.watchvideo/com.example.watchvideo.MainActivity}: android.view.InflateException: Binary XML file line #6: Error inflating class com.example.watchvideo.myVideoView 
06-23 10:45:34.150: E/AndroidRuntime(1254):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195) 
06-23 10:45:34.150: E/AndroidRuntime(1254):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 
06-23 10:45:34.150: E/AndroidRuntime(1254):  at android.app.ActivityThread.access$800(ActivityThread.java:135) 
06-23 10:45:34.150: E/AndroidRuntime(1254):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
06-23 10:45:34.150: E/AndroidRuntime(1254):  at android.os.Handler.dispatchMessage(Handler.java:102) 
06-23 10:45:34.150: E/AndroidRuntime(1254):  at android.os.Looper.loop(Looper.java:136) 
06-23 10:45:34.150: E/AndroidRuntime(1254):  at android.app.ActivityThread.main(ActivityThread.java:5017) 
06-23 10:45:34.150: E/AndroidRuntime(1254):  at java.lang.reflect.Method.invokeNative(Native Method) 
06-23 10:45:34.150: E/AndroidRuntime(1254):  at java.lang.reflect.Method.invoke(Method.java:515) 
06-23 10:45:34.150: E/AndroidRuntime(1254):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
06-23 10:45:34.150: E/AndroidRuntime(1254):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
06-23 10:45:34.150: E/AndroidRuntime(1254):  at dalvik.system.NativeStart.main(Native Method) 
06-23 10:45:34.150: E/AndroidRuntime(1254): Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class com.example.watchvideo.myVideoView 
06-23 10:45:34.150: E/AndroidRuntime(1254):  at android.view.LayoutInflater.createView(LayoutInflater.java:603) 
06-23 10:45:34.150: E/AndroidRuntime(1254):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696) 
06-23 10:45:34.150: E/AndroidRuntime(1254):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:755) 
06-23 10:45:34.150: E/AndroidRuntime(1254):  at android.view.LayoutInflater.inflate(LayoutInflater.java:492) 
06-23 10:45:34.150: E/AndroidRuntime(1254):  at android.view.LayoutInflater.inflate(LayoutInflater.java:397) 
06-23 10:45:34.150: E/AndroidRuntime(1254):  at android.view.LayoutInflater.inflate(LayoutInflater.java:353) 
06-23 10:45:34.150: E/AndroidRuntime(1254):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290) 
06-23 10:45:34.150: E/AndroidRuntime(1254):  at android.app.Activity.setContentView(Activity.java:1929) 
06-23 10:45:34.150: E/AndroidRuntime(1254):  at com.example.watchvideo.MainActivity.onCreate(MainActivity.java:50) 
06-23 10:45:34.150: E/AndroidRuntime(1254):  at android.app.Activity.performCreate(Activity.java:5231) 
06-23 10:45:34.150: E/AndroidRuntime(1254):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 
06-23 10:45:34.150: E/AndroidRuntime(1254):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159) 
06-23 10:45:34.150: E/AndroidRuntime(1254):  ... 11 more 
06-23 10:45:34.150: E/AndroidRuntime(1254): Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet] 
06-23 10:45:34.150: E/AndroidRuntime(1254):  at java.lang.Class.getConstructorOrMethod(Class.java:472) 
06-23 10:45:34.150: E/AndroidRuntime(1254):  at java.lang.Class.getConstructor(Class.java:446) 
06-23 10:45:34.150: E/AndroidRuntime(1254):  at android.view.LayoutInflater.createView(LayoutInflater.java:568) 
06-23 10:45:34.150: E/AndroidRuntime(1254):  ... 22 more 

編輯2:第三個錯誤日誌:

06-23 10:54:46.820: E/AndroidRuntime(1303): FATAL EXCEPTION: main 
06-23 10:54:46.820: E/AndroidRuntime(1303): Process: com.example.watchvideo, PID: 1303 
06-23 10:54:46.820: E/AndroidRuntime(1303): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.watchvideo/com.example.watchvideo.MainActivity}: android.view.InflateException: Binary XML file line #6: Error inflating class com.example.watchvideo.myVideoView 
06-23 10:54:46.820: E/AndroidRuntime(1303):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195) 
06-23 10:54:46.820: E/AndroidRuntime(1303):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 
06-23 10:54:46.820: E/AndroidRuntime(1303):  at android.app.ActivityThread.access$800(ActivityThread.java:135) 
06-23 10:54:46.820: E/AndroidRuntime(1303):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
06-23 10:54:46.820: E/AndroidRuntime(1303):  at android.os.Handler.dispatchMessage(Handler.java:102) 
06-23 10:54:46.820: E/AndroidRuntime(1303):  at android.os.Looper.loop(Looper.java:136) 
06-23 10:54:46.820: E/AndroidRuntime(1303):  at android.app.ActivityThread.main(ActivityThread.java:5017) 
06-23 10:54:46.820: E/AndroidRuntime(1303):  at java.lang.reflect.Method.invokeNative(Native Method) 
06-23 10:54:46.820: E/AndroidRuntime(1303):  at java.lang.reflect.Method.invoke(Method.java:515) 
06-23 10:54:46.820: E/AndroidRuntime(1303):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
06-23 10:54:46.820: E/AndroidRuntime(1303):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
06-23 10:54:46.820: E/AndroidRuntime(1303):  at dalvik.system.NativeStart.main(Native Method) 
06-23 10:54:46.820: E/AndroidRuntime(1303): Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class com.example.watchvideo.myVideoView 
06-23 10:54:46.820: E/AndroidRuntime(1303):  at android.view.LayoutInflater.createView(LayoutInflater.java:603) 
06-23 10:54:46.820: E/AndroidRuntime(1303):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696) 
06-23 10:54:46.820: E/AndroidRuntime(1303):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:755) 
06-23 10:54:46.820: E/AndroidRuntime(1303):  at android.view.LayoutInflater.inflate(LayoutInflater.java:492) 
06-23 10:54:46.820: E/AndroidRuntime(1303):  at android.view.LayoutInflater.inflate(LayoutInflater.java:397) 
06-23 10:54:46.820: E/AndroidRuntime(1303):  at android.view.LayoutInflater.inflate(LayoutInflater.java:353) 
06-23 10:54:46.820: E/AndroidRuntime(1303):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290) 
06-23 10:54:46.820: E/AndroidRuntime(1303):  at android.app.Activity.setContentView(Activity.java:1929) 
06-23 10:54:46.820: E/AndroidRuntime(1303):  at com.example.watchvideo.MainActivity.onCreate(MainActivity.java:50) 
06-23 10:54:46.820: E/AndroidRuntime(1303):  at android.app.Activity.performCreate(Activity.java:5231) 
06-23 10:54:46.820: E/AndroidRuntime(1303):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 
06-23 10:54:46.820: E/AndroidRuntime(1303):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159) 
06-23 10:54:46.820: E/AndroidRuntime(1303):  ... 11 more 
06-23 10:54:46.820: E/AndroidRuntime(1303): Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet] 
06-23 10:54:46.820: E/AndroidRuntime(1303):  at java.lang.Class.getConstructorOrMethod(Class.java:472) 
06-23 10:54:46.820: E/AndroidRuntime(1303):  at java.lang.Class.getConstructor(Class.java:446) 
06-23 10:54:46.820: E/AndroidRuntime(1303):  at android.view.LayoutInflater.createView(LayoutInflater.java:568) 
06-23 10:54:46.820: E/AndroidRuntime(1303):  ... 22 more 

新視頻類:

public class myVideoView extends VideoView{ 

long lastPausedTime = 0; // The time of the last pause (milliseconds) 
long totalPausedTime = 0; // The total time paused (milliseconds) 

public myVideoView(Context context, AttributeSet attrs, int defStyleAttr) { 
    super(context, attrs); 
} 

@Override 
public void pause() { 
    lastPausedTime = System.currentTimeMillis(); 

    super.pause(); 
} 

@Override 
public void start() { 
    if (lastPausedTime != 0) { 
     totalPausedTime += System.currentTimeMillis() - lastPausedTime;   
    }  
    super.start(); 
} 

public long getTotalTimeMillis() { 
    return totalPausedTime; 
} 

} 

新的主類(因爲休息第一部分是不變):

​​

回答

2

在你XML創建ViewView<VideoView>因此該類將是VideoView而不是myVideoView s o它將無法投射。 (如果你需要,read this

Caused by: java.lang.ClassCastException: android.widget.VideoView cannot be cast to com.example.watchvideo.myVideoView 

在你的XML,你應該用你的類創建View,所以這將是:

<com.example.watchvideo.myVideoView> 

而且不

<VideoView> 

實現所有構造函數

public myVideoView(Context context) { 
    super(context); 
} 

public myVideoView(Context context, AttributeSet attrs) { 
    super(context, attrs); 
} 

public myVideoView(Context context, AttributeSet attrs, int defStyleAttr) { 
    super(context, attrs, defStyleAttr); 
} 
+0

所以像這樣: dy19

+0

是的,試試看,你會看到該應用程序將工作。 –

+1

這次打開的佈局,但它沒有加載視頻,並給了我一個錯誤消息。我將新的錯誤日誌添加到編輯中 – dy19

0

用XML創建VideoView Widget。您應該在MainActivity.java文件中具有以下代碼。確保你改變了文件名。

videoView = (VideoView) findViewById(R.id.videoView); 
v = new myVideoView(this, videoView); 
v.setVideoPath(); 
v.startVideo(); 

簡單的myVideoView類的內容應該如下。

public class myVideoView { 
    private VideoView videoContainer; 
    private String fileName; 
    public myVideoView(Context context, VideoView v) { 
     videoContainer = (VideoView) v; 

     fileName = "android.resource://" + context.getPackageName() + "/" + R.raw.willferrell; 
    } 
    public void setVideoPath(String path) { 
     videoContainer.setVideoPath(path); 
    } 
    public void startVideo() { 
     videoContainer.start(); 
    } 
}