视频播放完毕后的对话框(OnCompletionListener + Dialog)

时间:2012-11-29 21:52:32

标签: android dialog onclicklistener

我正在尝试在视频完成后收到警告对话框。

这是我到目前为止的代码:

    public class VideoActivity extends Activity{
 private Context mContext; 

        public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.videoview);
          this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
          VideoView vv = (VideoView) findViewById(R.id.videoview);
          MediaController mc = new MediaController(this);

    public OnCompletionListener onCompletion(final String key){
            OnCompletionListener ocl = new OnCompletionListener(){  

    public void onCompletion(MediaPlayer mp) {      
    AlertDialog.Builder dialog = new AlertDialog.Builder(mContext); //Lag en dialog utifra kontekst
    dialog.setTitle("Hello"); //Hent ut tittelen til overlayItem
    dialog.setMessage("hello");  //Hent ut teksten til overlayItem
    dialog.setCancelable(false);

    dialog.setPositiveButton("Something", new OnClickListener() {           
        public void onClick(DialogInterface dialog, int id) {
            Intent intent = new Intent(mContext, VideoMenu.class);                      
            mContext.startActivity(intent);
        }
    });

    dialog.show(); //Vis dialogen
}

还有其他办法吗?我一直在谷歌搜索疯狂,但无法找到任何帮助。使用此代码,我得到一个空指针exeption。

谢谢! :)

11-29 23:12:54.215: E/AndroidRuntime(8445): FATAL EXCEPTION: main
11-29 23:12:54.215: E/AndroidRuntime(8445): java.lang.NullPointerException
11-29 23:12:54.215: E/AndroidRuntime(8445):     at android.app.AlertDialog.resolveDialogTheme(AlertDialog.java:142)
11-29 23:12:54.215: E/AndroidRuntime(8445):     at android.app.AlertDialog$Builder.<init>(AlertDialog.java:359)
11-29 23:12:54.215: E/AndroidRuntime(8445):     at com.example.norskattraksjon.VideoActivity$1.onCompletion(VideoActivity.java:97)
11-29 23:12:54.215: E/AndroidRuntime(8445):     at android.widget.VideoView$3.onCompletion(VideoView.java:345)
11-29 23:12:54.215: E/AndroidRuntime(8445):     at android.media.MediaPlayer$EventHandler.handleMessage(MediaPlayer.java:1773)
11-29 23:12:54.215: E/AndroidRuntime(8445):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-29 23:12:54.215: E/AndroidRuntime(8445):     at android.os.Looper.loop(Looper.java:137)
11-29 23:12:54.215: E/AndroidRuntime(8445):     at android.app.ActivityThread.main(ActivityThread.java:4517)
11-29 23:12:54.215: E/AndroidRuntime(8445):     at java.lang.reflect.Method.invokeNative(Native Method)
11-29 23:12:54.215: E/AndroidRuntime(8445):     at java.lang.reflect.Method.invoke(Method.java:511)
11-29 23:12:54.215: E/AndroidRuntime(8445):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
11-29 23:12:54.215: E/AndroidRuntime(8445):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
11-29 23:12:54.215: E/AndroidRuntime(8445):     at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

您已定义mContext但仍需要初始化它:

private Context mContext; // Defined

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mContext = this; // Initialized
    ...