我对Android很新,我正在尝试使用Parse.com的数据库API构建应用程序。就像一周一样,我在访问数据库并从数据库中检索对象时遇到以下问题:
E / ParseCommandCache:saveEventually线程有错误。 java.lang.IllegalStateException:试图序列化引用新的命令>未保存的对象。 在com.parse.ParseCommand.resolveLocalIds(ParseCommand.java:407) 在com.parse.ParseCommand.onPreExecute(ParseCommand.java:306) 在com.parse.ParseRequest $ 7.then(ParseRequest.java:299) 在com.parse.ParseRequest $ 7.then(ParseRequest.java:296) at com.parse.Task $ 11.run(Task.java:481) 在com.parse.Task $ ImmediateExecutor.execute(Task.java:673) 在com.parse.Task.completeAfterTask(Task.java:477) 在com.parse.Task.continueWithTask(Task.java:353) 在com.parse.Task.continueWithTask(Task.java:364) at com.parse.ParseRequest.executeAsync(ParseRequest.java:296) at com.parse.ParseRequest.executeAsync(ParseRequest.java:286) 在com.parse.ParseCommandCache.maybeRunAllCommandsNow(ParseCommandCache.java:487) 在com.parse.ParseCommandCache.runLoop(ParseCommandCache.java:597) 在com.parse.ParseCommandCache.access $ 000(ParseCommandCache.java:26) 在com.parse.ParseCommandCache $ 2.run(ParseCommandCache.java:146)
这是我的代码(如果它有点乱,我道歉,但我上周一直在尝试各种各样的事情,而且我没有太多时间进行重构或清理。):
package com.example.boacterapp.Pages;
import java.sql.Date;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.google.android.gms.maps.MapView;
import com.parse.FindCallback;
import com.parse.ParseException;
import com.parse.ParseGeoPoint;
import com.parse.ParseObject;
import com.parse.ParseQuery;
public class RecentSightings extends Activity {
ListView listview;
List<ParseObject> ob;
ProgressDialog mProgressDialog;
Alert alert;
List<Alert> alerts;
public ParseGeoPoint coordinates;
private CustomAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.setContentView(R.layout.list_view_layout);
}
private class RemoteDataTask extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Create a progressdialog
mProgressDialog = new ProgressDialog(RecentSightings.this);
// Set progressdialog title
mProgressDialog.setTitle("Parse.com Simple ListView Tutorial");
// Set progressdialog message
mProgressDialog.setMessage("Loading...");
mProgressDialog.setIndeterminate(false);
// Show progressdialog
mProgressDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
ParseQuery<ParseObject> query = ParseQuery.getQuery("AlertsClass");
try {
ob = query.find();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
// Locate the listview in listview_main.xml
listview = (ListView) findViewById(R.id.alertListView);
getDataForListView();
adapter = new CustomAdapter(RecentSightings.this,alerts);
listview.setAdapter(adapter);
// Close the progressdialog
mProgressDialog.dismiss();
// Capture button clicks on ListView items
listview.setOnItemClickListener(new 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(RecentSightings.this,
AlertViewOnMap.class);
// Pass data "name" followed by the position
i.putExtra("coordinatesLatitude", alerts.get(position).getCoordinates().getLatitude());
i.putExtra("coordinatesLongitude", alerts.get(position).getCoordinates().getLongitude());
i.putExtra("description", alerts.get(position).getDescription().toString());
i.putExtra("busNumber", alerts.get(position).getBusNumber());
// Open SingleItemView.java Activity
startActivity(i);
}
});
}
}
public void getDataForListView(){
final List<Alert> alerts = new ArrayList<Alert>();
for (ParseObject alertParse : ob) {
Alert alert = new Alert();
alert.setBusNumber(alertParse.getInt("BusNumber"));
alert.setCoordinates(alertParse.getParseGeoPoint("Coordinates"));
alert.setCreatedAt(alertParse.getDate("createdAt"));
alert.setDescription(alertParse.getString("Description"));
alerts.add(alert);
}
}
}
类CustomAdapter扩展了BaseAdapter {
Context context;
//ArrayList<HashMap<String,String>> data;
List<Alert> ob;
public CustomAdapter(Context context, List<Alert> ob){
this.context = context;
this.ob = ob;
}
@Override
public int getCount() {
return ob.size();
}
@Override
public Object getItem(int pos) {
return ob.get(pos);
}
@Override
public long getItemId(int pos) {
return pos;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView==null){
holder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(R.layout.list_item,null,false);
holder.channel = (TextView) convertView.findViewById(R.id.channelVariableLabel);
holder.viewOnMap = (TextView) convertView.findViewById(R.id.viewOnMapVariableLabel);
holder.description = (TextView) convertView.findViewById(R.id.descriptionVariableLabel);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
holder.channel.setText(ob.get(position).getBusNumber());
holder.viewOnMap.setText("<Tap here to view alert on map>");
holder.description.setText(ob.get(position).getDescription().toString());
convertView.setTag(holder);
return convertView;
}
class ViewHolder {
TextView channel;
TextView viewOnMap;
TextView description;
}
}
class Alert {
private int busNumber;
private ParseGeoPoint coordinates;
private Date createdAt;
private String description;
public Alert () {
}
public void setCreatedAt(java.util.Date date) {
// TODO Auto-generated method stub
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Date getCreatedAt() {
return createdAt;
}
public void setCreatedAt(Date createdAt) {
this.createdAt = createdAt;
}
public ParseGeoPoint getCoordinates() {
return coordinates;
}
public void setCoordinates(ParseGeoPoint coordinates) {
this.coordinates = coordinates;
}
public int getBusNumber() {
return busNumber;
}
public void setBusNumber(int busNumber) {
this.busNumber = busNumber;
}
}
所以,请全能的编程/ Java / Android / Parse.com用户的领主们,帮助这个凡人找到他的问题!
谢谢!尊重。