在包含地图的片段中恢复可分割的对象Array列表

时间:2015-05-07 07:48:30

标签: java android google-maps android-fragments

我有一个包含地图的片段。片段使用对象的arraylist来获取它从父节点接收的标记的位置和描述。问题是,即使我将 onSaveInstanceState 中的arraylist保存在一个包上,并在 onCreate 中再次检索它,也会弹出以下错误

05-07 15:39:55.453: E/Parcel(24252): Class not found when unmarshalling: com.ictcsu.DatabaseClasses.sentdata, e: java.lang.ClassNotFoundException: com.ictcsu.DatabaseClasses.sentdata
05-07 15:39:55.453: D/AndroidRuntime(24252): Shutting down VM
05-07 15:39:55.453: W/dalvikvm(24252): threadid=1: thread exiting with uncaught exception (group=0x40fe72a0)
05-07 15:39:55.473: E/AndroidRuntime(24252): FATAL EXCEPTION: main
05-07 15:39:55.473: E/AndroidRuntime(24252): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ictcsu.testapp1/com.ictcsu.viewdata.Activity_Viewdata}: android.os.BadParcelableException: ClassNotFoundException when unmarshalling: com.ictcsu.DatabaseClasses.sentdata
05-07 15:39:55.473: E/AndroidRuntime(24252):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2070)
05-07 15:39:55.473: E/AndroidRuntime(24252):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2095)
05-07 15:39:55.473: E/AndroidRuntime(24252):    at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3568)
05-07 15:39:55.473: E/AndroidRuntime(24252):    at android.app.ActivityThread.access$700(ActivityThread.java:134)
05-07 15:39:55.473: E/AndroidRuntime(24252):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1209)
05-07 15:39:55.473: E/AndroidRuntime(24252):    at android.os.Handler.dispatchMessage(Handler.java:99)
05-07 15:39:55.473: E/AndroidRuntime(24252):    at android.os.Looper.loop(Looper.java:137)
05-07 15:39:55.473: E/AndroidRuntime(24252):    at android.app.ActivityThread.main(ActivityThread.java:4830)
05-07 15:39:55.473: E/AndroidRuntime(24252):    at java.lang.reflect.Method.invokeNative(Native Method)
05-07 15:39:55.473: E/AndroidRuntime(24252):    at java.lang.reflect.Method.invoke(Method.java:511)
05-07 15:39:55.473: E/AndroidRuntime(24252):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
05-07 15:39:55.473: E/AndroidRuntime(24252):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
05-07 15:39:55.473: E/AndroidRuntime(24252):    at dalvik.system.NativeStart.main(Native Method)
05-07 15:39:55.473: E/AndroidRuntime(24252): Caused by: android.os.BadParcelableException: ClassNotFoundException when unmarshalling: com.ictcsu.DatabaseClasses.sentdata

它说我的班级即使确实存在也找不到!这是我的片段代码

public class Fragment_map extends Fragment{
private static final String TAG = Fragment_map.class.getSimpleName();
private static final String MYLIST = "myList";
List<Marker> markers = new ArrayList<Marker>();
MapView mMapView;
private static GoogleMap googleMap;
private selector selectorListener;
private static ArrayList<sentdata> data = new ArrayList<sentdata>();
private Boolean mInitialCreate;


public static Fragment_map newInstance(ArrayList<sentdata> _data){
    Log.i(TAG, "List Size:" + _data.size());
    Fragment_map newfragment_map = new Fragment_map();
    Bundle args = newfragment_map.getArguments();
    if(args==null){
        args = new Bundle();
    }
    args.putParcelableArrayList(MYLIST, _data);
    newfragment_map.setArguments(args);
    return newfragment_map;

}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    Fragment parentFragment = getParentFragment();
    if (parentFragment != null && parentFragment instanceof selector) {
        selectorListener = (selector) parentFragment;
    }

    else if (activity != null && activity instanceof selector) {
        selectorListener = (selector) activity;
    }

