....没有定义无参数构造函数? [复制]
Posted
技术标签:
【中文标题】....没有定义无参数构造函数? [复制]【英文标题】:....does not define a no-argument constructor? [duplicate] 【发布时间】:2018-01-04 23:22:02 【问题描述】:我收到以下错误,强制关闭一个活动(不是整个应用程序):
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();
我很难看到错误。我觉得我已经正确构建了所有内容,无法正确理解或解释它所指向的错误。 谁能帮帮我?
【问题讨论】:
在您的 Notes.java 文件中,我认为您需要添加一个无构造函数部分public Notes()
你为什么认为你做得对?
【参考方案1】:
除了@omodemilade-bamgbose 的回答,你还需要了解 Java 构造函数的基本原理。
Java 基本上默认提供默认构造函数(不带参数),即使你没有定义它。
但是,在您的情况下,您已经定义了另一个带有一些参数的构造函数。这会导致您让 JVM 了解您没有使用默认构造函数。
【讨论】:
【参考方案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;
【讨论】:
以上是关于....没有定义无参数构造函数? [复制]的主要内容,如果未能解决你的问题,请参考以下文章