无法在Android中保存parseobject

时间:2015-02-04 18:43:11

标签: android parse-platform

请帮忙。我是Android开发和Parse的新手。我正在尝试创建一个对象列表。所以我创建了一个名为“listObject”的类,它扩展了“ParseObject”,我试图保存listObjects。但是当我尝试从Android模拟器添加对象时,我收到此运行时错误。:

java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.Object.hashCode()' on a null object reference
            at java.util.concurrent.ConcurrentHashMap.get(ConcurrentHashMap.java:746)
            at com.parse.ParseObject.<init>(ParseObject.java:158)
            at com.parse.ParseObject.<init>(ParseObject.java:121)
            at com.parse.starter.listObject.<init>(listObject.java:10)
            at com.parse.starter.CreateList.post(CreateList.java:75)
            at com.parse.starter.CreateList.access$000(CreateList.java:29)
            at com.parse.starter.CreateList$1.onClick(CreateList.java:59)
            at android.view.View.performClick(View.java:4756)
            at android.view.View$PerformClick.run(View.java:19749)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

这是我的代码:

public class CreateList extends Activity {

    // UI references.
    private EditText postEditBrand;
    private EditText postEditProduct;
    private EditText postEditPrice;
    private TextView characterCountTextView;
    private Button postButton;

    private int maxCharacterCount = 200;

    private ParseGeoPoint geoPoint;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_create_list);

        Intent intent = getIntent();

        postEditBrand = (EditText) findViewById(R.id.enterBrand);
        postEditProduct = (EditText) findViewById(R.id.enterProduct);

        postButton = (Button) findViewById(R.id.post_button);
        postButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                post();
            }
        });
    }

    private void post () {
        String brand = postEditBrand.getText().toString().trim();
        String product = postEditProduct.getText().toString().trim();

        // Set up a progress dialog
        final ProgressDialog dialog = new ProgressDialog(CreateList.this);
        dialog.setMessage(getString(R.string.progress_post));
        dialog.show();

        // Create a post.
        listObject post = new listObject();

        post.setBrand(brand);
        post.setProduct(product);
        ParseACL acl = new ParseACL();

        // Give public read access
        acl.setPublicReadAccess(true);
        post.setACL(acl);

        // Save the post
        post.saveInBackground(new SaveCallback() {

            @Override
            public void done(com.parse.ParseException e) {
                dialog.dismiss();
                finish();
            }
        });
    }
}

public class listObject extends ParseObject {
    public String getBrand() {
        return getString("brand");
    }

    public void setBrand(String value) {
        put("brand", value);
    }

    public String getProduct() {
        return getString("product");
    }

    public void setProduct(String value) {
        put("product", value);
    }

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

    public void setPrice(Integer value) {
        put("price", value);
    }

    public static ParseQuery<listObject> getQuery() {
        return ParseQuery.getQuery(listObject.class);
    }
}

1 个答案:

答案 0 :(得分:3)

您忘记了代码中的一些要点:

您必须在DAO类中注释扩展ParseObject的类名:

@ParseClassName("ListObject") //THIS
public class listObject extends ParseObject {
public String getBrand() {
    return getString("brand");
}

您必须在初始化Parse(应用程序类)之前添加您的类:

  

在你的中调用ParseObject.registerSubclass(listObject.class)   在调用Parse.initialize()之前的应用程序构造函数。

Check the docs here

顺便说一句,您应该使用Java标准来声明类,在您的情况下,ListObject。 (资本L)

希望它有所帮助!