从片段写入Firebase数据库

时间:2019-03-22 16:01:54

标签: android firebase android-fragments firebase-realtime-database

我正在尝试从一个片段写入Firebase数据库,但是我无法做到这一点。

代码:

public final class MainFragment extends Fragment {

Context thiscontext;
private Firebase fire;

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mReceiver = new LoadAlarmsReceiver(this);
}

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    thiscontext = container.getContext();
    Firebase.setAndroidContext(MainFragment.this.thiscontext);
    final View v = inflater.inflate(R.layout.fragment_main, container, false);
    final FloatingActionButton fab1 =  v.findViewById(R.id.fab1);
    fab1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //Adding functionality to the text views
            fire = new Firebase("https://myapp-7t8s9.firebaseio.com/");
            Firebase fireChild = fire.child("S1");
            fireChild.setValue("ON");
        }
    });
    return v;
}

更新:此代码在android amulater上有效,但在物理设备上不起作用。 这是logcat:03-26 22:40:12.905 23456-23456/? V/FA: Registered activity lifecycle callback 03-26 22:40:12.927 23456-23484/? V/FA: Using measurement service 03-26 22:40:12.936 23456-23484/? V/FA: Connecting to remote service 03-26 22:40:13.106 23456-23484/? D/FA: Connected to remote service 03-26 22:40:13.107 23456-23484/? V/FA: Processing queued up service tasks: 1 03-26 22:40:15.407 22447-22476/? E/MiPicks-Connection: create session error : RESULT_ERROR 03-26 22:40:21.439 22223-22247/com.banaao.makersplace.iotsense E/FirebaseInstanceId: Token retrieval failed: SERVICE_NOT_AVAILABLE 03-26 22:41:22.156 22223-22223/com.banaao.makersplace.iotsense E/FirebaseInstanceId: Failed to resolve target intent service, skipping classname enforcement 03-26 22:41:22.159 22223-22223/com.banaao.makersplace.iotsense E/FirebaseInstanceId: Error while delivering the message: ServiceIntent not found. 03-26 22:41:36.066 22447-22902/? E/MiPicks-ConnectionRSA: get key exception : com.android.org.bouncycastle.util.encoders.DecoderException: unable to decode base64 string: invalid characters encountered in base64 data

0 个答案:

没有答案