Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/214.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在android中将对象发送到服务_Java_Android_Parcelable - Fatal编程技术网

Java 如何在android中将对象发送到服务

Java 如何在android中将对象发送到服务,java,android,parcelable,Java,Android,Parcelable,我想通过intent将YouTubeplyer对象发送到我的服务。下面是我如何将YouTubeplyer对象发送到我的服务的代码,但它给出了错误。我怎样才能解决它?请帮忙 java.lang.ClassCastException:com.pierfrancescosoffriti.androidyoutubeplayer.player.a不能强制转换为android.os.Parcelable MainActivity.java @Override protected void onCrea

我想通过intent将YouTubeplyer对象发送到我的服务。下面是我如何将YouTubeplyer对象发送到我的服务的代码,但它给出了错误。我怎样才能解决它?请帮忙

java.lang.ClassCastException:com.pierfrancescosoffriti.androidyoutubeplayer.player.a不能强制转换为android.os.Parcelable

MainActivity.java

  @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mYouTubePlayerView = findViewById(R.id.youtube_player_view);
    mYouTubePlayerView.initialize(new YouTubePlayerInitListener() {
        @Override
        public void onInitSuccess(@NonNull final YouTubePlayer youTubePlayer) {
             youTubePlayer.addListener(new AbstractYouTubePlayerListener() {
                 @Override
                 public void onReady() {
                     startService(youTubePlayer);
                 }
             });
        }
    },true);
}


private void startService(YouTubePlayer player)
{
    Intent intent = new Intent(MainActivity.this, ExampleService.class);
    intent.putExtra("player", (Parcelable) player);
    startService(intent);
}
服务等级

public class ExampleService extends Service {
YouTubePlayer mYouTubePlayer;

@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onStart(Intent intent, int startId) {
    super.onStart(intent, startId);

    mYouTubePlayer = intent.getParcelableExtra("player");
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    mYouTubePlayer.loadVideo("mHM5zyRZqpw",0);

    Intent main_intent = new Intent(this, MainActivity.class);
    PendingIntent pi = PendingIntent.getActivity(this,0,main_intent,0);

    Notification notification = new NotificationCompat.Builder(this, MainActivity.CHANNEL_ID)
            .setContentTitle("Example service")
            .setContentText("Content text")
            .setSmallIcon(R.drawable.ic_launcher_background)
            .addAction(R.drawable.ic_launcher_background, "Stop service", pi)
            .setContentIntent(pi)
            .build();

    startForeground(1, notification);


    return START_NOT_STICKY;
}

@Override
public void onDestroy() {
    super.onDestroy();
}

}

将您的类放入YouTubePlayer,请仅使用MainActivity.java类和Service.java类,YouTubePlayer是您的Tube API库