Firebase在特定数据上显示Null

时间:2018-04-03 06:58:48

标签: android firebase firebase-realtime-database

我目前正在做一个应用程序,系统应该自动上传用户驾驶的详细信息,然后firebase将显示该驾驶员的车辆类型等。

我目前收到一个错误,我在没有使用""我使用以下代码修复它

String vehiclecat ="" ;

但现在的问题是使用此代码不会将车辆数据输出到firebase,我只是输出""在firebase上

任何人都可以帮我修复它吗?

 package com.example.muzza.carbon1;

public class HomePage extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener, 
GoogleApiClient.ConnectionCallbacks, 
GoogleApiClient.OnConnectionFailedListener{
TextView textViewMovemnet;
EditText editTextDistance, editTextSpeed, editTextTimeInterval;
Button buttonHistory;

LocationManager locationManager;
GoogleApiClient mLocationClient;
LocationListener mListener;

String vehicle ;
String vehicleCat ;

double lat, lon;

double carbonDioxideVal;

double distance, speed;

double carbonFootprint;

double x1, x2, y1, y2, t1, t2;
double x_Diff, y_Diff, timeInterval;


double total_TimeIntervals = 0;
double total_DistanceMoved = 0;
double avg_Speed = 0;


CountDownTimer timer;
double finishTime;

String userMail;

DatabaseReference dbref;

DatabaseReference dbref_Profile;

double bicycle[] = {10, 15};
double motorcycle[] = {15, 100};
double car[] = {30, 120};
double bus[] = {25, 80};
double truck[] = {25, 40};
double metro[] = {15, 40};



//////////////


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home_page);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    textViewMovemnet = (TextView) findViewById(R.id.textViewMovement);
    editTextDistance = (EditText) findViewById(R.id.editTextDistance);
    editTextSpeed = (EditText) findViewById(R.id.editTextSpeed);
    editTextTimeInterval = (EditText) findViewById(R.id.editTextTimeInterval);

    buttonHistory = (Button) findViewById(R.id.buttonHistory);

    dbref = FirebaseDatabase.getInstance().getReference().child("UserTrackingData");

    dbref_Profile = FirebaseDatabase.getInstance().getReference().child("UserProfileData");


    //GPS Enabled ?
    LocationManager locationManager;
    String context = Context.LOCATION_SERVICE;
    locationManager = (LocationManager) getSystemService(context);

    if (!locationManager.isProviderEnabled( LocationManager.GPS_PROVIDER )) {
        // Build the alert dialog
        android.support.v7.app.AlertDialog.Builder builder = new android.support.v7.app.AlertDialog.Builder(this);
        builder.setTitle("Location Services Not Active");
        builder.setMessage("Please enable Location Services and GPS");
        builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialogInterface, int i) {
                // Show location settings when the user acknowledges the alert dialog
                Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                startActivity(intent);
            }
        });

        builder.setNegativeButton("No", new DialogInterface.OnClickListener(){
            public void onClick(DialogInterface dialogInterface, int i) {
                Toast.makeText(getApplicationContext(), "Cannot use app without GPS", Toast.LENGTH_SHORT).show();
            }
        });
        Dialog alertDialog = builder.create();
        alertDialog.setCanceledOnTouchOutside(false);
        alertDialog.show();
    }


    /// =================================== ///



    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.addDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);


    ///// Retrieve user email from mainActivity
    Bundle emailData = getIntent().getExtras();

    if (emailData == null) {
        return;
    }

    userMail = emailData.getString("userEMail");

    TextView txtUID = (TextView) findViewById(R.id.textViewUId);
    txtUID.setText(userMail);

    /// End retrieve ///



}

//Network available ?
public boolean isNetworkAvailable() {
    ConnectivityManager connectivityManager
            = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}

@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.home_page, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {

        Intent i = new Intent(HomePage.this, UserProfileActivity.class);

        i.putExtra("userEMail", userMail);
        startActivity(i);
        return true;
    }

    return super.onOptionsItemSelected(item);
}

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();
   /*
    if (id == R.id.nav_camera) {
        // Handle the camera action
    } else if (id == R.id.nav_gallery) {

    } else if (id == R.id.nav_slideshow) {

    } else if (id == R.id.nav_manage) {

    } else if (id == R.id.nav_share) {

    } else if (id == R.id.nav_send) {

    }
  */
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}

