我在Logcat中收到此运行时错误:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.parse.ParseFile.getUrl()' on a null object reference
at com.google.geoplace.MainActivity$RemoteDataTask.doInBackground(MainActivity.java:68)
at com.google.geoplace.MainActivity$RemoteDataTask.doInBackground(MainActivity.java:36)
at android.os.AsyncTask$2.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
这是
MainActivity
类public class MainActivity extends Activity {
// Declare Variables
ListView listview;
List<ParseObject> ob;
ProgressDialog mProgressDialog;
ListViewAdapter adapter;
private List<PlaceFilter> worldpopulationlist = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the view from listview_main.xml
setContentView(R.layout.activity_main);
// Execute RemoteDataTask AsyncTask
new RemoteDataTask().execute();
}
// RemoteDataTask AsyncTask
private class RemoteDataTask extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Create a progressdialog
mProgressDialog = new ProgressDialog(MainActivity.this);
// Set progressdialog title
mProgressDialog.setTitle("Parse.com Custom ListView Tutorial");
// Set progressdialog message
mProgressDialog.setMessage("Loading...");
mProgressDialog.setIndeterminate(false);
// Show progressdialog
mProgressDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
// Create the array
worldpopulationlist = new ArrayList<PlaceFilter>();
try {
// Locate the class table named "Country" in Parse.com
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>(
"geo_filters");
// Locate the column named "ranknum" in Parse.com and order list
// by ascending
ob = query.find();
for (ParseObject geo_filters : ob) {
// Locate images in flag column
ParseFile image = (ParseFile) geo_filters.get("FilterFile");
PlaceFilter map = new PlaceFilter();
map.setPlaceName((String) geo_filters.get("PlaceName"));
map.setFilterFile(image.getUrl());
worldpopulationlist.add(map);
}
} catch (ParseException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
// Locate the listview in listview_main.xml
listview = (ListView) findViewById(R.id.listView);
// Pass the results into ListViewAdapter.java
adapter = new ListViewAdapter(MainActivity.this,
worldpopulationlist);
// Binds the Adapter to the ListView
listview.setAdapter(adapter);
// Close the progressdialog
mProgressDialog.dismiss();
}
}
}
这是
PlaceFilter
类:public class PlaceFilter {
private String PlaceName;
private String FilterFile;
public String getPlaceName() {
return PlaceName;
}
public void setPlaceName(String PlaceName) {
this.PlaceName = PlaceName;
}
public String getFilterFile() {
return FilterFile;
}
public void setFilterFile(String FilterFile) {
this.FilterFile = FilterFile;
}
}
它从解析后端返回
PlaceName
,而没有图像,PlaceFilter.java
中存在一些语法错误。纠正错误后,我得到运行时错误。 最佳答案
直接原因是geo_filters.get("FilterFile")
返回null
。
下一步,您必须检查从“ geo_filters”中检索到的ParseObject
,以及为什么其中某些没有“ FilterFile”值。