【问题标题】:ArrayList<Obj> storing null Objects?ArrayList<Obj> 存储空对象?
【发布时间】:2017-02-02 07:07:34
【问题描述】:

我正在尝试提取一些 JSON 数据,对其进行解析并将其作为参数发送,以便在显示信息的片段中打开这些数据。

当收到片段中的捆绑对象时,我得到一个 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: 致命异常: main 进程:com.blitzar.testgrability,PID:1884 java.lang.NullPointerException 在 com.blitzar.testgrability.GalleryFragment.onCreateView(GalleryFragment.java:40)

为什么会这样?任何能让我摆脱困境的线索都将受到高度赞赏,完整的项目在https://github.com/mastakillahBlitzar/Grability

【问题讨论】:

  • 在您实际将数据传递给片段的位置添加代码。
  • 请记住,如果您将数据从活动传递到捆绑包中的片段,那么您必须为片段设置参数,并在onCreateView() 方法中使用getArguments() 检索您的数据。这也是ArrayList&lt;Entry&gt; e= entry; 不是将数据复制到另一个列表的好方法。最好使用list2.addAll(list1);
  • 无法解析方法
  • @Juancadiyjohndiy 将此Bundle bundle= getActivity().getIntent().getExtras(); 更改为Bundle bundle= getArguments();
  • 你在哪里调用了 callFragment() 方法?

标签: android android-fragments arraylist nullpointerexception parcelable


【解决方案1】:

我建议你使用:

JSONObject feed = jsonObj.optJSONObject("feed");

代替:

JSONObject feed = jsonObj.getJSONObject("feed");

如果服务器的响应为空,则省略该值

同样的方式使用字符串n = name.optString("label");

JSONArray image = e.optJSONArray("im:image");

这可能是您从服务器获取 null 的情况,并且由于 null 它无法解析数据,也无法将值存储在您的 ArrayList&lt;Entiry&gt; entries

【讨论】:

  • 实际上我是在将数据添加到数组之前打印数据并正确检索所有数据,所以我认为不是
【解决方案2】:

特别感谢@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());

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-16
    • 2014-01-17
    • 2013-03-01
    • 1970-01-01
    相关资源
    最近更新 更多