在youtube api中,您将获得以下代码
@Override
public void onInitializationSuccess(Provider provider, YouTubePlayer player, boolean wasRestored) {
if (!wasRestored) {
player.cueVideo("C0DPdy98e4c"); // Plays https://www.youtube.com/watch?v=C0DPdy98e4c
}
}
由于我想做很多链接到youtube,我不想为每个youtube视频布局。我试图在Activity1中制作一个字符串,将youtube视频链接转移到Activity2。
Activity1发送到Activity2
private void button_les1Click()
{
startActivity(new Intent("company.app.activity2"));
}
public void onClick(View v) {
Intent videoId = new Intent(this, les_video.class);
videoId.putExtra("C0DPdy98e4c", videoId); //Input Youtube link here
switch (v.getId())
{
case R.id.button_les1:
button_les1Click();
break;
}
startActivity(videoId);
finish();
}
这是不工作的部分,它无法检测字符串值
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String videolink = getIntent().getExtras().getString("videoId");
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_activity2);
youTubeView = (YouTubePlayerView) findViewById(R.id.youtube_view);
youTubeView.initialize(Config.YOUTUBE_API_KEY, this);
}
@Override
public void onInitializationSuccess(Provider provider, YouTubePlayer player, boolean wasRestored) {
if (!wasRestored) {
player.loadVideo(videolink); // Play link from videolink string
}
}
你可能注意到我还在学习这种很棒的语言,因为我认为这只是一个问题,我现在不知道如何使用字符串。有人能指出绳子为什么不起作用吗?
您滥用putExtra.
putExtra
与键值对一起工作。键是一个字符串,用作值的标识符或索引。该值可以是String, int, boolean, etc.
密钥应该存储在XML中,或者作为常量存储在应用程序中的某个位置。
换句话说,它应该类似于:
public static final String KEY_VIDEO_LINK = "video_link";
//...
videoId.putExtra(KEY_VIDEO_LINK, "C0DPdy98e4c"); //assuming C0DPdy98e4c is the
//String that you want to send to the next Activity.
在Activity2中,使用常量作为密钥读取:
String videoLink = getIntent().getExtras().getString(Activity1.KEY_VIDEO_LINK);