gpt4 book ai didi

android - 使用 Intent 将值从适配器解析为 Activity

转载 作者:行者123 更新时间:2023-12-03 06:17:58 25 4
gpt4 key购买 nike

在遵循了一些教程之后,我制作了一个应用,可以在列表 View 中显示特定YouTube播放列表的YouTube视频。

如何解析对应的视频ID以使用VideoView在我的应用程序中播放视频

我的播放列表卡适配器

    //  click listener to play the video
holder.mThumbnailImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(view.getContext(), Activity2.class);
intent.putExtra("VidId", video.getId());
view.getContext().startActivity(intent);

}
});

我想在其中解析VideoId的第二个 Activity
 public class Activity2 extends YouTubeBaseActivity implements View.OnClickListener {
Button mbutton;
private YouTubePlayerView myouTubePlayerView;
private YouTubePlayer.OnInitializedListener monInitializedListener;

String VidKey = getIntent().getExtras().getString("VidId");//error here as per logcat

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.player);myouTubePlayerView = (YouTubePlayerView) findViewById(R.id.youtube_player_view);
monInitializedListener = new YouTubePlayer.OnInitializedListener() {

@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
youTubePlayer.loadVideo(VidKey);
}

@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {

}
};

mbutton = (Button) findViewById(R.id.button_play_youtube_video);
mbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
myouTubePlayerView.initialize(YOUTUBE_API_KEY, monInitializedListener);
}
});
}

我收到错误
  java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.paperwrrk.playlistyoutube/com.paperwrrk.playlistyoutube.Activity2}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Bundle android.content.Intent.getExtras()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2361)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2510)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5461)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:102)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Bundle android.content.Intent.getExtras()' on a null object reference
at com.paperwrrk.playlistyoutube.Activity2.<init>(Activity2.java:19)
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1068)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2351)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2510) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5461) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:102) 

最佳答案

要在您的应用程序中播放 YouTube视频,您已经集成

YouTube Android Player API

在您的项目中。

它将提供一个自定义 View ,您可以像这样将其添加到xml中。

<com.google.android.youtube.player.YouTubePlayerView
android:id="@+id/youtube_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="30dp" />

为了获得进一步的帮助,您可以遵循tutorial

关于android - 使用 Intent 将值从适配器解析为 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41165298/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com