gpt4 book ai didi

java - Parcelable:在 fragment 旋转时解码错误时未找到类

转载 作者:行者123 更新时间:2023-12-01 11:34:59 28 4
gpt4 key购买 nike

我有一个包含 2 个 fragment 的 Fragment Activity ,该 fragment Activity 将我的类的数组列表传递给它的 2 个 fragment 。

这 2 个 fragment 在启动时成功接收到了 Parcellable 数组列表,但是当我旋转我的应用程序时,弹出了 Parcelable:Class not found when unmarshalling 错误,我真的不知道出了什么问题。请帮我。抱歉英语不好。

这是我的 FragmentActivity 的代码

public class Activity_Viewdata extends FragmentActivity implements selector {
private static final String TAG = Activity_Viewdata.class.getSimpleName();
private static ArrayList<sentdata> sneardatalist = new ArrayList<sentdata>();
private ViewGroup mapLayout, listLayout;
public static FragmentManager fragmentManager;



Fragment_listview fragment_list;
Fragment_map fragment_map;
FragmentManager myFragmentManager;

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

Log.i(TAG, "here in viewdata");
Bundle mBundle = getIntent().getExtras();
sneardatalist= mBundle.getParcelableArrayList("myList"); //getIntent().getParcelableExtra("myList");
Log.i(TAG, "Size is: " + sneardatalist.size());
Log.v(TAG, "onCreate: savedInstanceState " + (savedInstanceState == null ? "==" : "!=") + " null");
fragmentManager = getSupportFragmentManager();

if(savedInstanceState !=null){
savedInstanceState.getParcelableArrayList("myList");
}else{
mapLayout = (ViewGroup) findViewById(R.id.frameLayout_map);
if(mapLayout!=null){
Log.i(TAG, "onCreate: adding mapview to MainActivity");
Fragment_map fragmentmap = Fragment_map.newInstance(sneardatalist);
FragmentTransaction fragmenttransaction = getSupportFragmentManager().beginTransaction();
fragmenttransaction.replace(mapLayout.getId(), fragmentmap, Fragment_map.class.getName());
fragmenttransaction.commit();
}
listLayout = (ViewGroup) findViewById(R.id.frameLayout_pager);
if(listLayout!=null){
Log.i(TAG, "onCreate: adding listview to MainActivity");
Fragment_listview fragmentlst = Fragment_listview.newInstance(sneardatalist);
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(listLayout.getId(), fragmentlst, Fragment_listview.class.getName());
fragmentTransaction.commit();
}

}

}

这是第一个 fragment

public class Fragment_listview extends  Fragment implements OnPageChangeListener{
private static final String TAG = Fragment_listview.class.getSimpleName();
private static final String MYLIST = "myList";
private static ArrayList<sentdata> data = new ArrayList<sentdata>();
private selector selectorListener;
private Boolean mInitialCreate=true;

ViewPager viewPager;
PagerAdapter adapter;

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



@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);
Log.w(TAG, "here in OCREATE Of list");
Bundle args = getArguments();
if (args != null) {
data = args.getParcelableArrayList(MYLIST);

}

if (savedInstanceState != null) {
data = savedInstanceState.getParcelableArrayList(MYLIST);
mInitialCreate = false;
}
else {
mInitialCreate = true;
}

Log.w(TAG, "here in OCREATE Of map" + "Size is " + data.size());
}




@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.v(TAG, "onCreateView: savedInstanceState " + (savedInstanceState == null ? "==" : "!=") + " null");

View v = inflater.inflate(R.layout.fragment_list, container, false);



viewPager = (ViewPager) v.findViewById(R.id.pager);

viewPager.setOnPageChangeListener(new OnPageChangeListener() {
public void onPageScrollStateChanged(int state) {

}
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}

public void onPageSelected(int position) {
// Check if this is the page you want.
Log.d(TAG, "onPageSelected: " + position);
if (selectorListener != null) {
selectorListener.setdata(Double.parseDouble(data.get(position).getlat()), Double.parseDouble(data.get(position).getlong()), position,2);
}


}
});
Log.i(TAG, "here in listview: " + data.size() );
if(mInitialCreate && data.size()!=0){
adapter = new viewpager_adapter(getActivity(),data);
viewPager.setAdapter(adapter);
}

if (mInitialCreate) {
mInitialCreate = false;
}
return v;

}
@Override
public void onSaveInstanceState(Bundle outState) {

Log.w(TAG, "onSaveInstanceState" + "Saving list");
outState.putParcelableArrayList(MYLIST, data);
super.onSaveInstanceState(outState);
}


@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub

}

@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub

}

@Override
public void onPageSelected(int arg0) {
// TODO Auto-generated method stub

}

这是我的 Fragment2

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 GoogleMap googleMap;
private selector selectorListener;
private static ArrayList<sentdata> data = new ArrayList<sentdata>();
private Boolean mInitialCreate=true;


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);
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);
mInitialCreate = false;

}else {
mInitialCreate = true;
}
Log.w(TAG, "here in OCREATE Of map" + "Size is " + data.size());
}

@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(mInitialCreate && data.size()!=0){
setmap();
}
if (mInitialCreate) {
mInitialCreate = false;
}

return rootView;
}



@Override
public void onSaveInstanceState(Bundle outState) {

Log.w(TAG, "onSaveInstanceState" + "Saving list");
outState.putParcelableArrayList(MYLIST, data);
super.onSaveInstanceState(outState);
}

public void setmap(){
........
}

