0

视频播放完毕后,我正试图获得一个警告对话框。

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

    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
}

还有另一种方法可以做到这一点吗?我一直在疯狂地搜索,但找不到任何帮助。使用此代码,我得到一个空指针异常。

谢谢!:)

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)
4

1 回答 1

0

您已定义mContext但仍需要对其进行初始化:

private Context mContext; // Defined

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mContext = this; // Initialized
    ...
于 2012-11-29T22:34:03.573 回答