....没有定义无参数构造函数?

时间:2018-01-04 23:22:03

标签: java android constructor

我收到以下错误,即强制关闭活动(不是整个应用):

com.google.firebase.database.DatabaseException: Class com.example.android.frapp.Notes does not define a no-argument constructor. If you are using ProGuard, make sure these constructors are not stripped.

它似乎表明问题在于:

at com.example.android.frapp.NoteTakerActivity$3.onDataChange(NoteTakerActivity.java:93)

以下是Notes类的代码:

public class Notes {

String notesId;
String notesTitle;
String notesType;

public Notes(String id, String mainNotes) {

}

public Notes(String notesId, String notesTitle, String notesType) {
    this.notesId = notesId;
    this.notesTitle = notesTitle;
    this.notesType = notesType;
}

public String getNotesId() {
    return notesId;
}

public String getNotesTitle() {

    return notesTitle;
}

public String getNotesType() {

    return notesType;
}
}

以下是NoteTakerActivity类的代码:

public class NoteTakerActivity extends AppCompatActivity {

public static final String NOTES_TITLE = "notestitle";
public static final String NOTES_ID = "notesid";

EditText editNoteTitle;
Button addButton;
Spinner spinnerType;

DatabaseReference databaseNotes;

ListView listViewNotes;

List<Notes> notesList;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_note_taker);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    databaseNotes = FirebaseDatabase.getInstance().getReference("notes");

    editNoteTitle = (EditText) findViewById(R.id.noteTitleTxt);
    addButton = (Button) findViewById(R.id.addNoteBtn);
    spinnerType = (Spinner) findViewById(R.id.spinnerNoteType);

    listViewNotes = (ListView) findViewById(R.id.listViewNotes);

    notesList = new ArrayList<>();

    addButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            addNote();
        }
    });

    // for when a note title is clicked to open the next activity to write extensive notes
    listViewNotes.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            Notes notes = notesList.get(i);
            Intent intent = new Intent(getApplicationContext(), AddMainNotesActivity.class);

            intent.putExtra(NOTES_ID, notes.getNotesId());
            intent.putExtra(NOTES_TITLE, notes.getNotesTitle());

            startActivity(intent);
        }
    });

}

@Override
protected void onStart() {
    super.onStart();

    // every time this method is executed it will fecth all the notes from the database and populate the list view
    databaseNotes.addValueEventListener(new ValueEventListener() {
        @Override
        // executed any time something is changed in the database
        public void onDataChange(DataSnapshot dataSnapshot) {

            notesList.clear();

            for (DataSnapshot notesSnapshot: dataSnapshot.getChildren()) {
                Notes notes = notesSnapshot.getValue(Notes.class); // this is line 93

                notesList.add(notes);
            }

            NotesList adapter = new NotesList(NoteTakerActivity.this, notesList);
            listViewNotes.setAdapter(adapter);
        }

        @Override
        // executed if there is some kind of error
        public void onCancelled(DatabaseError databaseError) {

        }
    });
}

private void addNote() {
    String title = editNoteTitle.getText().toString().trim();
    String type = spinnerType.getSelectedItem().toString();

    if (!TextUtils.isEmpty(title)) {

        String id = databaseNotes.push().getKey(); // id being created is unique every time

        Notes notes = new Notes(id, title, type);

        databaseNotes.child(id).setValue(notes); // to send data to database

        Toast.makeText(this, "Title added", Toast.LENGTH_SHORT).show();

        startActivity(getIntent());

    } else {
        Toast.makeText(this, "You must give the note a title", Toast.LENGTH_SHORT).show();
    }
}
}

我很难看到错误。我觉得我已经正确地构建了一切,无法正确理解或解释它指向的错误 任何人都可以帮助我吗?

2 个答案:

答案 0 :(得分:2)

错误消息意味着您需要为Notes class定义一个不带参数的构造函数,如下所示:

public Notes() {

}

所以你会:

public class Notes {

 String notesId;
 String notesTitle;
 String notesType;

 public Notes(String id, String mainNotes) {

 }

 //a blank constructor//
 public Notes() {

 }

 public Notes(String notesId, String notesTitle, String notesType) {
   this.notesId = notesId;
   this.notesTitle = notesTitle;
   this.notesType = notesType;
 }

 public String getNotesId() {
   return notesId;
 }

 public String getNotesTitle() {

  return notesTitle;
 }

 public String getNotesType() {

  return notesType;
 }
}

答案 1 :(得分:2)

除了@ omodemilade-bamgbose的答案之外,您还需要了解Java构造函数的基本原理。

默认情况下,Java默认提供默认构造函数(不带参数),即使您没有定义它。

但是,在您的情况下,您已经定义了另一个带有一些参数的构造函数。这会导致您使JVM明白您没有使用默认构造函数。