with fragments i got confused on how to call the UI compared to activities as i have to do it differently. Now with my listview i am initialising it in the oncreateView as most of the tutorials state that i have to do. however on my PostExecute function it's seems to return an empty ui and therefore the results will be null which triggers a null exception error.
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);
}
});
}
}
And this is the error that i am getting. And I have the Listview set correctly with no mistakes so i don't think the issue is from that.
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)