@Override
public void onConnected(@Nullable Bundle bundle) {
    mListener = new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {
            lat = location.getLatitude();
            lon = location.getLongitude();

            if(String.valueOf(lat).isEmpty()){
                lat = 0;
            }

            if(String.valueOf(lon).isEmpty()){
                lon = 0;
            }

            //    Toast.makeText(HomePage.this, "Location: " + location.getLatitude() + " - " + location.getLongitude(), Toast.LENGTH_SHORT).show();




        }


    };

    LocationRequest request = LocationRequest.create();
    request.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    request.setInterval(4000);
    request.setFastestInterval(1000);
    if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        // TODO: Consider calling
        //    ActivityCompat#requestPermissions
        // here to request the missing permissions, and then overriding
        //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
        //                                          int[] grantResults)
        // to handle the case where the user grants the permission. See the documentation
        // for ActivityCompat#requestPermissions for more details.
        return;
    }
    else {
        LocationServices.FusedLocationApi.requestLocationUpdates(mLocationClient, request, mListener);
    }
}

@Override
public void onConnectionSuspended(int i) {
    return;


}

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
    return;


}

@Override
protected void onPause() {
    super.onPause();
    LocationServices.FusedLocationApi.removeLocationUpdates(mLocationClient, mListener);

}

@Override
protected void onStart() {
    super.onStart();


    firebaseRead(); // call to fetch fm db

}

//Firebase Read//
public void firebaseRead(){

    dbref_Profile.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            for(DataSnapshot ds : dataSnapshot.getChildren()) {

                String emailAdd = ds.child("email").getValue(String.class);
                // int userAge = Integer.parseInt(ds.child("age").getValue(String.class));
                String fname = ds.child("firstname").getValue(String.class);
                String lname = ds.child("lastname").getValue(String.class);
                String vehicleType = ds.child("vehicle").getValue(String.class);
                String vehCat = ds.child("vehicleCateg").getValue(String.class);



              /*  Toast.makeText(HistoryActivity.this, emailAdd + " - " + String.valueOf(avgSpeed) + " - " + String.valueOf(totalDist) + " - " +
                                String.valueOf(totalTime) + " - " + currDate, Toast.LENGTH_SHORT).show(); */

                if(emailAdd.equals(userMail)){
                    //String listStr = emailAdd + "|" + currDate + "|" + totalDist + "|" + avgSpeed + "|" + totalTime;
                    // userDataArr.add(listStr);

                    vehicle = vehicleType;
                    vehicleCat = vehCat;


                    // Toast.makeText(HomePage.this, vehicle + " - " + vehicleCat, Toast.LENGTH_SHORT).show();

                }

            }//



        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            return;

        }
    });

} // End firebaseRead

