每当我尝试在从 firebase 检索到的地图上显示位置时,都会出现错误 [重复]

Posted

技术标签:

【中文标题】每当我尝试在从 firebase 检索到的地图上显示位置时,都会出现错误 [重复]【英文标题】:I get a error whenever I try to show a location on the map retrieved from firebase [duplicate] 【发布时间】:2019-12-17 13:16:29 【问题描述】:

当我撤消所有操作时,应用程序不会崩溃,我确定代码有问题。请帮帮我。

主要活动 - 用户选择他们想要跟踪的 BUS 并将字符串发送到地图活动

    private
    RadioGroup route;
    private FirebaseAuth mAuth;
    private FirebaseDatabase mDatabase = FirebaseDatabase.getInstance();
    private DatabaseReference mDatabaseReference = mDatabase.getReference().child("Locations");
    public String someVariable;
    TextView numberbs;
    Button getLocationBtn;
    private Button btnSignOut;
    TextView locationText;
    Button stoptracking;
    Button busb1;
    Button busb2;
    Button busb3;
    Button busb4;
    Button busb5;
    Button busb6;
    Button busb7;
    Button busb8;
    Button busb9;
    Button busb10;
    Button busb11;
    Button busb12;
    Button busb13;
    Button busb14;
    Button busb15;
    Button busb16;
    TextView statusi;


    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        numberbs = findViewById(R.id.busnotext);
        btnSignOut = findViewById(R.id.signoutbtn);
        getLocationBtn = findViewById(R.id.getLocationBtn);


        numberbs = findViewById(R.id.busnotext);
        busb1 = findViewById(R.id.busbtn1);
        busb2 = findViewById(R.id.busbtn2);
        busb3 = findViewById(R.id.busbtn3);
        busb4 = findViewById(R.id.busbtn4);
        busb5 = findViewById(R.id.busbtn5);
        busb6 = findViewById(R.id.busbtn6);
        busb7 = findViewById(R.id.busbtn7);
        busb8 = findViewById(R.id.bustbtn8);
        busb9 = findViewById(R.id.busbtn9);
        busb10 = findViewById(R.id.busbtn10);
        busb11 = findViewById(R.id.busbtn11);
        busb12 = findViewById(R.id.busbtn12);
        busb13 = findViewById(R.id.busbtn13);
        busb14 = findViewById(R.id.busbtn14);
        busb15 = findViewById(R.id.bustbtn15);
        busb16 = findViewById(R.id.busbtn16);

        busb1.setOnClickListener(this);
        busb2.setOnClickListener(this);
        busb3.setOnClickListener(this);
        busb4.setOnClickListener(this);
        busb5.setOnClickListener(this);
        busb6.setOnClickListener(this);
        busb7.setOnClickListener(this);
        busb8.setOnClickListener(this);
        busb9.setOnClickListener(this);
        busb10.setOnClickListener(this);
        busb11.setOnClickListener(this);
        busb12.setOnClickListener(this);
        busb13.setOnClickListener(this);
        busb14.setOnClickListener(this);
        busb15.setOnClickListener(this);
        busb16.setOnClickListener(this);

        getLocationBtn.setOnClickListener(new View.OnClickListener() 

            @Override
            public void onClick(View v) 

                //startService(new Intent(this, MyService.class));
                Intent intent = new Intent(MainActivity.this, MapsActivity.class);
                intent.putExtra("message", someVariable);
                startActivity(intent);
            
        );

