我正在使用 Retrofit 将图片加载到RecyclerView
,一次加载10个。当用户将RecyclerView
滚动到底部位置时,会加载另外十个图像。当Activity
进入暂停状态(onPause
)时,我会得到 RunTimeException
。
这不是一个重复的问题。我在网上查了一下并遇到过类似的问题,但是这种例外的原因是不同的......请看下面的内容:
例外:
Process: com.globemaster.samples, PID: 13132<br/>
**java.lang.RuntimeException: Parcel: unable to marshal value**
com.globemaster.samples.RetroPhoto@150122e at
android.os.Parcel.writeValue(Parcel.java:1418) at
android.os.Parcel.writeList(Parcel.java:759) at
android.os.Parcel.writeValue(Parcel.java:1365) at
android.os.Parcel.writeArrayMapInternal(Parcel.java:686) at
android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1330) at
android.os.Bundle.writeToParcel(Bundle.java:1079) at
android.os.Parcel.writeBundle(Parcel.java:711) at
android.support.v4.app.FragmentState.writeToParcel(Fragment.java:157) at
android.os.Parcel.writeTypedArray(Parcel.java:1254) at
android.support.v4.app.FragmentManagerState.writeToParcel(FragmentManager.java:617) at android.os.Parcel.writeParcelable(Parcel.java:1437) at
android.os.Parcel.writeValue(Parcel.java:1343) at
android.os.Parcel.writeArrayMapInternal(Parcel.java:686) at
android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1330) at
android.os.Bundle.writeToParcel(Bundle.java:1079) at
android.os.Parcel.writeBundle(Parcel.java:711)at
android.app.ActivityManagerProxy.activityStopped(ActivityManagerNative.java:3752) at android.app.ActivityThread$StopInfo.run(ActivityThread.java:4700) at
android.os.Handler.handleCallback(Handler.java:739) at
android.os.Handler.dispatchMessage(Handler.java:95) at
android.os.Looper.loop(Looper.java:148) at
android.app.ActivityThread.main(ActivityThread.java:7325) at
java.lang.reflect.Method.invoke(Native Method) at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
MainActivity.java:
list.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
GetDataService service=RetrofitClientInstance.getClient().create(GetDataService.class);
Call<List<RetroPhoto>> call=service.getAllPhotos();
call.enqueue(new Callback<List<RetroPhoto>>() {
@Override
public void onResponse(Call<List<RetroPhoto>> call, Response<List<RetroPhoto>> response) {
Log.e("OnResponse","Called");
data=response.body();
arrayList= (ArrayList) data;
runOnUiThread(new Runnable() {
@Override
public void run() {
android.support.v4.app.FragmentTransaction transaction=getSupportFragmentManager().beginTransaction();
Recycler_Frag recycler_frag=new Recycler_Frag();
Bundle bundle=new Bundle();
bundle.putStringArrayList("list",arrayList);
recycler_frag.setArguments(bundle);
transaction.replace(R.id.frame,recycler_frag).commit();
}
});
}
@Override
public void onFailure(Call<List<RetroPhoto>> call, Throwable t) {
Log.e("OnFailure","Called");
Toast.makeText(getApplicationContext(),"Something wrong",Toast.LENGTH_SHORT).show();
}
});
}
});
Recycler_Frag.java
@Override
public View onCreateView(LayoutInflater inflater, @Nullable final ViewGroup container, @Nullable Bundle savedInstanceState) {
view=inflater.inflate(R.layout.recycler_lyt,container,false);
arrayList=new ArrayList<String>();
importedlist=new ArrayList();
arrayList2=new ArrayList<String>();
importedlist=getArguments().getStringArrayList("list");
if (importedlist.size()>0){
for(counter=0;counter<10;counter++)
{
retroPhoto= (RetroPhoto) importedlist.get(counter);
arrayList.add(retroPhoto.getThumbnailUrl());
}
standardsize=counter;
previouslength= arrayList.size();
}
progressBar=view.findViewById(R.id.progress);
relativeLayout=view.findViewById(R.id.relative);
recyclerView=view.findViewById(R.id.recycler);
recyclerView.setAdapter(new RecyclerAdapter(getContext(),arrayList));
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
}
@Override
public void onScrolled(final RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
try {
if (!recyclerView.canScrollVertically(1))
{
if (!isScrolledup){
isScrolledup=true;
new Thread(new Runnable() {
@Override
public void run()
{
relativeLayout.post(new Runnable() {
@Override
public void run() {
relativeLayout.setVisibility(View.VISIBLE);
}
});
while (counter<standardsize+11)
{
retroPhoto= (RetroPhoto) importedlist.get(counter);
arrayList2.add(retroPhoto.getThumbnailUrl());
counter++;
}
standardsize=counter;
//
recyclerView.post(new Runnable() {
@Override
public void run() {
Iterator iterator=arrayList2.iterator();
while(iterator.hasNext())
{
arrayList.add(iterator.next());
}
arrayList2.clear();
recyclerView.getAdapter().notifyItemRangeInserted(previouslength,arrayList.size()-previouslength);
// recyclerView.scrollToPosition(arrayList.size()-1);
}
});
relativeLayout.post(new Runnable() {
@Override
public void run() {
relativeLayout.setVisibility(View.GONE);
}
});
}
}).start();
Toast.makeText(getContext(),"last",Toast.LENGTH_SHORT).show();
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
});
return view;
}
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.RecyclerHolder>{
private ArrayList<RetroPhoto> dataList;
private Context context;
private RecyclerAdapter(Context context,ArrayList<RetroPhoto> dataList)
{
this.context=context;
this.dataList=dataList;
}
@Override
public RecyclerHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new RecyclerHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item,
parent,false));
}
@Override
public void onBindViewHolder(RecyclerHolder holder, int position) {
// dataList.get(position).getThumbnailUrl()
Picasso.with(context).load(String.valueOf(dataList.get(position)))
.placeholder(R.drawable.ic_launcher_background).error(R.drawable.ic_launcher_background).into(holder.imageView);
}
@Override
public int getItemCount() {
return arrayList.size();
}
public class RecyclerHolder extends RecyclerView.ViewHolder{
TextView textView;ImageView imageView;
public RecyclerHolder(View itemView) {
super(itemView);
imageView=itemView.findViewById(R.id.image);
textView=itemView.findViewById(R.id.text);}
}
}
@Override
public void onPause() {
super.onPause();
Log.e("OnPause","Called");
}
@Override
public void onResume() {
super.onResume();
Log.e("OnResume","Called");
}
}
RetroPhoto.java:
public class RetroPhoto {
@SerializedName("albumId")
private Integer albumId;
@SerializedName("id")
private Integer id;
@SerializedName("title")
private String title;
@SerializedName("url")
private String url;
@SerializedName("thumbnailUrl")
private String thumbnailUrl;
public RetroPhoto(Integer albumId,Integer id,String title,String url,String thumbnailUrl)
{
this.albumId=albumId;
this.id=id;
this.title=title;
this.url=url;
this.thumbnailUrl=thumbnailUrl;
}
public Integer getAlbumId() {
return albumId;
}
public void setAlbumId(Integer albumId) {
this.albumId = albumId;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getThumbnailUrl() {
return thumbnailUrl;
}
public void setThumbnailUrl(String thumbnailUrl) {
this.thumbnailUrl = thumbnailUrl;
}
}