我尝试从Firebase检索数据到Android项目上的recycler-view。 首先,这是我在firebase上的结构数据库
然后,我写Ph课
public class Ph {
public String date;
public String nilai;
public String time;
public Ph(){
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getNilai() {
return nilai;
}
public void setNilai(String nilai) {
this.nilai = nilai;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
}
为保存数据,我创建了PhAdapter类。
public class PhAdapter extends RecyclerView.Adapter<PhAdapter.PhViewHolder> {
private ArrayList<Ph> dataList;
public int rowLayout;
private Context context;
public static class PhViewHolder extends RecyclerView.ViewHolder {
private TextView tvNilai, tvDate, tvTime;
private LinearLayout phLayout;
public PhViewHolder(View itemview) {
super(itemview);
phLayout = (LinearLayout) itemview.findViewById(R.id.ph_view);
tvNilai = (TextView) itemview.findViewById(R.id.tv_ph);
tvTime = (TextView) itemview.findViewById(R.id.tv_time);
tvDate = (TextView) itemview.findViewById(R.id.tv_date);
}
}
public PhAdapter(ArrayList<Ph> dataList, Context context) {
this.dataList = dataList;
this.context = context;
}
@Override
public PhAdapter.PhViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(rowLayout, parent, false);
return new PhViewHolder(view);
}
@Override
public void onBindViewHolder(final PhViewHolder holder, final int position) {
holder.tvNilai.setText(dataList.get(position).getNilai());
holder.tvTime.setText(dataList.get(position).getTime());
holder.tvDate.setText(dataList.get(position).getDate());
}
@Override
public int getItemCount() {
return dataList.size();
}
}
最后一个是我的MainActivity类
public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName();
TextView txtNilai, txtDate, txtTime;
Button cekdata;
private DatabaseReference mDatabase;
//get reference database
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view1);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
cekdata = (Button)findViewById(R.id.btn_cekdata);
txtNilai = (TextView)findViewById(R.id.tv_ph);
txtDate = (TextView)findViewById(R.id.tv_date);
txtTime = (TextView)findViewById(R.id.tv_time);
//firebase
mDatabase = FirebaseDatabase.getInstance().getReference("https://raspi-ph.firebaseio.com/Fakultas Teknik/ph");
FirebaseDatabase.getInstance().setPersistenceEnabled(true);
cekdata.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ValueEventListener postListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Ph ph = dataSnapshot.getValue(Ph.class);
}
@Override
public void onCancelled(DatabaseError databaseError) {
//Getting Ph failed, log a message
Log.w(TAG, "loadPost:onCancelled", databaseError.toException());
}
};
mDatabase.addValueEventListener(postListener);
}
});
}
}
但是,当我将项目运行到仿真器时,出现错误java.lang.NoSuchErrorMethod。 MainActivity上第42行引起的错误是获取参考数据库的行。
请帮助我。我的代码有什么错误吗?
错误消息:
07-09 08:45:54.124 6296-6296 / com.example.titiarimba.raspi_ph E / AndroidRuntime:致命异常:main 流程:com.example.titiarimba.raspi_ph,PID:6296 java.lang.NoSuchMethodError:类Lcom / google / firebase / FirebaseApp中没有虚拟方法zzbqo()Z;或其超类(声明为 “ com.google.firebase.FirebaseApp”出现在 /data/app/com.example.titiarimba.raspi_ph-2/split_lib_dependencies_apk.apk) 在com.google.firebase.database.FirebaseDatabase.getInstance(未知 资源) 在com.google.firebase.database.FirebaseDatabase.getInstance(未知 资源) 在com.example.titiarimba.raspi_ph.MainActivity.onCreate(MainActivity.java:43) 在android.app.Activity.performCreate(Activity.java:5990) 在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 在android.app.ActivityThread.access $ 800(ActivityThread.java:151) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1303) 在android.os.Handler.dispatchMessage(Handler.java:102) 在android.os.Looper.loop(Looper.java:135) 在android.app.ActivityThread.main(ActivityThread.java:5254) 在java.lang.reflect.Method.invoke(本机方法) 在java.lang.reflect.Method.invoke(Method.java:372) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:903) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
答案 0 :(得分:1)
尝试删除您的valueEventListener并改为执行
首先删除此侦听器
ValueEventListener postListener = new ValueEventListener() {...
更改此
mDatabase = FirebaseDatabase.getInstance().getReference("https://raspi-ph.firebaseio.com/Fakultas Teknik/ph");
对此
mDatabase = FirebaseDatabase.getInstance().getReference();
,然后在onClick内部使用该引用来访问您的数据。借助此方法,您可以在onDataChange内部使用不同的键访问所有值
mDatabase.child("Fakultas Teknik").child("ph").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot childSnapshot : dataSnapshot.getChildren()){
Ph ph = childSnapshot.getValue(Ph.class);
String date = ph.getDate();
Log.i("Date:",""+date);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(getApplicationContext(), databaseError.getMessage(), Toast.LENGTH_SHORT).show();
}
});
或者您可以尝试使用getReferenceFromUrl()
mDatabase = FirebaseDatabase.getInstance().getReferenceFromUrl("https://raspi-ph.firebaseio.com/Fakultas Teknik/ph");
答案 1 :(得分:0)
这个问题已经解决。我从Firebase控制台下载google-service.json并将其添加到project / app文件夹:)
答案 2 :(得分:0)
在实施FirebaseAuth后,我遇到了Firebase
类似的错误(不是相同)。
尝试了很多之后,我删除了FirebaseCore
依赖项,它起作用了。
答案 3 :(得分:-1)
检查您的依赖关系,确保Fire base核心与Fire base数据库保持一致。
对于eExample
实现'com.google.firebase:firebase-core:16.0.1' 实施'com.google.firebase:firebase-database:16.0.1'
不是
实现'com.google.firebase:firebase-core:16.0.1' 实施'com.google.firebase:firebase-database:10.0.1'