    else if (selectorListener == null) {
        Log.w(TAG, "onAttach: niether the parent fragment or parent activity implement OnImageSelectedListener, "
                + "image selections will not be communicated to other components");
    }

}



@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //super.onCreate(savedInstanceState);
    Log.w(TAG, "here in OCREATE Of map");
    Bundle args = getArguments();
    if (args != null) {
        data = args.getParcelableArrayList(MYLIST);

    }
    if (savedInstanceState != null) {

        data = savedInstanceState.getParcelableArrayList(MYLIST);
        if(data.size()!=0){
            for(int i=0; i< data.size(); i++){
                Log.w(TAG,data.get(i).gettmp());
            }

        }
        mInitialCreate = false;

    }else {
        mInitialCreate = true;
    }
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_map, container, false);
    mMapView = (MapView) rootView.findViewById(R.id.map);
    mMapView.onCreate(savedInstanceState);

    mMapView.onResume();// needed to get the map to display immediately

    try {
        MapsInitializer.initialize(getActivity().getApplicationContext());
    } catch (Exception e) {
        e.printStackTrace();
    }
    googleMap = mMapView.getMap();
    googleMap.setOnMarkerClickListener(new OnMarkerClickListener()
    {

        @Override
        public boolean onMarkerClick(Marker arg0) {
            for(int i=0; i< markers.size();i++){
                if(arg0.equals(markers.get(i))){ // if marker source is clicked
                     Log.w(TAG,"Clicked");
                     selectorListener.setlist(i);

                }
                // display toast;
            }
            return true;
        }



    }); 

    if(data.size()!=0 && data !=null){
        setmap();
    }
    if (mInitialCreate) {
        mInitialCreate = false;
    }

    return rootView;   
}




public void setmap(){
    for(int i=0; i<data.size();i++){
        double lat = Double.parseDouble(data.get(i).getlat());
        double longt = Double.parseDouble(data.get(i).getlong());

        Marker marker = googleMap.addMarker(new MarkerOptions()
            .title("Hello Maps")
            .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ROSE))
            .position(new LatLng(lat, longt)));



        // Changing marker icon

       // marker.showInfoWindow();
        // adding marker
        markers.add(marker);


    }
     markers.get(0).showInfoWindow();
     CameraPosition cameraPosition = new CameraPosition.Builder()
        .target(new LatLng(Double.parseDouble(data.get(0).getlat()), Double.parseDouble(data.get(0).getlong()))).zoom(75).build();
     googleMap.animateCamera(CameraUpdateFactory
                .newCameraPosition(cameraPosition));
}

public void setview(double lat, double longt, int num){
     //MarkerOptions marker = new MarkerOptions().position(
      //          new LatLng(lat, longt)).title("Hello Maps");

        // Changing marker icon
        //marker.icon(BitmapDescriptorFactory
        //        .defaultMarker(BitmapDescriptorFactory.HUE_ROSE));

        // adding marker
        //googleMap.addMarker(marker);
    Log.i(TAG, "___Check data:"+ num);
    for(int i=0;i<markers.size();i++){
        markers.get(i).hideInfoWindow();
    }

        markers.get(num).showInfoWindow();


        CameraPosition cameraPosition = new CameraPosition.Builder()
                .target(new LatLng(Double.parseDouble(data.get(num).getlat()),  Double.parseDouble(data.get(num).getlong()))).zoom(90).build();
        googleMap.animateCamera(CameraUpdateFactory
                .newCameraPosition(cameraPosition));
}


@Override
public void onResume() {
    super.onResume();
    mMapView.onResume();
}

@Override
public void onPause() {
    super.onPause();
    mMapView.onPause();
}

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

@Override
public void onLowMemory() {
    super.onLowMemory();
    mMapView.onLowMemory();
}


@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    if (savedInstanceState != null) {
        data = savedInstanceState.getParcelableArrayList(MYLIST);
        mInitialCreate = false;
    }
    Log.v(TAG, "onActivityCreated");
}

@Override
public void onViewStateRestored(Bundle savedInstanceState) {
    super.onViewStateRestored(savedInstanceState);
    Log.v(TAG, "onViewStateRestored");
}

