有人可以告诉我这个代码有什么问题我正在尝试从网址上显示视频。使用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)
答案 0 :(得分:0)
getApplicationContext()
为Context
使用Application
。当您使用片段时,您只需使用getActivity()
而不是getActivity().getApplicationContext()
。所以只需从
getActivity().getApplicationContext()
到
getActivity()
为您的上下文。