我正在尝试播放画廊中的视频。单击按钮时,它应该开始在 videoView 中播放视频。但是我得到了指定的孩子已经有父母的例外。以下是我的代码:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button bt= (Button) findViewById(R.id.button1);
final String filename = "/sdcard/myvideo.mp4";
Log.e("Video Error", "Problem Below");
final VideoView videoView1 = (VideoView)findViewById(R.id.videoview);
Log.e("Video Error", "Problem Above");
setContentView(videoView1);
videoView1.setVideoPath(filename);
// videoView1.setMediaController(MainActivity.this);
videoView1.requestFocus();
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//String fileName = "android.resource://" + getPackageName() + "/" + R.raw.sample;
Toast.makeText(getApplicationContext(), "Hey Baby", 5000);
//videoView1.start();
//Exit after saved
// finish();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
当我尝试初始化我的 VideoView 时,它给出了以下异常:
:12:11.838: E/AndroidRuntime(1124): FATAL EXCEPTION: main
03-12 19:12:11.838: E/AndroidRuntime(1124): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.et.getimages/com.et.getimages.MainActivity}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
请帮忙。