/**
        Intent intent = new Intent(MainActivity.this, MapsActivity.class);
        intent.putExtra("Ref", someVariable);
        startActivity(intent);
 */
    

    private void toastMessage(String message) 
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    

    @Override
    public void onClick(View v) 
        switch (v.getId()) 
            case R.id.busbtn1:
               someVariable ="BUS1";
                numberbs.setText("Selected: BUS 1");
                mDatabaseReference = mDatabase.getReference().child("Locations").child("BUS1");
                break;
            case R.id.busbtn2:
                numberbs.setText("Selected: BUS 2");
                someVariable ="BUS2";
                mDatabaseReference = mDatabase.getReference().child("Locations").child("BUS2");
                break;
            case R.id.busbtn3:
                numberbs.setText("Selected: BUS 3");
                someVariable ="BUS3";
                mDatabaseReference = mDatabase.getReference().child("Locations").child("BUS3");
                break;
            case R.id.busbtn4:
                numberbs.setText("Selected: BUS 4");
                someVariable ="BUS4";
                mDatabaseReference = mDatabase.getReference().child("Locations").child("BUS4");
                break;
            case R.id.busbtn5:
                numberbs.setText("Selected: BUS 5");
                someVariable ="BUS5";
                mDatabaseReference = mDatabase.getReference().child("Locations").child("BUS5");
                break;
            case R.id.busbtn6:
                numberbs.setText("Selected: BUS 6");
                someVariable ="BUS6";
                mDatabaseReference = mDatabase.getReference().child("Locations").child("BUS6");
                break;
            case R.id.busbtn7:
                numberbs.setText("Selected: BUS 7");
                someVariable ="BUS7";
                mDatabaseReference = mDatabase.getReference().child("Locations").child("BUS7");
                break;
            case R.id.bustbtn8:
                numberbs.setText("Selected: BUS 8");
                someVariable ="BUS8";
                mDatabaseReference = mDatabase.getReference().child("Locations").child("BUS8");
                break;
            case R.id.busbtn9:
                numberbs.setText("Selected: BUS 9");
                someVariable ="BUS9";
                mDatabaseReference = mDatabase.getReference().child("Locations").child("BUS9");
                break;
            case R.id.busbtn10:
                numberbs.setText("Selected: BUS 10");
                someVariable ="BUS10";
                mDatabaseReference = mDatabase.getReference().child("Locations").child("BUS10");
                break;
            case R.id.busbtn11:
                numberbs.setText("Selected: BUS 11");
                someVariable ="BUS11";
                mDatabaseReference = mDatabase.getReference().child("Locations").child("BUS11");
                break;
            case R.id.busbtn12:
                numberbs.setText("Selected: BUS 12");
                someVariable ="BUS12";
                mDatabaseReference = mDatabase.getReference().child("Locations").child("BUS12");
                break;
            case R.id.busbtn13:
                numberbs.setText("Selected: BUS 13");
                someVariable ="BUS13";
                mDatabaseReference = mDatabase.getReference().child("Locations").child("BUS13");
                break;
            case R.id.busbtn14:
                someVariable ="BUS14";
                numberbs.setText("Selected: BUS 14");
                mDatabaseReference = mDatabase.getReference().child("Locations").child("BUS14");
                break;
            case R.id.bustbtn15:
                numberbs.setText("Selected: BUS 15");
                someVariable ="BUS15";
                mDatabaseReference = mDatabase.getReference().child("Locations").child("BUS15");
                break;
            case R.id.busbtn16:
                numberbs.setText("Selected: BUS 16");
                someVariable ="BUS16";
                mDatabaseReference = mDatabase.getReference().child("Locations").child("BUS16");
                break;
        
    `



MAPS ACTIVITY - 从 firebase 检索位置并将其显示在地图上


    private GoogleMap mMap;
    private FirebaseAuth mAuth;
    String value_latitude;
    String value_longitue;
    double longi;
    double lati;
    Button Refreshh;
    String valie;
    Button Backkk;
    private FirebaseDatabase mDatabase = FirebaseDatabase.getInstance();
    private DatabaseReference mDatabaseReference = mDatabase.getReference().child("Locations");

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        Intent intent = getIntent();
        String message = intent.getStringExtra("message");
        Refreshh = findViewById(R.id.refresh);
        Backkk = findViewById(R.id.backbtn);
        mDatabaseReference = mDatabase.getReference().child("Locations").child(valie);
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        valie = message;
        lati = Double.valueOf(value_latitude);
        longi = Double.valueOf(value_longitue);


        mDatabaseReference.addValueEventListener(new ValueEventListener() 
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) 
                for (DataSnapshot ds : dataSnapshot.getChildren())
                    String value_latitude = ds.child("latitude").getValue().toString();
                    String value_longitue = ds.child("longitude").getValue().toString();


                
            

            @Override
            public void onCancelled(DatabaseError databaseError) 


            
        );

        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
        /**
        Backkk.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View view) 


                Intent intent = new Intent(MapsActivity.this, MainActivity.class);
                startActivity(intent);
            
        );

      Refreshh.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View view) 
                mMap.clear();
                lati = Double.valueOf(value_latitude);
                longi = Double.valueOf(value_longitue);


                mDatabaseReference.addValueEventListener(new ValueEventListener() 
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) 
                        for (DataSnapshot ds : dataSnapshot.getChildren())
                            String value_latitude = ds.child("latitude").getValue().toString();
                            String value_longitue = ds.child("longitude").getValue().toString();

                        
                    

                    @Override
                    public void onCancelled(DatabaseError databaseError) 


                    
                );
                LatLng sydney = new LatLng(lati, longi);
                mMap.addMarker(new MarkerOptions().position(sydney).title(valie));
                mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
            
        );
         */
         


    /**
     * Manipulates the map once available.
     * This callback is triggered when the map is ready to be used.
     * This is where we can add markers or lines, add listeners or move the camera. In this case,
     * we just add a marker near Sydney, Australia.
     * If Google Play services is not installed on the device, the user will be prompted to install
     * it inside the SupportMapFragment. This method will only be triggered once the user has
     * installed Google Play services and returned to the app.
     */
    @Override
    public void onMapReady(GoogleMap googleMap) 
        mMap = googleMap;


        // Add a marker in Sydney and move the camera

          LatLng sydney = new LatLng(lati, longi);
         mMap.addMarker(new MarkerOptions().position(sydney).title(valie));
         mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));

    

我的 LOGCAT -

19-08-10 00:34:43.912 24741-24741/com.manavchordia.aplparent E/androidRuntime: FATAL EXCEPTION: main
    Process: com.manavchordia.aplparent, PID: 24741
    java.lang.RuntimeException: Unable to start activity ComponentInfocom.manavchordia.aplparent/com.manavchordia.aplparent.MapsActivity: java.lang.NullPointerException: Can't pass null for argument 'pathString' in child()
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3093)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3228)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1948)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6954)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:537)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
     Caused by: java.lang.NullPointerException: Can't pass null for argument 'pathString' in child()
        at com.google.firebase.database.DatabaseReference.child(com.google.firebase:firebase-database@@18.0.1:101)
        at com.manavchordia.aplparent.MapsActivity.onCreate(MapsActivity.java:48)
        at android.app.Activity.performCreate(Activity.java:7155)
        at android.app.Activity.performCreate(Activity.java:7146)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1288)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3068)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3228) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1948) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:193) 
        at android.app.ActivityThread.main(ActivityThread.java:6954) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:537) 

【问题讨论】:

【参考方案1】:

错误消息表明您将 null 传递给对 child() 的调用之一。可能是这个,因为您直到稍后才为valie 赋值:

mDatabaseReference = mDatabase.getReference().child("Locations").child(valie);

在像这样使用它之前先将valie分配给一个非空值。

【讨论】:

我该怎么做?【参考方案2】:

您需要在 valie 变量中设置值,例如 mDatabaseReference = mDatabase.getReference().child("Locations").child(valie).setValue(in_here_which_you_want_to_Save_in_Db);

【讨论】:

这不是正确的答案,仍然会导致同样的问题,你需要为变量valie赋值,而不是使用setValue 在您声明它时或在查询中使用之前分配valie的值

以上是关于每当我尝试在从 firebase 检索到的地图上显示位置时,都会出现错误 [重复]的主要内容,如果未能解决你的问题,请参考以下文章

从 Firebase 检索位置并将标记放在谷歌地图上

Firebase/Android:将检索到的值从 Firebase 添加到 arraylist 会返回空指针异常

试图快速删除从firebase检索到的数组中的重复值

如何从android中的firebase中检索List对象

android - 比较两个非常大的 ArrayList,其中一个是从 firebase 检索到的

if 语句在从 firebase 检索数据时未返回正确的元素