本文介绍了ListItem缺少一个没有参数的构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个问题。我想了解一些关于Firebase等等。今天,我做了RecyclerView,它应该显示我的Firebase数据库的项目。但是,如果我运行它,我得到这个错误:
E / AndroidRuntime:致命的例外:主
过程:com。 example.andrejulia.painting,PID:16777
com.google.firebase.database.DatabaseException:类com.example.andrejulia.painting.ListItem缺少一个没有参数的构造函数
在com.google.android .gms.internal.zzbqi $ zza.zze(未知来源)
在com.google.android.gms.internal.zzbqi $ zza.zzaG(未知来源)
在com.google.android.gms .internal.zzbqi.zze(未知来源)
在com.google.android.gms.internal.zzbqi.zzb(未知来源)
在com.google.android.gms.internal.zzbqi.zza (未知来源)
在com.google.firebase.database.DataSnapshot.getValue(未知来源)
在com.example.andrejulia.painting.RoomActivity $ 2.onChildChanged(RoomActivity.java:87)$ b $ com在com.google.a ndroid.gms.internal.zzblz.zza(未知来源)
在com.google.android.gms.internal.zzbnz.zzYj(未知来源)
在com.google.android.gms.internal。 zzboc $ 1.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
在android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7229)
在java.lang.reflect.Method.invoke(Native方法)
at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
我已经检查了五次以上,但是即使在Google的帮助下也无法解决我很少需要回答什么是错的与我的代码,我得到这个错误?
这是我的适配器:
$ pre $ RecyclerAdapter扩展了RecyclerView.Adapter< RecyclerAdapter.ViewHolder> {
私人列表< ListItem> listItems中;
public RecyclerAdapter(List< ListItem> listItems){
this.listItems = listItems;
$ b @Override
public RecyclerAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,int viewType){
View v = LayoutInflater.from(parent.getContext())$ b。b .inflate(R.layout.room_items,parent,false);
返回新的ViewHolder(v);
$ b @Override
public void onBindViewHolder(final RecyclerAdapter.ViewHolder持有者,int位置){
ListItem listItem = listItems.get(position);
holder.text_desc.setText(listItem.desc);
holder.text_Title.setText(listItem.head);
holder.itemView.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener(){
@Override
public void onCreateContextMenu(ContextMenu菜单,View v,ContextMenu.ContextMenuInfo menuInfo){
menu.add(holder.getAdapterPosition(),0,0,test);
menu.add(holder.getAdapterPosition(),1,0,test2);
}
});
@Override
public int getItemCount(){
return listItems.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView text_Title;
TextView text_desc;
public ViewHolder(View itemView){
super(itemView);
text_Title =(TextView)itemView.findViewById(R.id.room_title);
text_desc =(TextView)itemView.findViewById(R.id.room_describtion);
$ b $ p $这些是我的ListItem类:
public class ListItem {
字符串头;
String desc;
字符串键;
public ListItem(String head,String desc,String key){
this.head = head;
this.desc = desc;
this.key = key;
}
最后我的MainActivity
public class RoomActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private RecyclerView.Adapter mAdapter;
私人列表< ListItem>结果;
private FirebaseDatabase mDatabase = FirebaseDatabase.getInstance();
private DatabaseReference mMainReference;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_room);
mMainReference = mDatabase.getReference(Room);
settingRecyclerView();
FloatingActionButton fab =(FloatingActionButton)findViewById(R.id.fabCreateRoom);
fab.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
$ b}
});
updateList();
private void settingRecyclerView(){
recyclerView =(RecyclerView)findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(新的LinearLayoutManager(this));
result = new ArrayList<>();
mAdapter = new RecyclerAdapter(result);
recyclerView.setAdapter(mAdapter);
@Override
public boolean onContextItemSelected(MenuItem item){
switch(item.getItemId()){
case 0 :
break;
案例1:
break;
}
return super.onContextItemSelected(item);
$ b private void updateList(){
mMainReference.addChildEventListener(new ChildEventListener(){
@Override
public void onChildAdded(DataSnapshot dataSnapshot,String s){
result.add(dataSnapshot.getValue(ListItem.class));
mAdapter.notifyDataSetChanged();
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot,String s){
ListItem listItem = dataSnapshot.getValue(ListItem.class);
int index = getItemIndex(listItem);
result.set (index,listItem);
mAdapter.notifyItemChanged(index);
}
$ b @覆盖
public void onChildRemoved(DataSnapshot dataSnapshot){
ListItem listItem = dataSnapshot.getValue(ListItem.class);
$ b $ int index = getItemIndex(listItem);
result.remove(index);
mAdapter.notifyItemRemoved(索引);
$ b @Override
public void onChildMoved(DataSnapshot dataSnapshot,String s){
}
@Override
public void onCancelled(DatabaseError databaseError){
$ b}
});
private int getItemIndex(ListItem list){
int index = -1;
(int i = 0; i< result.size(); i ++){
if(result.get(i).key.equals(list.key)){
index = i;
break;
}
}
return index;
}
我希望你们能帮助我。这里是我测试的Firebase实时数据库JSON代码:
{
房间:[null,{
desc:Test2,
head:Test1
},{
desc:Test4,
head:Test3
]
如果有人可以帮我,我会很开心,这个问题的解决方法如下:
您缺少Firebase所需的空构造函数。
我建议您添加一个:
public ListItem(){
this(,,);
}
I got a problem. I am trying to learn a bit about Firebase etc. Today I made RecyclerView which should show Items of my Firebase Database. But if i run it i get this error:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.andrejulia.painting, PID: 16777
com.google.firebase.database.DatabaseException: Class com.example.andrejulia.painting.ListItem is missing a constructor with no arguments
at com.google.android.gms.internal.zzbqi$zza.zze(Unknown Source)
at com.google.android.gms.internal.zzbqi$zza.zzaG(Unknown Source)
at com.google.android.gms.internal.zzbqi.zze(Unknown Source)
at com.google.android.gms.internal.zzbqi.zzb(Unknown Source)
at com.google.android.gms.internal.zzbqi.zza(Unknown Source)
at com.google.firebase.database.DataSnapshot.getValue(Unknown Source)
at com.example.andrejulia.painting.RoomActivity$2.onChildChanged(RoomActivity.java:87)
at com.google.android.gms.internal.zzblz.zza(Unknown Source)
at com.google.android.gms.internal.zzbnz.zzYj(Unknown Source)
at com.google.android.gms.internal.zzboc$1.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7229)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
I already checked it more than five times but i couldnt solve it not even with the help of Google so I poorly need to answer What is wrong with my code that i get this error?
Here is my Adapter:
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {
private List<ListItem> listItems;
public RecyclerAdapter(List<ListItem> listItems) {
this.listItems = listItems;
}
@Override
public RecyclerAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.room_items, parent, false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(final RecyclerAdapter.ViewHolder holder, int position) {
ListItem listItem = listItems.get(position);
holder.text_desc.setText(listItem.desc);
holder.text_Title.setText(listItem.head);
holder.itemView.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() {
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
menu.add(holder.getAdapterPosition(), 0, 0, "test");
menu.add(holder.getAdapterPosition(), 1, 0, "test2");
}
});
}
@Override
public int getItemCount() {
return listItems.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
TextView text_Title;
TextView text_desc;
public ViewHolder(View itemView) {
super(itemView);
text_Title = (TextView) itemView.findViewById(R.id.room_title);
text_desc = (TextView) itemView.findViewById(R.id.room_describtion);
}
}
Here is my ListItem class:
public class ListItem {
String head;
String desc;
String key;
public ListItem(String head, String desc, String key) {
this.head = head;
this.desc = desc;
this.key = key;
}
And finally my MainActivity
public class RoomActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private RecyclerView.Adapter mAdapter;
private List<ListItem> result;
private FirebaseDatabase mDatabase = FirebaseDatabase.getInstance();
private DatabaseReference mMainReference;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_room);
mMainReference = mDatabase.getReference("Room");
settingRecyclerView();
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fabCreateRoom);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
updateList();
}
private void settingRecyclerView(){
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
result = new ArrayList<>();
mAdapter = new RecyclerAdapter(result);
recyclerView.setAdapter(mAdapter);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
switch(item.getItemId()){
case 0:
break;
case 1:
break;
}
return super.onContextItemSelected(item);
}
private void updateList(){
mMainReference.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
result.add(dataSnapshot.getValue(ListItem.class));
mAdapter.notifyDataSetChanged();
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
ListItem listItem = dataSnapshot.getValue(ListItem.class);
int index = getItemIndex(listItem);
result.set(index, listItem);
mAdapter.notifyItemChanged(index);
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
ListItem listItem = dataSnapshot.getValue(ListItem.class);
int index = getItemIndex(listItem);
result.remove(index);
mAdapter.notifyItemRemoved(index);
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
private int getItemIndex(ListItem list){
int index = -1;
for(int i = 0; i < result.size(); i++){
if(result.get(i).key.equals(list.key)){
index = i;
break;
}
}
return index;
}
I hope you guys can help me. Here is my test JSON Code of Firebase Realtime Database:
{
"Room" : [ null, {
"desc" : "Test2",
"head" : "Test1"
}, {
"desc" : "Test4",
"head" : "Test3"
} ]
I would be very happy if someone can help me and I wanna thank you for taking the time and try to solve this problem .
解决方案
As the error message says, you are missing an empty constructor needed by Firebase.
I suggest you add one:
public ListItem() {
this("", "", "");
}
这篇关于ListItem缺少一个没有参数的构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!