YouTubeプレーヤーがクラッシュし、その理由がわかりません。YouTube Playerがクラッシュする(null例外オブジェクト、ヌルオブジェクト参照)
これは私のJavaコードです:
public class YoutubeActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener, YouTubePlayer.OnInitializedListener {
private YouTubePlayerSupportFragment playerFragment;
private YouTubePlayer mPlayer;
private String YouTubeKey = "I DO NOT PUBLISH MY KEY";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_youtube);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
playerFragment = (YouTubePlayerSupportFragment) getSupportFragmentManager().findFragmentById(R.id.youtube_player_fragment);
playerFragment.initialize(YouTubeKey, this);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_photo) {
} else if (id == R.id.nav_gallery) {
} else if (id == R.id.nav_video) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
mPlayer = youTubePlayer;
//Enables automatic control of orientation
mPlayer.setFullscreenControlFlags(YouTubePlayer.FULLSCREEN_FLAG_CONTROL_ORIENTATION);
//Show full screen in landscape mode always
mPlayer.addFullscreenControlFlag(YouTubePlayer.FULLSCREEN_FLAG_ALWAYS_FULLSCREEN_IN_LANDSCAPE);
//System controls will appear automatically
mPlayer.addFullscreenControlFlag(YouTubePlayer.FULLSCREEN_FLAG_CONTROL_SYSTEM_UI);
if (!b) {
mPlayer.cueVideo("9rLZYyMbJic");
mPlayer.loadVideo("9rLZYyMbJic");
}
else
{
mPlayer.play();
}
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
mPlayer = null;
String ausgabe = youTubeInitializationResult.toString();
Log.d("hi", ausgabe);
} }
これは、クラッシュログです:
05 - 30 20: 14: 39.013 16232 - 16232/de.sammy.flugradarlive E/AndroidRuntime: FATAL EXCEPTION: main
Process: de.sammy.flugradarlive, PID: 16232
java.lang.RuntimeException: Unable to start activity ComponentInfo {
de.sammy.flugradarlive/de.sammy.flugradarlive.YoutubeActivity
}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.youtube.player.YouTubePlayerSupportFragment.initialize(java.lang.String, com.google.android.youtube.player.YouTubePlayer$OnInitializedListener)'
on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java: 2702)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java: 2767)
at android.app.ActivityThread.access$900(ActivityThread.java: 177)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java: 1449)
at android.os.Handler.dispatchMessage(Handler.java: 102)
at android.os.Looper.loop(Looper.java: 145)
at android.app.ActivityThread.main(ActivityThread.java: 5951)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java: 372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java: 1400)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java: 1195)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.youtube.player.YouTubePlayerSupportFragment.initialize(java.lang.String, com.google.android.youtube.player.YouTubePlayer$OnInitializedListener)'
on a null object reference
at de.sammy.flugradarlive.YoutubeActivity.onCreate(YoutubeActivity.java: 50)
at android.app.Activity.performCreate(Activity.java: 6289)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java: 1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java: 2655)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java: 2767)
at android.app.ActivityThread.access$900(ActivityThread.java: 177)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java: 1449)
at android.os.Handler.dispatchMessage(Handler.java: 102)
at android.os.Looper.loop(Looper.java: 145)
at android.app.ActivityThread.main(ActivityThread.java: 5951)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java: 372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java: 1400)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java: 1195)
05 - 30 20: 14: 41.055 16232 - 16232/de.sammy.flugradarlive I/Process: Sending signal.PID: 16232 SIG: 9
を役に立てば幸い? – RaceYouAnytime
私が持っているので、<フラグメント アンドロイド:ID = "@ + ID/youtube_player_fragment" アンドロイド:名= "com.google.android.youtube.player.YouTubePlayerFragment" アンドロイド:layout_width = "match_parent" アンドロイド:layout_height =」 wrap_content "/>私のcontent_youtube.xml –