简体   繁体   中英

IllegalArgumentException (java) from a class which extends RealmObject

I am getting this error:

IllegalArgumentException

while performing the below method. I have no idea why it is happening.

Any idea whats wrong here ??

    public void sendNoteWithoutImage(){

    Toast.makeText(getContext(), "Step 1", Toast.LENGTH_LONG).show();

    // saving objects
    Note notesRealmClass = new Note();

    notesRealmClass.setTitle(titleStr);

    Toast.makeText(getContext(), "Step 2", Toast.LENGTH_LONG).show();

    ChannelIDs = TextUtils.join(" ",selectedItems);

    Toast.makeText(getContext(), "Step 3", Toast.LENGTH_LONG).show();

    notesRealmClass.setObjId(objId);

    Toast.makeText(getContext(), "Step 4", Toast.LENGTH_LONG).show();

    // save object asynchronously
    Backendless.Persistence.save(notesRealmClass, new AsyncCallback<Note>() {
        public void handleResponse(Note note) {

            Toast.makeText(getContext(), "Step 5", Toast.LENGTH_LONG).show();
            // new Contact instance has been saved
            Toast.makeText(getActivity(), "Successfully posted ", Toast.LENGTH_SHORT).show();

        }

        public void handleFault(BackendlessFault fault) {

            Toast.makeText(getContext(), "Step 6", Toast.LENGTH_LONG).show();
            Log.d("ERROR : ", "" + fault.getMessage());
            Log.d("ERROR Code: ",""+fault.getCode());
            Toast.makeText(getActivity(), "" + fault.getMessage(), Toast.LENGTH_SHORT).show();
            // an error has occurred, the error code can be retrieved with fault.getCode()
        }
    });}

As you can see i put numbered toasts to check which parts of the codes are executing. From step 1 to 4, everything is fine, but not in step 5. I am getting an error directly on step 6 and the error's print is:

 02-18 12:54:09.025 25161-25161/pb.package D/ERROR :: rx/Observable 02-18 12:54:09.025 25161-25161/pb.package D/ERROR Code:: IllegalArgumentException 

Your issue should be when creating your Toast inside AsyncCallBack. Because you're using it inside an anonymous class, AsyncCallBack, you can't simply call getContext or getActivity because you are no longer in the scope of the Activity. Try this:

Toast.makeText(NameOfYourActivityClass.this, "Successfully posted ", Toast.LENGTH_SHORT).show();

For example, let's say your Acitivity is called "NotesActivity" then you would do:

Toast.makeText(NotesActivity.this, "Successfully posted ", Toast.LENGTH_SHORT).show();

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