null变量的NullPointerException错误。因为列表视图是空的

时间:2015-12-11 16:10:14

标签: java android listview parse-platform

片段我对如何调用UI感到困惑与活动相比,因为我必须以不同方式进行操作。现在使用我的listview,我在oncreateView中初始化它,因为大多数教程都声明我必须这样做。但是在我的PostExecute函数中,它似乎返回一个空的ui,因此结果将为null,从而触发null异常错误。

 public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.listview_main, container, false);
    listview = (ListView) view.findViewById(R.id.listview);

    new RemoteDataTask().execute();
    return view;
}




private class RemoteDataTask extends AsyncTask<Void, Void, Void> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        // Create a progressdialog
        mProgressDialog = new ProgressDialog(getActivity());
        // Set progressdialog title
        mProgressDialog.setTitle("Stored Files");
        // Set progressdialog message
        mProgressDialog.setMessage("Loading...");
        mProgressDialog.setIndeterminate(false);
        // Show progressdialog
        mProgressDialog.show();
    }

    @Override
    protected Void doInBackground(Void... params) {
        // Locate the class table named "UploadedFiles" in Parse.com
        final ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("NewFiles");
        query.orderByDescending("_created_at");
        query.findInBackground(new FindCallback<ParseObject>() {

            @Override
            public void done(List<ParseObject> objects, com.parse.ParseException e) {

                if (e == null) {
                    try {
                        aF = query.find();
                    } catch (com.parse.ParseException e1) {
                        e1.printStackTrace();
                    }
                    Toast.makeText(getActivity(), "Success",
                            Toast.LENGTH_LONG).show();
                } else {
                    Log.d("Error", e.toString());
                }
            }


        });

        return null;
    }


    @Override
    protected void onPostExecute(Void result) {
        // Locate the listview in listview_main.xml

        // Pass the results into an ArrayAdapter
        adapter = new ArrayAdapter<String>(getActivity(), R.layout.list_view_item);
        // Retrieve object "ImageName" from Parse.com database
        for (ParseObject NewFiles : aF) {
            adapter.add((String) NewFiles.get("ImageName"));
        }
        // Binds the Adapter to the ListView
        listview.setAdapter(adapter);
        // Close the progressdialog
        mProgressDialog.dismiss();
        // Capture button clicks on ListView items
        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                // Send single item click data to SingleItemView Class
                Intent i = new Intent(getActivity(), SingleFileView.class);
                // Pass data "name" followed by the position
                i.putExtra("ImageName", aF.get(position).getString("ImageName")
                        .toString());
                // Open SingleItemView.java Activity
                startActivity(i);
            }
        });
    }
}

这是我得到的错误。我正确设置了Listview,没有错误,所以我认为问题不在于此。

Process: com.venomdev.safestorage, PID: 3027
java.lang.NullPointerException: Attempt to invoke interface method 'java.util.Iterator java.util.List.iterator()' on a null object reference
at com.venomdev.safestorage.ListViewFiles$RemoteDataTask.onPostExecute(ListViewFiles.java:146)
at com.venomdev.safestorage.ListViewFiles$RemoteDataTask.onPostExecute(ListViewFiles.java:95)

3 个答案:

答案 0 :(得分:1)

在解除引用之前,您没有验证aF是否为空。是否应该执行空检查,或者如果它为null,则退出该函数取决于您。

你可以试试这个

    if(aF != null)
    {
        for (ParseObject NewFiles : aF) 
        {
            adapter.add((String) NewFiles.get("ImageName"));
        }
    }

此外,在您的doInBackground功能中,无论发生什么情况,您都会报告“成功”。你应该这样做:

               if (e == null) {
                    try {
                        aF = query.find();
                    } catch (com.parse.ParseException e1) {
                        e1.printStackTrace();
                        return; //<-- add this, and if you wish, an eror message
                    }
                    Toast.makeText(getActivity(), "Success",
                            Toast.LENGTH_LONG).show();
                } else {
                    Log.d("Error", e.toString());
                }

答案 1 :(得分:1)

此时您的变量aF未初始化

 for (ParseObject NewFiles : aF) {
            adapter.add((String) NewFiles.get("ImageName"));
        }

可能e为空:

  if (e == null) {
                    try {
                        aF = query.find();
                    } catch (com.parse.ParseException e1) {
                        e1.printStackTrace();
                    }
                    Toast.makeText(getActivity(), "Success",
                            Toast.LENGTH_LONG).show();
                } else {
                    Log.d("Error", e.toString());
                }

并且永远不会执行此行:

aF = query.find();

答案 2 :(得分:1)

NullPointerException通常意味着您尝试从可能已声明但尚未设置的对象调用方法。在没有看到代码的情况下,我猜你试图从arrayList调用一个方法,(可能在for循环中),但arrayList尚未实例化。