我生气了,这段代码工作了一段时间,现在它不是......通过调试器一切正常但是当我刷新我的数据库时我什么也看不见.... 问题是它没有上传到数据库。
规则设置为
“。read”:“true”, “.write”:“true”
public class MainViewFragment extends Fragment {
private static final String TAG = "MainViewFragment";
private RecyclerView mView;
private List<Product> mProducts;
private DatabaseReference mRef;
public MainViewFragment() {
mRef = FirebaseDatabase.getInstance().getReference();
Product pr = new Product("title","price");
mRef.child("products").child(pr.getStringId()).setValue(pr);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_main_view, container, false);
mView = (RecyclerView)v.findViewById(R.id.mainViewRecyclerView);
mProducts = new ArrayList<>();
mView.setLayoutManager(new LinearLayoutManager(getActivity(),LinearLayoutManager.HORIZONTAL,false));
MainViewAdapter adapter = new MainViewAdapter(mProducts,getActivity());
mView.setAdapter(adapter);
return v;
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
}
}
从这里调用片段
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fm = getSupportFragmentManager();
fm.beginTransaction().replace(R.id.container,new MainViewFragment()).commit();
fm.beginTransaction().replace(R.id.container2,new SecondProductFragment()).commit();
在调试器中,代码尝试setValue()后,它会给我这个错误
没有这样的实例字段:'mRef'
答案 0 :(得分:0)
转到此链接:
https://console.firebase.google.com/project/note-it-b6f1c/database/note-it-b6f1c/rules
并将规则设置为:
{
"rules": {
"foo": {
".read": true,
".write": true
}
}
这可能会解决您的问题
答案 1 :(得分:0)
尝试使用地图保存您的数据。
所以替换这个:
public MainViewFragment() {
mRef = FirebaseDatabase.getInstance().getReference();
Product pr = new Product("title","price");
mRef.child("products").child(pr.getStringId()).setValue(pr);
}
用这个
public MainViewFragment() {
mRef = FirebaseDatabase.getInstance().getReference().child("products").push();
//make a map
Map map=new HashMap();
map.put("title","any_title");
map.put("price","any_price");
//write the data
mRef.setValue(map);
}
并确保调用(MainViewFragment())。