我正在尝试将ArrayList
数据(从Fragment
)发送到另一个Fragment
,以便在该片段中为ListView
(称为lv)创建适配器
错误:
java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.ArrayList android.os.Bundle.getStringArrayList(java.lang.String)' on a null object reference at rang.afterflight.fragments.DemandFragment.onCreateView(DemandFragment.java:38)
FinishPostFragment:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.add_post_finish:
// User chose the "Favorite" action, mark the current item
// as a favorite...
FragmentManager fm = getActivity().getFragmentManager();
final Fragment fragment = new DemandFragment();
// put strings to listview in DemandFragment
ParseObject post = new ParseObject("Posts");
post.put("airport", airport_finish.getText().toString());
post.put("date", date_finish.getText().toString());
post.put("time", time_finish.getText().toString());
post.put("persons", persons_finish.getText().toString());
post.put("address", address_finish.getText().toString());
post.put("flightnr", flightnr_finish.getText().toString());
post.saveInBackground(new SaveCallback() {
@Override
public void done(ParseException e) {
airport_finish.setText("");
date_finish.setText("");
time_finish.setText("");
persons_finish.setText("");
address_finish.setText("");
flightnr_finish.setText("");
ParseQuery<ParseObject> query = ParseQuery.getQuery("Posts");
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> scoreList, ParseException e) {
if (e == null) {
ArrayList arrayPost = new ArrayList<String>();
for (ParseObject j : scoreList) {
arrayPost.add(j.getString("airport"));
arrayPost.add(j.getString("date"));
arrayPost.add(j.getString("time"));
arrayPost.add(j.getString("persons"));
arrayPost.add(j.getString("address"));
arrayPost.add(j.getString("flightnr"));
}
Bundle bundle = new Bundle();
bundle.putStringArrayList("listPost", arrayPost);
fragment.setArguments(bundle);
} else {
// error
}
}
});
}
});
fm.beginTransaction().replace(R.id.content_main, new DemandFragment()).commit();
return true;
DemandFragment:
public class DemandFragment extends Fragment {
ListView lv;
ArrayList yourList;
ArrayAdapter adapter;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_demand, container, false);
if(rootView != null){
lv = (ListView) rootView.findViewById(R.id.listDemand);
ArrayList<String> arraypost = getArguments().getStringArrayList("listPost");
Log.d("arraylist", "arraypost");
ArrayAdapter adapter = new ArrayAdapter(getActivity(), android.R.layout.simple_list_item_1, arraypost);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// clicked on item show post
}
});
}
return rootView;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
}
答案 0 :(得分:1)
此方法fragment.setArguments(bundle)
在后台线程中调用。因此,当您在onCreateView()
方法中检索ArrayList时,可能尚未设置参数。
在此代码中:
fm.beginTransaction().replace(R.id.content_main, new DemandFragment()).commit();
您创建了新的DemandFragment,而不是使用fragment
变量。
答案 1 :(得分:1)
您正在创建一个新的DemandFragment并将其添加到片段管理器中。然后,您将在未添加到片段管理器的最终片段上设置参数。所以经理中的片段没有参数。
fm.beginTransaction().replace(R.id.content_main, new DemandFragment()).commit();
您可能希望将实际设置参数的片段添加到片段管理器中。
fm.beginTransaction().replace(R.id.content_main, fragment).commit();
您还需要确保在设置参数后阅读参数...
在回调中完成所有这些操作可能是最简单的。即获取片段管理器,创建新片段,设置参数,然后将其添加到片段管理器。然后,您可以放心,您可以在片段的onCreate()方法中检索参数。
答案 2 :(得分:0)
尝试检索片段onActivityCreated()方法中的ArrayList
,它应该有效。
<强>更新强>:
如 Rey 所说,请使用:
fm.beginTransaction().replace(R.id.content_main, fragment).commit();
而不是:
fm.beginTransaction().replace(R.id.content_main, new DemandFragment()).commit();