public void setview(double lat, double longt, int num){
......
}

这是我的课

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];
}




};

我是android新手,可能有一些我需要知道的事情,所以请帮忙。我一直在谷歌上寻找解决方案。谢谢。

这是我的 LogCat

05-07 14:25:03.093: V/Fragment_map(17606): onDestroyView
05-07 14:25:03.153: V/Fragment_listview(17606): onDestroyView
05-07 14:25:03.173: V/Fragment_listview(17606): onDestroy
05-07 14:25:03.173: V/Activity_Viewdata(17606): onDestroy
05-07 14:25:03.213: W/Fragment_listview(17606): here in OCREATE Of list
05-07 14:25:03.213: W/Fragment_listview(17606): here in OCREATE Of mapSize is 2
05-07 14:25:03.263: I/Activity_Viewdata(17606): here in viewdata
05-07 14:25:03.273: I/Activity_Viewdata(17606): Size is: 2
05-07 14:25:03.273: V/Activity_Viewdata(17606): onCreate: savedInstanceState != null
05-07 14:25:03.273: I/Google Maps Android API(17606): Google Play services package version: 7329032
05-07 14:25:03.383: E/Parcel(17606): Class not found when unmarshalling: com.ictcsu.DatabaseClasses.sentdata, e: java.lang.ClassNotFoundException: com.ictcsu.DatabaseClasses.sentdata
05-07 14:25:03.383: D/AndroidRuntime(17606): Shutting down VM
05-07 14:25:03.383: W/dalvikvm(17606): threadid=1: thread exiting with uncaught exception (group=0x40fe72a0)
05-07 14:25:03.403: E/AndroidRuntime(17606): FATAL EXCEPTION: main
05-07 14:25:03.403: E/AndroidRuntime(17606): 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 14:25:03.403: E/AndroidRuntime(17606): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2070)
05-07 14:25:03.403: E/AndroidRuntime(17606): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2095)
05-07 14:25:03.403: E/AndroidRuntime(17606): at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3568)
05-07 14:25:03.403: E/AndroidRuntime(17606): at android.app.ActivityThread.access$700(ActivityThread.java:134)
05-07 14:25:03.403: E/AndroidRuntime(17606): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1209)
05-07 14:25:03.403: E/AndroidRuntime(17606): at android.os.Handler.dispatchMessage(Handler.java:99)
05-07 14:25:03.403: E/AndroidRuntime(17606): at android.os.Looper.loop(Looper.java:137)
05-07 14:25:03.403: E/AndroidRuntime(17606): at android.app.ActivityThread.main(ActivityThread.java:4830)
05-07 14:25:03.403: E/AndroidRuntime(17606): at java.lang.reflect.Method.invokeNative(Native Method)
05-07 14:25:03.403: E/AndroidRuntime(17606): at java.lang.reflect.Method.invoke(Method.java:511)
05-07 14:25:03.403: E/AndroidRuntime(17606): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)

最佳答案

当你旋转屏幕时,android 会销毁 Activity 并重新创建它,这就是你的包返回 null 的原因。尝试将其存储在 onSaveInstanceState 并在 oncreate 方法中检索它。

公共(public)类 Activity_Viewdata 扩展 FragmentActivity 实现选择器 {私有(private)静态最终字符串TAG = Activity_Viewdata.class.getSimpleName();私有(private)静态ArrayList sneardatalist = new ArrayList();私有(private)ViewGroup mapLayout,listLayout;公共(public)静态FragmentManagerfragmentManager;

fragment_list View fragment_list; fragment _映射 fragment _映射;FragmentManager myFragmentManager;

@覆盖protected void onCreate(Bundle savingInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.acitivity_view_pdata);

Log.i(TAG, "here in viewdata");
Bundle mBundle = getIntent().getExtras();
sneardatalist= mBundle.getParcelableArrayList("myList"); //getIntent().getParcelableExtra("myList");
Log.i(TAG, "Size is: " + sneardatalist.size());
Log.v(TAG, "onCreate: savedInstanceState " + (savedInstanceState == null ? "==" : "!=") + " null");
fragmentManager = getSupportFragmentManager();

if(savedInstanceState !=null){
// **remove it you are doing it in fragment right ??**
// savedInstanceState.getParcelableArrayList("myList");
}else{
mapLayout = (ViewGroup) findViewById(R.id.frameLayout_map);
if(mapLayout!=null){
Log.i(TAG, "onCreate: adding mapview to MainActivity");
Fragment_map fragmentmap = Fragment_map.newInstance(sneardatalist);
FragmentTransaction fragmenttransaction = getSupportFragmentManager().beginTransaction();
fragmenttransaction.replace(mapLayout.getId(), fragmentmap, Fragment_map.class.getName());
fragmenttransaction.commit();
}
listLayout = (ViewGroup) findViewById(R.id.frameLayout_pager);
if(listLayout!=null){
Log.i(TAG, "onCreate: adding listview to MainActivity");
Fragment_listview fragmentlst = Fragment_listview.newInstance(sneardatalist);
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(listLayout.getId(), fragmentlst, Fragment_listview.class.getName());
fragmentTransaction.commit();
}

}

}

你的 fragment 尝试保存数组列表的实例状态并尝试在创建的 Activity 上检索它

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if(savedInstanceState!=null){
sneardatalist=savedInstanceState.getParcelableArrayList("myList");
}
}

关于java - Parcelable:在 fragment 旋转时解码错误时未找到类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30091871/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com