0

我正在尝试在接近警报中接收阵列。我唯一可以发送的是一个 Long。这就是我设置接近警报的方式:

private void setProximityAlert(double lat, double lon, final long eventID, int requestCode)
{
    float radius = 30f;
    LocationManager locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    Intent intent = new Intent(PROXIMITY_INTENT_ACTION);
    intent.putExtra(ProximityAlert.EVENT_ID_INTENT_EXTRA, eventID);
    intent.putExtra("pole", mPositions);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), requestCode, intent, PendingIntent.FLAG_CANCEL_CURRENT);

    locManager.addProximityAlert(lat, lon, radius, -1, pendingIntent);
}

当我评论“intent.putExtra(“pole”,mPositions);”时它不会崩溃。

这是数组:

private ArrayList<LatLonPair> mPositions;

这是接收器:

public class ProximityAlert extends BroadcastReceiver {

public static final String EVENT_ID_INTENT_EXTRA = "EventIDIntentExtraKey";

@Override
public void onReceive(Context context, Intent intent) {
    long eventID = intent.getLongExtra(EVENT_ID_INTENT_EXTRA, -1);
    ArrayList<LatLonPair> list = (ArrayList<LatLonPair>) intent.getSerializableExtra("list");
    Toast.makeText(context, "Event: " + eventID, Toast.LENGTH_SHORT).show();
}
}

没有一个 putExtra 它正在工作,但我需要那个数组。所以这对我不起作用: Array List of objects via intent

LogCat 错误:

FATAL EXCEPTION: main
 java.lang.RuntimeException: Unable to start activity ComponentInfo{co.uk.gauntface.android.proximityalerts/co.uk.gauntface.android.proximityalerts.ProximityDisplay}: java.lang.RuntimeException: Parcel: unable to marshal value co.uk.gauntface.android.proximityalerts.models.LatLonPair@44fb61f0
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
    at android.app.ActivityThread.access$2300(ActivityThread.java:125)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:123)
    at android.app.ActivityThread.main(ActivityThread.java:4627)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:521)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
    at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.RuntimeException: Parcel: unable to marshal value co.uk.gauntface.android.proximityalerts.models.LatLonPair@44fb61f0
    at android.os.Parcel.writeValue(Parcel.java:1116)
    at android.os.Parcel.writeList(Parcel.java:510)
    at android.os.Parcel.writeValue(Parcel.java:1076)
    at android.os.Parcel.writeMapInternal(Parcel.java:479)
    at android.os.Bundle.writeToParcel(Bundle.java:1552)
    at android.os.Parcel.writeBundle(Parcel.java:493)
    at android.content.Intent.writeToParcel(Intent.java:5438)
    at android.app.ActivityManagerProxy.getIntentSender(ActivityManagerNative.java:2206)
    at android.app.PendingIntent.getBroadcast(PendingIntent.java:230)
    at co.uk.gauntface.android.proximityalerts.ProximityDisplay.setProximityAlert(ProximityDisplay.java:91)
    at co.uk.gauntface.android.proximityalerts.ProximityDisplay.registerIntents(ProximityDisplay.java:79)
    at co.uk.gauntface.android.proximityalerts.ProximityDisplay.onCreate(ProximityDisplay.java:43)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
    ... 11 more

这是 LatLonPair 类:

package co.uk.gauntface.android.proximityalerts.models;

import android.os.Parcel;
import android.os.Parcelable;

import com.google.android.maps.GeoPoint;

public class LatLonPair implements Parcelable{
private double mLatitude;
private double mLongitude;
private String popis;
private String nadpis;

public LatLonPair() {
    mLatitude = -2.60312596534349;
    mLongitude = 51.4558140934651;
    popis = "Nezadany popis";
    nadpis = "Nezadany nadpis";
}

public LatLonPair(double latitude, double longitude, String popis, String nadpis) {
    mLatitude = latitude;
    mLongitude = longitude;
    this.popis = popis;
    this.nadpis = nadpis;
}

public static boolean isValid(double lat, double lon) {

    if((lon >= -180 && lon <= 180) && (lat >= -90 && lat <= 90)) {
        return true;
    }

    return false;
}

public String getPopis() {
    return popis;
}

public String getNadpis() {
    return nadpis;
}

public void setLatitude(double latitude) {
    mLatitude = latitude;
}

public double getLatitude() {
    return mLatitude;
}

public void setLongitude(double longitude) {
    mLongitude = longitude;
}

public double getLongitude() {
    return mLongitude;
}

public GeoPoint getGeoPoint() {
    return new GeoPoint((int) (mLatitude * 1E6), (int) (mLongitude * 1E6));
}

@Override
public int describeContents() {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    // TODO Auto-generated method stub

}

}

4

2 回答 2

0

问题是我没有implements Parceable,之后就没有了CREATE。之后它没有崩溃并且数组长度还可以,但是项目具有默认值。我猜问题是在 Parceable 中保存变量的错误实现 - 它自行重置。所以我放弃了,只向广播接收器发送了我需要的几个字符串。谢谢大家的帮助。

于 2013-02-06T13:52:45.147 回答
0

尝试在类中调用super(dest,flags)被覆盖的writeToParcel方法LatLonPair

于 2013-02-05T11:44:12.443 回答