android 选项卡中 videoview 中的片段

android fragment in videoview in tab

谁能告诉我这段代码有什么问题我正在尝试显示来自 url 的视频。使用 Videoview 并且它在片段中。所以我不能延长活动..我猜错误来自我不确定是否只使用 getActivity()

ProgressDialog(getActivity().getApplicationContext());

我有错误 这是我的代码

package com.mall.first;
import com.actionbarsherlock.app.SherlockFragment;
import android.app.ProgressDialog;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.MediaController;
import android.widget.VideoView;

public class Now_Showing extends SherlockFragment {
    ProgressDialog pDialog;
    VideoView videoview;

    // Insert your Video URL
    String VideoURL = "http://10.0.2.2/stop.3gp";

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.nowshowing, container, false);
        final VideoView videoview =(VideoView) rootView.findViewById(R.id.videoView1);
        pDialog = new ProgressDialog(getActivity().getApplicationContext());
        // Set progressbar title
        pDialog.setTitle("Android Video Streaming Tutorial");
        // Set progressbar message
        pDialog.setMessage("Buffering...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        // Show progressbar
        pDialog.show();

        try {
            // Start the MediaController
            MediaController mediacontroller = new MediaController(getActivity().getApplicationContext());
            mediacontroller.setAnchorView(videoview);
            // Get the URL from String VideoURL
            Uri video = Uri.parse(VideoURL);
            videoview.setMediaController(mediacontroller);
            videoview.setVideoURI(video);

        } catch (Exception e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }

        videoview.requestFocus();
        videoview.setOnPreparedListener(new OnPreparedListener() {
            // Close the progress bar and play the video
            public void onPrepared(MediaPlayer mp) {
                pDialog.dismiss();
                videoview.start();
            }
        });
        return rootView;


}}

LOGCAT:

12-31 17:25:10.140: E/AndroidRuntime(5021): FATAL EXCEPTION: main
12-31 17:25:10.140: E/AndroidRuntime(5021): Process: com.mall.first, PID: 5021
12-31 17:25:10.140: E/AndroidRuntime(5021): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
12-31 17:25:10.140: E/AndroidRuntime(5021):     at android.view.ViewRootImpl.setView(ViewRootImpl.java:540)
12-31 17:25:10.140: E/AndroidRuntime(5021):     at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:259)
12-31 17:25:10.140: E/AndroidRuntime(5021):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
12-31 17:25:10.140: E/AndroidRuntime(5021):     at android.app.Dialog.show(Dialog.java:286)
12-31 17:25:10.140: E/AndroidRuntime(5021):     at com.mall.first.Now_Showing.onCreateView(Now_Showing.java:37)
12-31 17:25:10.140: E/AndroidRuntime(5021):     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
12-31 17:25:10.140: E/AndroidRuntime(5021):     at  android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)
12-31 17:25:10.140: E/AndroidRuntime(5021):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)

getApplicationContext() 使用 Context 作为你的 Application。当您使用片段时,您只需要使用 getActivity() 而不是 getActivity().getApplicationContext()。所以只需更改

 getActivity().getApplicationContext()

 getActivity()

适合您的上下文。