gpt4 book ai didi

android - YouTubePlayerSupportFragment 空指针异常

转载 作者:行者123 更新时间:2023-11-29 17:08:57 25 4
gpt4 key购买 nike

之前我的代码运行良好,我使用 YoutubeBaseActivity 扩展我的 java 类,现在由于某些原因我使用 AppCompatActivity 并获得 NPE。

这是我得到 NPE 的行:

youTubePlayerSupportFragment.initialize(ApiKey.YOUTUBE_API_KEY, this);

YouTubeViewActivity.java这里是完整的代码

public class YouTubeViewActivity extends AppCompatActivity implements
YouTubePlayer.OnInitializedListener {

private static final int RECOVERY_DIALOG_REQUEST = 1;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.youtube_view_activity);

YouTubePlayerSupportFragment youTubePlayerSupportFragment =
(YouTubePlayerSupportFragment) getSupportFragmentManager().findFragmentById(R.id.youtube_view);

// at below line I am getting NPE
youTubePlayerSupportFragment.initialize(ApiKey.YOUTUBE_API_KEY, this);

}

@Override
public void onInitializationFailure(YouTubePlayer.Provider provider,
YouTubeInitializationResult errorReason) {
if (errorReason.isUserRecoverableError()) {
errorReason.getErrorDialog(this, RECOVERY_DIALOG_REQUEST).show();
} else {
String errorMessage = String.format(
getString(R.string.error_player), errorReason.toString());
Toast.makeText(this, errorMessage, Toast.LENGTH_LONG).show();
}
}

@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider,
YouTubePlayer player, boolean wasRestored) {
if (!wasRestored) {

player.loadVideo("pRpeEdMmmQ0"); // cue, load

}
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == RECOVERY_DIALOG_REQUEST) {
// Retry initialization if user performed a recovery action
getYouTubePlayerProvider().initialize(ApiKey.YOUTUBE_API_KEY, this);
}
}

private YouTubePlayer.Provider getYouTubePlayerProvider() {
return (YouTubePlayerView) findViewById(R.id.youtube_view);
}

}

youtube_view_activity.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<fragment
android:name="com.google.android.youtube.player.YouTubePlayerFragment"
android:id="@+id/youtube_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

</LinearLayout>

查看日志:

FATAL EXCEPTION: main
Process: com.akoscz.youtube, PID: 13033
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.akoscz.youtube/com.akoscz.youtube.YouTubeViewActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2338)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5299)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:829)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.akoscz.youtube.YouTubeViewActivity.onCreate(YouTubeViewActivity.java:28)
at android.app.Activity.performCreate(Activity.java:5264)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2302)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390) 
at android.app.ActivityThread.access$800(ActivityThread.java:151) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321) 
at android.os.Handler.dispatchMessage(Handler.java:110) 
at android.os.Looper.loop(Looper.java:193) 
at android.app.ActivityThread.main(ActivityThread.java:5299) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:515) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:829) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645) 
at dalvik.system.NativeStart.main(Native Method) 

最佳答案

您的布局中有错误的 Fragment 类。它应该是 com.google.android.youtube.player.YouTubePlayerSupportFragment

支持 FragmentManager 只处理支持的 Fragment,所以 getSupportFragmentManager().findFragmentById() 调用不会找到 >YouTubePlayerFragment 在您的布局中,并且只会返回 null。

关于android - YouTubePlayerSupportFragment 空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41055979/

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