我正在尝试提取一些JSON数据,解析它并将其作为参数发送,以便在显示信息的片段中打开此数据。
当收到片段中的bundle对象时,我得到一个NULL指针异常。
尝试追溯错误,在我看来,ArrayList存储空对象,因此当将该数组传递给片段时,试图用空值替换实例
这是我在entry = bundle.getParcelableArrayList(“Entry”)中得到错误的地方;:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//Inflate the fragment layout file
Bundle bundle= getActivity().getIntent().getExtras();
entry= bundle.getParcelableArrayList("Entry");
Log.d("f", String.valueOf(entry));
ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.gallery_fragment_layout,container,false);
initViews(rootView);
setRetainInstance(true);
return rootView;
}
这就是我将对象添加到数组的方式:
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
//Getting object feed
JSONObject feed = jsonObj.getJSONObject("feed");
//Getting array node
JSONArray entry = feed.getJSONArray("entry");
for (int i = 0; i < entry.length(); i++) {
JSONObject e = entry.getJSONObject(i);
JSONObject name = e.getJSONObject("im:name");
String n = name.getString("label");
JSONArray image = e.getJSONArray("im:image");
for (int y = 0; y < image.length(); y++) {
JSONObject im = image.getJSONObject(y);
ims = im.getString("label");
}
JSONObject summary = e.getJSONObject("summary");
String label = summary.getString("label");
JSONObject category = e.getJSONObject("category");
JSONObject at = category.getJSONObject("attributes");
String term = at.getString("term");
data = new Entry();
data.setCategory(term);
data.setImage(ims);
data.setName(n);
data.setSummary(label);
Log.d("hola", term);
entries.add(i, data);
//System.out.printf("das",Arrays.toString(entries));
}
将数据传递给Fragment:
private void callFragment(GalleryFragment galleryFragment, ArrayList<Entry> entry) {
//create parcel and add
ArrayList<Entry> e= entry;
galleryFragment= new GalleryFragment();
Bundle bundle = new Bundle();
bundle.putParcelableArrayList("Entry",e);
galleryFragment.setArguments(bundle);
Log.d("s", String.valueOf(entry));
//Initiate transaction
FragmentTransaction transaction= getSupportFragmentManager().beginTransaction();
transaction.replace(android.R.id.content, galleryFragment, "galleryFragment");
transaction.addToBackStack(null);
transaction.commit();
}
这是日志:
02-02 02:05:17.680 1884-1884 / com.blitzar.testgrability E / AndroidRuntime:FATAL EXCEPTION:main 过程:com.blitzar.testgrability,PID:1884 显示java.lang.NullPointerException 在com.blitzar.testgrability.GalleryFragment.onCreateView(GalleryFragment.java:40)
为什么会这样?任何能够加速我的工作都会受到高度赞赏,整个项目都在https://github.com/mastakillahBlitzar/Grability
答案 0 :(得分:0)
我建议您使用:
JSONObject feed = jsonObj.optJSONObject("feed");
而不是:
JSONObject feed = jsonObj.getJSONObject("feed");
如果服务器
中的响应为null,则省略该值使用String n = name.optString("label");
JSONArray image = e.optJSONArray("im:image");
因此,您可能会从服务器获取null,并且由于null,它无法解析数据,也无法解析ArrayList<Entiry> entries
答案 1 :(得分:0)
特别感谢@Piyush指出我将数据从一个数组传输到另一个数组的错误
e.addAll(entry);
我通过解码我之前没有意识到的数组解决了这个问题 这是结果代码:
Bundle bundle = getArguments();
entry= bundle.getParcelableArrayList("Entry");
e=new Entry[entry.size()];
for(int x=0; x<entry.size();x++) {
e[x]=entry.get(x);
Log.d("testing",e[x].getName());