@Override
protected void onResume() {
    super.onResume();

    /// Location
    mLocationClient = new GoogleApiClient.Builder(this)
            .addApi(LocationServices.API)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();
    mLocationClient.connect();
    /////////



    // Start timer
    timer =   new CountDownTimer(6000, 1000) {
        public void onTick(long millisUntilFinished) {
            if(((millisUntilFinished/1000) == 4)){
                x1 = lat;
                y1 = lon;

                t1 = millisUntilFinished/1000;
            }

            if(((millisUntilFinished/1000) == 1)){
                x2 = lat;
                y2 = lon;

                t2 = millisUntilFinished/1000;

                //timeInterval = t2 - t1;



                BigDecimal bd1 = new BigDecimal(x1);
                bd1 = bd1.round(new MathContext(7));
                x1 = bd1.doubleValue();

                BigDecimal bd2 = new BigDecimal(x2);
                bd2 = bd2.round(new MathContext(7));
                x2 = bd2.doubleValue();

                BigDecimal bd3 = new BigDecimal(y1);
                bd3 = bd3.round(new MathContext(7));
                y1 = bd3.doubleValue();

                BigDecimal bd4 = new BigDecimal(y2);
                bd4 = bd4.round(new MathContext(7));
                y2 = bd4.doubleValue();


                x_Diff = Math.abs(x2) - Math.abs(x1);
                x_Diff = Math.abs(x_Diff);

                y_Diff = Math.abs(y2) - Math.abs(y1);
                y_Diff = Math.abs(y_Diff);

                // Toast.makeText(getApplicationContext(), "x_Diff: " + x_Diff  + ";  y-diff: " + y_Diff, Toast.LENGTH_SHORT).show();

                // User Vehicle info

                // CAR
                if(vehicle.equals("Car") && vehicleCat.equals("Small")){
                    carbonDioxideVal = 0.14545;
                }

                if(vehicle.equals("Car") && vehicleCat.equals("Medium")){
                    carbonDioxideVal = 0.1738;
                }

                if(vehicle.equals("Car") && vehicleCat.equals("Average")){
                    carbonDioxideVal = 0.17887;
                }

                if(vehicle.equals("Car") && vehicleCat.equals("Large")){
                    carbonDioxideVal = 0.21834;
                }
                // END CAR /////////////

                // Van //
                if(vehicle.equals("Van") && vehicleCat.equals("Small")){
                    carbonDioxideVal = 0.14545;
                }

                if(vehicle.equals("Van") && vehicleCat.equals("Medium")){
                    carbonDioxideVal = 0.1738;
                }

                if(vehicle.equals("Van") && vehicleCat.equals("Average")){
                    carbonDioxideVal = 0.17887;
                }

                if(vehicle.equals("Van") && vehicleCat.equals("Large")){
                    carbonDioxideVal = 0.21834;
                }
                // END Van ////////////////////

                // Motorbike //
                if(vehicle.equals("Motorbike") && vehicleCat.equals("Small")){
                    carbonDioxideVal = 0.08474;
                }

                if(vehicle.equals("Motorbike") && vehicleCat.equals("Medium")){
                    carbonDioxideVal = 0.10323;
                }

                if(vehicle.equals("Motorbike") && vehicleCat.equals("Average")){
                    carbonDioxideVal = 0.11662;
                }

                if(vehicle.equals("Motorbike") && vehicleCat.equals("Large")){
                    carbonDioxideVal = 0.13542;
                }
                // END Motorbike //////////////////

                // End User Vehicle info



                distance = Math.sqrt(Math.pow((x2 - x1),2) + Math.pow((y2 - y1), 2));


                carbonFootprint = distance * carbonDioxideVal;

                speed = distance / 3;

                // Public transport
                if((speed >= 4.5) && (speed <= 8)){
                    vehicle = "Bus";
                    vehicleCat = "Long vehicle";
                }

                if((speed >= 8.5) && (speed <= 12.5)){
                    vehicle = "Metro";
                    vehicleCat = "Long vehicle";
                }

                // End Public transport

                timeInterval = t2 - t1;
                timeInterval = Math.abs(timeInterval);

                editTextDistance.setText(String.valueOf(distance));
                editTextSpeed.setText(String.valueOf(speed));
                editTextTimeInterval.setText(String.valueOf(timeInterval));




                if((speed == 0.0 || speed < 0.00005)){

                    //// Current time ////
                    DateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
                    Date today = Calendar.getInstance().getTime();
                    String reportDate = df.format(today);

                    ///

                    textViewMovemnet.setText("still");

                    avg_Speed = total_DistanceMoved / total_TimeIntervals;

                    UserTrackData userData = new UserTrackData(userMail, reportDate, String.valueOf(total_DistanceMoved), String.valueOf(total_TimeIntervals), String.valueOf(avg_Speed), String.valueOf(carbonFootprint), vehicle, vehicleCat);

                    if(String.valueOf(total_DistanceMoved).equals("0.0") || String.valueOf(total_TimeIntervals).equals("0.0") || String.valueOf(avg_Speed).equals("NaN")){
                        return;
                    }
                    dbref.push().setValue(userData);



                      /*  if 
(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) || 
!userMail.isEmpty() || !reportDate.isEmpty()
                                || 
!String.valueOf(total_DistanceMoved).isEmpty() || 
!String.valueOf(total_TimeIntervals).isEmpty() ||
                                !String.valueOf(avg_Speed).isEmpty() || 
!String.valueOf(carbonFootprint).isEmpty() || !vehicle.isEmpty() ||
                                !vehicleCat.isEmpty()){


                            dbref.push().setValue(userData);

                        }
                        else {
                            return;
                        } */


                    //  Toast.makeText(HomePage.this, "final: " + 
total_DistanceMoved + " ; " + total_TimeIntervals + " ; " + avg_Speed, 
Toast.LENGTH_SHORT).show();

                    avg_Speed = total_DistanceMoved = total_TimeIntervals = 
0;



                }
                else {


                    textViewMovemnet.setText("moving");

                    total_DistanceMoved += distance;
                    total_TimeIntervals += timeInterval;
                }

            }
            finishTime = (millisUntilFinished/1000);
        }

        public void onFinish() {
            //Toast.makeText(getApplicationContext(), "Receive done" + 
finishTime, Toast.LENGTH_SHORT).show();
            this.start();
        }
    }.start();

    Toast.makeText(getApplicationContext(), "Tracking started", Toast.LENGTH_SHORT).show();



}





