来自扩展RealmObject的类的IllegalArgumentException(java)

时间:2016-02-18 07:33:28

标签: java android realm

我收到此错误:

  

IllegalArgumentException

执行以下方法时。我不知道为什么会这样。

这里有什么想法吗?

    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()
        }
    });}

正如你所看到的,我把数字吐司检查代码的哪些部分正在执行。从步骤1到4,一切都很好,但不是在第5步。我在第6步直接收到错误,错误的打印是:

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

1 个答案:

答案 0 :(得分:1)

您的问题应该是在AsyncCallBack中创建Toast时。因为您在匿名类AsyncCallBack中使用它,所以不能简单地调用getContext或getActivity,因为您不再属于Activity的范围。试试这个:

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

例如,假设您的Acitivity被称为“NotesActivity”,那么您可以这样做:

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