getParseFile(string key)使用扩展的ParseObject类从Parse.com返回null

时间:2015-01-03 10:16:31

标签: java android nullpointerexception parse-platform

我有一个包含6个字段的扩展ParseObject类。除了ParseFile getter方法之外,所有字段都返回正确的数据。以旧方式获取ParseFiles是有效的,但由于某些原因,当我使用扩展类时,调用GetDataCallback时数据为null。它有一个URL和图像名称,但数据字段为空。

我的扩展课程:

package [mypackage];

import com.parse.ParseClassName;
import com.parse.ParseFile;
import com.parse.ParseObject;
import java.io.Serializable;

@ParseClassName("Listing")
public class Listing extends ParseObject implements Serializable {
    private boolean  active;
    private String description, title, username;
    private int price;
    private ParseFile file;

    public Listing() {
        super();
    }

    public void setDetail(boolean active, String description, String title,
                          String username, int price, ParseFile file) {
        this.active = active;
        this.description = description;
        this.price = price;
        this.title = title;
        this.username = username;
        this.file = file;
    }

    /* getter methods */
    public boolean getIsActive() {
        return getBoolean("active");
    }

    public String getDescription() {
        return getString("description");
    }

    public int getPrice() {
        return getInt("price");
    }

    public String getListingTitle() { /* getTitle() reserved by android */
        return getString("title");
    }

    public String getUsername() {
        return getString("username");
    }

    public ParseFile getFile() {
        return getParseFile("image");
    }
}

调用getter方法的地方:

public void getListings() {
        ParseQuery<Listing> query = ParseQuery.getQuery(Listing.class);
        /* only retrieve active listings */
        query.whereEqualTo("active", true);
        query.findInBackground(new FindCallback<Listing>() {

            @Override
            //public void done(List<ParseObject> listingList, ParseException e) {
            public void done(List<Listing> listingList, ParseException e) {

                if (e == null) {
                    Log.d("listing", "Retrieved " + listingList.size() + " listings");
                    /* clear adapter before populating */
                    adapter.clear();
                    /* iterate through listings and create listing objects */
                    for (Listing listingObject : listingList) {
                        boolean active;
                        String description, title, username;
                        int price;

                        active = listingObject.getIsActive();
                        username = listingObject.getUsername();
                        description = listingObject.getDescription();
                        title = listingObject.getListingTitle();
                        price = listingObject.getPrice();
                        file =  listingObject.getFile();

                        /* create a listing object to be added to a ListView */    
                        Listing listing = new Listing();
                        listing.setDetail(active, description, title, username, price, file);
                        listings.add(listing);

                    } /* end for loop */
                }
                else {
                    Log.d("listing", "Error: " + e.getMessage());
                }
            }
        });
    }

在适配器中:

public View getView(int position, View convertView, ViewGroup parent){
        if(convertView == null){
            LayoutInflater mLayoutInflater = LayoutInflater.from(context);
            convertView = mLayoutInflater.inflate(R.layout.listing_row_item, null);
        }

        Listing listing = listings.get(position);
        TextView titleView = (TextView) convertView.findViewById(R.id.listing_title);
        TextView priceView = (TextView) convertView.findViewById(R.id.listing_price);
        final ParseImageView imageView = (ParseImageView) convertView.findViewById(R.id.ivPicture);

        titleView.setText(listing.getListingTitle());
        priceView.setText("$" + String.valueOf(listing.getPrice()));

        listing.getFile().getDataInBackground(new GetDataCallback() { //getFile() returns null

            public void done(byte[] data, ParseException e) {

修改 我相信我的根本误解是如何设置扩展ParseObject的值。如下面的答案所示,这里的put Parse方法实际上将值放在对象中。我的印象是put只用于实际的数据库操作,因此我的setter方法没有正确设置ParseObject。

1 个答案:

答案 0 :(得分:1)

Uggh ......我们太近了。我发现了问题,你没有将值设置为ParseObject中的字段,这就是为什么适配器中的所有内容都为null。将以下内容添加到Listing类中,并按如下所示更改setDetail方法:

public void setDetail(boolean active, String description, String title,
                      String username, int price, ParseFile file) {

    setIsActive(active);
    setIsActive(description);
    setPrice(price);
    setListingTitle(title);
    setUsername(username);
    setFile(file);
}

public void setIsActive(boolean a) {
    put("active", a);
}

public void setDescription(String s) {
    put("description", s);
}

public void setPrice(int p) {
    put("price", p);
}

public void setListingTitle(String t) {
    put("title", t);
}

public void setUsername(String u) {
    put("username", u);
}

public void setFile(ParseFile f) {
    put("image", f);
}

旧答案

我可能错了,但我很确定你应该保存ParseFile的URL而不是ParseFile本身,并使用URL来获取文件。因此,在done(..)方法中,您应该执行以下操作:

file.getUrl();

并将其设置为String。继续,如果