简体   繁体   中英

ParseException: “You must register this ParseObject subclass before instantiating it.”

I'm in Android Studio, work with Parse.com and when I run this code:

public void onClick(View v) {
    Log.i(TAG, "Like-button clicked");
    ParseUser currentUser = ParseUser.getCurrentUser();
    Activity likes = new Activity();
    likes.setToUser(photo.getUser());
    likes.setFromUser(currentUser);
    likes.setType("like");
    likes.setPhoto(photo);
    ParseACL acl = new ParseACL(currentUser);
    acl.setPublicReadAccess(true);
    likes.setACL(acl);
    likes.saveInBackground(new SaveCallback() {
        public void done(ParseException e) {
            //....

It crashes at the line Activity likes = new Activity(); , and says as ParseException: "You must register this ParseObject subclass before instantiating it."

My class Activity looks like this:

package com.parse.myApp

import com.parse.ParseClassName;
import com.parse.ParseFile;
import com.parse.ParseObject;
import com.parse.ParseUser;

/*
 * An extension of ParseObject that makes
 * it more convenient to access information
 * about a given Photo 
 */

@ParseClassName("Activity")
public class Activity extends ParseObject {

    public Activity() {
        // A default constructor is required.
    }

    public ParseUser getFromUser(){
        return getParseUser("fromUser");
    }

    public void setFromUser(ParseUser user){
        put("fromUser", user);
    }

    public ParseUser getToUser(){
        return getParseUser("toUser");
    }

    public void setToUser(ParseUser user){
        put("toUser", user);
    }

    public String getType(){
        return getString("type");
    }

    public void setType(String t){
        put("type", t);
    }

    public String getContent(){
        return getString("content");
    }

    public void setContent(String c){
        put("content", c);
    }

    public ParseFile getPhoto(){
        return getParseFile("photo");
    }

    public void setPhoto(ParseFile pf){
        put("photo", pf);
    }
}

And I have already set in my ApplicationClass :

package com.parse.myApp;

import android.app.Application;

import com.parse.Parse;

import com.parse.ParseACL;

import com.parse.ParseFacebookUtils;

import com.parse.ParseInstallation;

import com.parse.ParseObject;

import com.parse.PushService;

public class AnypicApplication extends Application {
static final String TAG = "Panagram";

@Override
public void onCreate() {
    super.onCreate();       

    /*
     * In this tutorial, we'll subclass ParseObjects for convenience to
     * create and modify Photo objects.
     * 
     * Also, we'll use an Activity class to keep track of the relationships
     * of ParseUsers with each other and Photos. Every time a user follows, likes 
     * or comments, a new activity is created to represent the relationship.
     */
    ParseObject.registerSubclass(Photo.class);
    ParseObject.registerSubclass(Activity.class);
    /*
     * Fill in this section with your Parse credentials
     */
    Parse.initialize(this, "myKey", "myKey");

    // Set your Facebook App Id in strings.xml
    ParseFacebookUtils.initialize("373432736168885");


    /*
     * For more information on app security and Parse ACL:
     * https://www.parse.com/docs/android_guide#security-recommendations
     */
    ParseACL defaultACL = new ParseACL();

    /*
     * If you would like all objects to be private by default, remove this
     * line
     */
    defaultACL.setPublicReadAccess(true);

    /*
     * Default ACL is public read access, and user read/write access
     */
    ParseACL.setDefaultACL(defaultACL, true);

    /*
     *  Register for push notifications.
     */
    PushService.setDefaultPushCallback(this, LoginActivity.class);
    ParseInstallation.getCurrentInstallation().saveInBackground();
}

}

My Manifest.xml looks like this:

   <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.parse.anypic"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="19" />

    <uses-feature android:name="android.hardware.camera" />
    <uses-feature android:name="android.hardware.camera.autofocus" />

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

    <permission
        android:name="com.parse.anypic.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />

    <uses-permission android:name="com.parse.anypic.permission.C2D_MESSAGE" />

    <uses-permission android:name="android.permission.READ_PHONE_STATE" />

    <application
        android:name=".AnypicApplication"
        android:allowBackup="true"
        android:icon="@drawable/anypic_icon"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".HomeListActivity"
            android:label="@string/title_activity_home_list" >

            <!-- <meta-data -->
            <!-- android:name="android.support.PARENT_ACTIVITY" -->
            <!-- android:value="android.app.Activity" /> -->
        </activity>
        <activity
            android:name=".NewPhotoActivity"
            android:label="@string/title_activity_new_photo" >
        </activity>
        <activity
            android:name=".LoginActivity"
            android:label="@string/title_activity_login" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.facebook.LoginActivity"
            android:label="@string/app_name" >
        </activity>

        <meta-data
            android:name="com.facebook.sdk.ApplicationId"
            android:value="@string/app_id" />

        <service android:name="com.parse.PushService" />

        <receiver android:name="com.parse.ParseBroadcastReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <action android:name="android.intent.action.USER_PRESENT" />
            </intent-filter>
        </receiver>
        <receiver
            android:name="com.parse.GcmBroadcastReceiver"
            android:permission="com.google.android.c2dm.permission.SEND" >
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <action android:name="com.google.android.c2dm.intent.REGISTRATION" />

                <!-- IMPORTANT: Change "com.parse.starter" to match your app's package name. -->
                <category android:name="com.parse.anypic" />
            </intent-filter>
        </receiver>

        <activity
            android:name=".RegisterAcitivity"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:label="@string/title_activity_register_acitivity"
            android:theme="@style/FullscreenTheme" >
        </activity>
    </application>

</manifest>

And even if I already have set the registerSubclass, I get every time the same error.

SOLUTION

My problem was that I had two Activity classes, and I did: ParseObject.registerSubclass(Activity.class); , but android didn't know which Activity class it was. So I deleted one of them, and the problem is solved.

SOLUTION

My problem was that I had two Activity classes, and I did: ParseObject.registerSubclass(Activity.class); , but android didn't know which Activity class it was. So I deleted one of them, and the problem is solved.

I recommend you to rename your Activity class into ActivityParse to be sure that you have registered correct class.

Please be sure that you used your ApplicationClass at AndroidManifest and call initialization at onCreate method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM