我试图扩展VideoControlsMobile课程,为其添加一些功能,但是当我尝试使用它时,我会获得NullPointerException
。
我使用此类的代码是:
MyVideoControls controls = new MyVideoControls(this);
emVideoView = (EMVideoView)findViewById(R.id.video_view);
emVideoView.setOnPreparedListener(this);
emVideoView.setControls(controls);
emVideoView.setVideoURI(Uri.parse(url));
我得到的错误是:
05-03 22:41:08.954 8385-8385/test.myapp.css.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: test.myapp.css.myapplication, PID: 8385
java.lang.RuntimeException: Unable to start activity ComponentInfo{test.myapp.css.myapplication/test.myapp.css.myapplication.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2198)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257)
at android.app.ActivityThread.access$800(ActivityThread.java:139)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5086)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.devbrackets.android.exomedia.ui.widget.VideoControls.setLoading(VideoControls.java:210)
at com.devbrackets.android.exomedia.ui.widget.VideoControls.restartLoading(VideoControls.java:245)
at com.devbrackets.android.exomedia.ui.widget.EMVideoView.setVideoURI(EMVideoView.java:379)
at test.myapp.css.myapplication.MainActivity.onCreate(MainActivity.java:38)
at android.app.Activity.performCreate(Activity.java:5248)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2162)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257)
at android.app.ActivityThread.access$800(ActivityThread.java:139)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5086)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
MainActivity中的第38行是:emVideoView.setVideoURI(Uri.parse(url));
和MyVideoControls
类代码是:
public class MyVideoControls extends VideoControlsMobile {
private static final String TAG = "MY-VIDEOCONTROLS";
private SeekBar seekBar;
private boolean pausedForSeek = false;
private boolean userInteracting = false;
private boolean isFullScreen;
private int originHeight;
private Activity activity;
private View rl_loading;
private OnFullScreenListener onFullScreenListener;
private View rl_top_control;
private View iv_play_back;
private View ib_switch_screen;
private int dp_55;
private int dp_70;
private View ll_bottom_control;
public MyVideoControls(Context context) {
super(context);
setup(context);
}
public MyVideoControls(Context context, AttributeSet attrs) {
this(context, attrs, 0);
setup(context);
}
public MyVideoControls(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
Log.i(TAG, "MyVideoControls 333");
setup(context);
activity = (Activity) context;
}
@Override
protected int getLayoutResource() {
return R.layout.video_controls;
}
/**
* Registers any internal listeners to perform the playback controls,
* such as play/pause, next, and previous
*/
@Override
protected void registerListeners() {
Log.i(TAG, "registerListeners ");
playPauseButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
onPlayPauseClick();
}
});
previousButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
onPreviousClick();
}
});
nextButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
onNextClick();
}
});
}
/**
* Updates the drawables used for the buttons to AppCompatTintDrawables
*/
@Override
protected void updateButtonDrawables() {
defaultPlayDrawable = EMResourceUtil.tintList(getContext(), R.drawable.exomedia_ic_play_arrow_white, R.color.exomedia_default_controls_button_selector);
defaultPauseDrawable = EMResourceUtil.tintList(getContext(), R.drawable.exomedia_ic_pause_white, R.color.exomedia_default_controls_button_selector);
playPauseButton.setImageDrawable(defaultPlayDrawable);
defaultPreviousDrawable = EMResourceUtil.tintList(getContext(), R.drawable.exomedia_ic_skip_previous_white, R.color.exomedia_default_controls_button_selector);
previousButton.setImageDrawable(defaultPreviousDrawable);
defaultNextDrawable = EMResourceUtil.tintList(getContext(), R.drawable.exomedia_ic_skip_next_white, R.color.exomedia_default_controls_button_selector);
nextButton.setImageDrawable(defaultNextDrawable);
}
/**
* Retrieves the view references from the xml layout
*/
@Override
protected void retrieveViews() {
rl_top_control = findViewById(R.id.rl_top_control);
iv_play_back = findViewById(R.id.iv_play_back);
iv_play_back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (isFullScreen) {
//quitFullScreen();
} else {
activity.finish();
//activity.overridePendingTransition(R.anim.slide_left_in, R.anim.slide_right_out);
}
}
});
ll_bottom_control = findViewById(R.id.ll_bottom_control);
currentTime = (TextView) findViewById(R.id.exomedia_controls_current_time);
endTime = (TextView) findViewById(R.id.exomedia_controls_end_time);
playPauseButton = (ImageButton) findViewById(R.id.exomedia_controls_play_pause_btn);
previousButton = (ImageButton) findViewById(R.id.exomedia_controls_previous_btn);
nextButton = (ImageButton) findViewById(R.id.exomedia_controls_next_btn);
loadingProgress = (ProgressBar) findViewById(R.id.exomedia_controls_video_loading);
controlsContainer = (ViewGroup) findViewById(R.id.exomedia_controls_interactive_container);
seekBar = (SeekBar) findViewById(R.id.exomedia_controls_video_seek);
rl_loading = findViewById(R.id.rl_loading);
ib_switch_screen = findViewById(R.id.ib_switch_screen);
ib_switch_screen.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (isFullScreen) {
//quitFullScreen();
} else {
//setFullScreen();
}
}
});
}
/**
* Performs any initialization steps such as retrieving views, registering listeners,
* and updating any drawables.
*
* @param context The context to use for retrieving the correct layout
*/
@Override
protected void setup(Context context) {
View.inflate(context, getLayoutResource(), this);
retrieveViews();
registerListeners();
updateButtonDrawables();
}
public interface OnFullScreenListener {
void onFullScreen(boolean isFullScreen);
}
}