java - 来自 null 变量的 NullPointerException 错误。当 ListView 变空时

标签 java android listview parse-platform

对于 fragment ,我对如何调用 UI 与 Activity 相比感到困惑,因为我必须以不同的方式进行调用。现在,使用我的 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)

最佳答案

在解除引用之前,您没有验证 aF 是否不为 null。是否应该进行 null 检查,或者如果为 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());
                }

关于java - 来自 null 变量的 NullPointerException 错误。当 ListView 变空时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34227829/

相关文章:

java - hibernate : ORA-00907: missing right parenthesis

java - 即使命名良好,参数也不作为命名参数存在

Android Ble断开连接时间延迟

java - 从我的 ListView 中添加 ItemOnClick,使用 JSON 从 MYSQL 中获取数据

listview - 仅针对xamarin中的特定列表项在listview中隐藏行

java - 字节码中的内部 kotlin 修饰符是什么?

android - 任何方式以编程方式在android上运行shell命令?

android - 如何在 ImageView 中缩放特定 XY 坐标(点)

java - OnLongClickListener - 没有被解雇+ android

java - 运行 Calendar Api 示例代码时出错