我正在将ArrayList
从Fragment
传递到另一个{我正在使用Parcelable
来实现这一点。我正在使用ArrayList
传递Bundle
。但是在另一个片段中,我无法接受这个
这是实现 Parelable
public class CurrentEntry implements Parcelable{
String name;
String people;
String estimate;
public CurrentEntry()
{
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPeople() {
return people;
}
public void setPeople(String people) {
this.people = people;
}
public String getEstimate() {
return estimate;
}
public void setEstimate(String estimate) {
this.estimate = estimate;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(this.name);
dest.writeString(this.people);
dest.writeString(this.estimate);
}
public static final Parcelable.Creator<CurrentEntry> CREATOR
= new Parcelable.Creator<CurrentEntry>() {
public CurrentEntry createFromParcel(Parcel in) {
return new CurrentEntry(in);
}
public CurrentEntry[] newArray(int size) {
return new CurrentEntry[size];
}
};
public CurrentEntry(Parcel in)
{
this.name=in.readString();
this.people=in.readString();
this.estimate=in.readString();
}
}
这是将ArrayList从一个Fragment发送到另一个Fragment的代码。
JsonObjectRequest req = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try
{
JSONObject object=new JSONObject(response.toString());
JSONArray data=object.getJSONArray("results");
for( i=0;i<data.length();i++)
{
JSONObject json = data.getJSONObject(i);
final CurrentEntry c=new CurrentEntry();
userUrl=json.getString("list");
JsonObjectRequest request=new JsonObjectRequest(Request.Method.GET, userUrl, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONObject temp=response.getJSONObject("user");
String foodie_name=temp.getString("first_name");
String lname=temp.getString("last_name");
c.setName(name+"\t"+lname);
progressDialog.dismiss();
} catch (JSONException e) {
e.printStackTrace();
}
// adapt.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
})
AppController.getInstance().addToRequestQueue(request,second_req);
c.setPeople(json.getString("no_people"));
String temp=json.getString("predicated_t");
c.setEstimate(temp+"min");
// c.setNo(count);
TempUserStatusInfo info=new TempUserStatusInfo();
Bundle bundle=new Bundle();
bundle.putParcelable("myList",c);
info.setArguments(bundle);
current.add(c);
adapt=new NewAdapter(current,getActivity().getApplicationContext());
recyclerView.setAdapter(adapt);
}
}
catch (JSONException e)
{
e.printStackTrace();
}
}
这是用于接收我正在设置SwipeCards
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v=inflater.inflate(R.layout.temp_user_status_info, container, false);
swipeCardsView=(SwipeCardsView)v.findViewById(R.id.swipeCard);
infoAdapter=new CurrentInfoAdapter(info,getActivity());
swipeCardsView.retainLastCard(false);
swipeCardsView.enableSwipe(true);
CurrentInfo currentInfo=new CurrentInfo();
Bundle bundle=this.getArguments();
if(bundle!=null){
currentInfo=bundle.getParcelable("myList");
}
person_name=getArguments().getString("name");
person_people=getArguments().getString("people");
person_estimate=getArguments().getString("estimate");
currentInfo.setName(person_name);
currentInfo.setPeople(person_people);
currentInfo.setEstimate(person_estimate);
Log.i("name",person_name);
Log.i("people",person_people);
Log.i("estimate",person_estimate);
info.add(currentInfo);
swipeCardsView.setAdapter(infoAdapter);
return v;
}
错误是
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.BaseBundle.getString(java.lang.String)' on a null object reference
at biz.fyra.QueueApp.TempUserStatusInfo.onCreateView(TempUserStatusInfo.java:52)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2248)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1340)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1569)
答案 0 :(得分:1)
问题是您没有设置字符串值,但是您尝试接收该值。这就是为什么你的空指针异常.Pass字符串值也。
TempUserStatusInfo info=new TempUserStatusInfo();
Bundle bundle=new Bundle();
bundle.putParcelable("myList",c)
bundle.putString("name",value);
bundle.putString("people",value);
bundle.putString("estimate",value);
答案 1 :(得分:1)
使用正确的arraylist发送parceable arraylist而不是类对象
putParcelableArrayListExtra("list", infoArrayList);
自定义对象arraylist是
ArrayList<CurrentInfo> infoArrayList;
在接收Fragment
课程中使用此
ArrayList<CurrentInfo> infoArrayList = bundle.getParcelableArrayListExtra("list");
答案 2 :(得分:0)
您可以将Serializable
用于此
//for putting in bundle
bundle.putSerializable("key", class_value)
//for receieving
object = arguments.getSerializable("key") as className
不要忘记使用serializable
扩展课程。
答案 3 :(得分:0)
另一个想法是使用Gson制作json对象然后使它的字符串发送此字符串,并在另一侧再次使用Gson从该json字符串创建对象。
将Java对象转换为JSON
Gson gson = new Gson();
Staff obj = new Staff();
String jsonInString = gson.toJson(obj);
从json恢复为对象:
Gson gson = new Gson();
Staff staff = gson.fromJson(jsonInString, Staff.class);
将以下依赖项添加到您的gradle文件中:
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.6.2</version>
</dependency>
通过执行此操作,您无需序列化/反序列化,您可以将其作为字符串发送。
答案 4 :(得分:0)
尝试将arraylist一个片段传递给另一个片段
你的第一个片段
ArrayList<ParcelableRow> parceArrayList = new ArrayList<ParcelableRow>();
Bundle bundle=new Bundle(); bundle.putParcelableArrayListExtra("yourArraylist",parceArrayList);
在其他片段中获取Arraylist
ArrayList<ParcelableRow> result = data.getParcelableArrayList("yourArraylist");