tajchert / BusWear

EventBus for Android Wear devices.
Apache License 2.0
257 stars 23 forks source link

D/BusWearTag﹕ syncEvent error: <init> [class android.os.Parcel] #5

Closed trebormat closed 8 years ago

trebormat commented 9 years ago

I have an issue with a simple object Parcelable with just a bundle inside. I use https://github.com/dallasgutauckis/parcelabler I always the same error : D/BusWearTag﹕ syncEvent error: [class android.os.Parcel]

import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;
public class SimpleDataEvent implements Parcelable{

    private Bundle mBundle;

    public SimpleDataEvent() {
        mBundle = new Bundle();
    }

    public SimpleDataEvent(Bundle bundle) {
        this.mBundle = bundle;
    }

    ....

    public Bundle getBundle() {
        return mBundle;
    }

    protected SimpleDataEvent(Parcel in) {
        mBundle = in.readBundle();
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeBundle(mBundle);
    }

    @SuppressWarnings("unused")
    public static final Creator<SimpleDataEvent> CREATOR = new Creator<SimpleDataEvent>() {
        @Override
        public SimpleDataEvent createFromParcel(Parcel in) {
            return new SimpleDataEvent(in);
        }

        @Override
        public SimpleDataEvent[] newArray(int size) {
            return new SimpleDataEvent[size];
        }
    };
}

Could you help me to find the error. Because your library is very great for simple type. It works very fine, but I need a more complexe object. Thanks for your help.

Ch3D commented 9 years ago

@trebormat try to change SimpleDataEvent(Parcel in) from protected to public. That must help

powder366 commented 9 years ago

I have the same. From the Wear I issue:

EventBus.getDefault().postRemote(new StopRemoteEvent(settings), context);

Funny the message I get on Wear. This is not a local: D/Event﹕ No local subscribers registered for event class com.xyz.common.event.StartRemoteEvent

And on the Mobile: D/BusWearTag﹕ syncEvent error: [class android.os.Parcel]

Code Wear in ... WearableActivity:

  EventBus.getDefault().postRemote(new StartRemoteEvent(settings), context);

Code:

public class StopRemoteEvent extends GpsSettingsModelBaseEvent {
    public StopRemoteEvent(GpsSettingsModel settings) {
        super(settings);
    }
}

Code:

  public class GpsSettingsModelBaseEvent implements Parcelable {

  public GpsSettingsModel mData;

  public GpsSettingsModelBaseEvent(GpsSettingsModel data) {
      this.mData = data;
  }

  public GpsSettingsModelBaseEvent() {
 }

public GpsSettingsModel getData() {
    return mData;
}

public boolean hasData() {
    return mData != null;
}

@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeParcelable(this.mData, 0);
}

public GpsSettingsModelBaseEvent(Parcel in) {
    this.mData = in.readParcelable(GpsSettingsModel.class.getClassLoader());
}

public static final Creator<GpsSettingsModelBaseEvent> CREATOR = new  Creator<GpsSettingsModelBaseEvent>() {
    public GpsSettingsModelBaseEvent createFromParcel(Parcel source) {
        return new GpsSettingsModelBaseEvent(source);
    }

    public GpsSettingsModelBaseEvent[] newArray(int size) {
        return new GpsSettingsModelBaseEvent[size];
    }
};

Code:

public class GpsSettingsModel implements Parcelable {
public long delay;
public float distance;

public GpsSettingsModel() {
}

public GpsSettingsModel(long delay, float distance) {
    this.delay = delay;
    this.distance = distance;
}

@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeLong(this.delay);
    dest.writeFloat(this.distance);
}

protected GpsSettingsModel(Parcel in) {
    this.delay = in.readLong();
    this.distance = in.readFloat();
}

public static final Creator<GpsSettingsModel> CREATOR = new Creator<GpsSettingsModel>() {
    public GpsSettingsModel createFromParcel(Parcel source) {
        return new GpsSettingsModel(source);
    }

    public GpsSettingsModel[] newArray(int size) {
        return new GpsSettingsModel[size];
    }
};

@Override
public String toString() {
    return "GpsSettingsModel{" +
            ", delay=" + delay +
            ", distance=" + distance +
            '}';
}

}

Code Mobile in ... extends WearableListenerService:

public void onEvent(StopRemoteEvent event) {
    doSomething();
}
powder366 commented 9 years ago

Only simple Class with Parcelable possible. Due to implementation and the sending over the the Wear bus. Reflection does not find the Parcelable constructor otherwise. Also make sure your methods is public and not private or protected.