public void viewHistory(View view){
    Intent i = new Intent(HomePage.this, HistoryActivity.class);
    i.putExtra("userEMailAddr", userMail);
    startActivity(i);
}


}

我在Android工作室遇到的错误是

E/UncaughtException: java.lang.NullPointerException: Attempt to invoke 
virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null 
object reference
                     at com.example.muzza.carbon1.HomePage$5.onTick(HomePage.java:447)
                     at android.os.CountDownTimer$1.handleMessage(CountDownTimer.java:133)
                     at android.os.Handler.dispatchMessage(Handler.java:102)
                     at android.os.Looper.loop(Looper.java:135)
                     at android.app.ActivityThread.main(ActivityThread.java:5601)
                     at java.lang.reflect.Method.invoke(Native Method)
                     at java.lang.reflect.Method.invoke(Method.java:372)
                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:964)
                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:759)

我通过添加更改以下行来修复错误

String vehicle ; -------------- String vehicle = "" ;
String vehicleCat ; --------------- String vehicleCat="";

但现在的问题是我的firebase没有显示我想要的数据

在我保存firebase之前,我得到了这个 As can be seen, vehicle and vehiclecategory was available on firebase 现在我改变了线路字符串vehiclecat和String cat后,现在我无法在firebase上获得这样的数据

No data of vehicle and vehicle category

任何人都可以帮我解决这个问题吗?

这是另一个java文件UserTrackData.java

package com.example.muzza.carbon1;

/**
 * Created by muzza on 20-Mar-18.
*/


public class UserTrackData {

String email;
String currentDate;
String totalDistance;
String totalTime;
String avgSpeed;
String carbonFootprint;
String vahicle;
String vehicleCateg;


public UserTrackData(String email, String currentDate, String totalDistance, String totalTime, String avgSpeed, String carbonFootprint, String vahicle, String vehicleCateg) {
    this.email = email;
    this.currentDate = currentDate;
    this.totalDistance = totalDistance;
    this.totalTime = totalTime;
    this.avgSpeed = avgSpeed;
    this.carbonFootprint = carbonFootprint;
    this.vahicle = vahicle;
    this.vehicleCateg = vehicleCateg;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public String getCurrentDate() {
    return currentDate;
}

public void setCurrentDate(String currentDate) {
    this.currentDate = currentDate;
}

public String getTotalDistance() {
    return totalDistance;
}

public void setTotalDistance(String totalDistance) {
    this.totalDistance = totalDistance;
}

public String getTotalTime() {
    return totalTime;
}

public void setTotalTime(String totalTime) {
    this.totalTime = totalTime;
}

public String getAvgSpeed() {
    return avgSpeed;
}

public void setAvgSpeed(String avgSpeed) {
    this.avgSpeed = avgSpeed;
}

public String getCarbonFootprint() {
    return carbonFootprint;
}

public void setCarbonFootprint(String carbonFootprint) {
    this.carbonFootprint = carbonFootprint;
}

public String getVahicle() {
    return vahicle;
}

public void setVahicle(String vahicle) {
    this.vahicle = vahicle;
}

public String getVehicleCateg() {
    return vehicleCateg;
}

public void setVehicleCateg(String vehicleCateg) {
    this.vehicleCateg = vehicleCateg;
}
}

0 个答案:

没有答案