@Override
public void onStart() {
    super.onStart();
    Log.v(TAG, "onStart");
}



@Override
public void onStop() {
    super.onStop();
    Log.v(TAG, "onStop");
}

@Override
public void onDestroyView() {
    super.onDestroyView();
    Log.v(TAG, "onDestroyView");
}




@Override
public void onSaveInstanceState(Bundle outState) {

    Log.v(TAG, "onSaveInstanceState");
    outState.putParcelableArrayList(MYLIST, data);
    super.onSaveInstanceState(outState);
}

}

我甚至尝试在 onActivityCreated 中检索该捆绑包但仍然无效。我试图在我的Logcat中显示保存在OnCreate上的 savedInstanceState 中的arraylist,但它根本不是空的。我检查了我的课程并搜索了一些错误,但似乎没问题。

public class sentdata implements Parcelable{

private int _id;
private String _data="";
private String _lat="";
private String _long="";
private String _img="";
private String _imgP="";
private int _sntvia;
private String _snttime="";
private String _template="";


public sentdata(){}
public sentdata( String data, String lat, String longt, String img, String imgp, int sntvia, String sntime, String template  ){
    this._data = data;
    this._lat = lat;
    this._long = longt;
    this._img = img;
    this._imgP = imgp;
    this._sntvia = sntvia;
    this._snttime = sntime;
    this._template = template;
}

public sentdata( int id, String data, String lat, String longt, String img, String imgp, int sntvia, String sntime, String template  ){
    this._id = id;
    this._data = data;
    this._lat = lat;
    this._long = longt;
    this._img = img;
    this._imgP = imgp;
    this._sntvia = sntvia;
    this._snttime = sntime;
    this._template = template;
}


public int getid(){
    return this._id;
}


public void setid(int id){
     this._id = id;
}
public void setdata(String data){
    this._data = data;
}

public String getdata(){
    return this._data;
}

public void setlat(String lat){
    this._lat = lat;
}

public String getlat(){
    return this._lat;
}

public void setlong(String longt){
    this._long = longt;

}

public String getlong(){
    return this._long;
}

public void setimg(String img){
    this._img = img;

}

public String getimg(){
    return this._img;

}


public void setimgP (String imgP){
    this._imgP = imgP;
}

public String getimgP(){
    return this._imgP;
}


public void setsentvia(int sntvia){
    this._sntvia = sntvia;
}

public int getsentvia(){
    return this._sntvia;
}


public void setsentTime(String sentime ){
    this._snttime = sentime;
}

public String getsenttime(){
    return this._snttime;
}

public void settemp (String tmp ){
    this._template = tmp;
}

public String gettmp(){
    return this._template;
}



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


public sentdata(Parcel source){
    _id = source.readInt();
    _data = source.readString();
    _lat = source.readString();
    _long = source.readString();
    _img = source.readString();
    _imgP = source.readString();
    _sntvia = source.readInt();
    _snttime = source.readString();
    _template = source.readString();
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    // TODO Auto-generated method stub
    dest.writeInt(_id);     
    dest.writeString(_data);
    dest.writeString(_lat);
    dest.writeString(_long);
    dest.writeString(_img);
    dest.writeString(_imgP);
    dest.writeInt(_sntvia);
    dest.writeString(_snttime);
    dest.writeString(_template);

}

public static final Parcelable.Creator<sentdata> CREATOR = new Parcelable.Creator<sentdata>() {

    @Override
    public sentdata createFromParcel(Parcel source) {
        // TODO Auto-generated method stub
        return new sentdata(source);
    }

    @Override
    public sentdata[] newArray(int size) {
        // TODO Auto-generated method stub
        return new sentdata[size];
    }




};

}

请帮帮我。我花了差不多一天的时间来处理这个错误并且找不到解决方案。我认为我的地图可能有问题,但我在我的项目中使用谷歌地图是新的。请帮忙。!先感谢您。

1 个答案:

答案 0 :(得分:0)

你应该尝试使用:

args.setClassLoader(sentData.getClassLoader())

在您获取数据之前