YouTubePlayerSupportFragment null指针异常



之前我的代码正常工作,我正在使用 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 s,因此getSupportFragmentManager().findFragmentById()调用不会在您的布局中找到YouTubePlayerFragment,并且只会返回null。

相关内容

  • 没有找到相关文章

最新更新