gpt4 book ai didi

android - 从 firebase 数据库检索数据的问题

转载 作者:行者123 更新时间:2023-11-30 00:30:39 24 4
gpt4 key购买 nike

我计划使用 putExtra 将数据从 NearbyActivity (MapsActivity) 传递到 DetailsActivity。但是我遇到了问题,当我启动 NearbyActivity 时,它关闭了。当我检查 NearbyActivity 中存在空指针错误时。你能告诉我哪里出了问题吗?

附近 Activity

  package com.example.rizkafs.laundrize;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.util.Log;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptor;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MapStyleOptions;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

import java.util.ArrayList;
import java.util.HashMap;


public class NearbyActivity extends FragmentActivity implements OnMapReadyCallback {

private static final String TAG = NearbyActivity.class.getSimpleName();
public HashMap<Marker, DataLaundry> detailMarkerMap;

String latitude;
String longitude;
String namaTempat;
String alamat;
String jenis;
String harga;
String jamOperasi;
String noTelp;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_nearby);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}

@Override
public void onMapReady(final GoogleMap googleMap) {

try {
// Customise the styling of the base map using a JSON object defined
// in a raw resource file.
boolean success = googleMap.setMapStyle(
MapStyleOptions.loadRawResourceStyle(
this, R.raw.style_json));

if (!success) {
Log.e(TAG, "Style parsing failed.");
}
} catch (Resources.NotFoundException e) {
Log.e(TAG, "Can't find style. Error: ", e);
}

googleMap.setMyLocationEnabled(true);


DatabaseReference myRef = FirebaseDatabase.getInstance().getReference();

myRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot child : dataSnapshot.child("places").getChildren()){
latitude = child.child("latitude").getValue(String.class);
longitude = child.child("longitude").getValue(String.class);
namaTempat = child.child("nama").getValue(String.class);
alamat = child.child("alamat").getValue(String.class);
jenis = child.child("jenis").getValue(String.class);
harga = child.child("harga").getValue(String.class);
jamOperasi = child.child("jam_operasional").getValue(String.class);
noTelp = child.child("no_telp").getValue(String.class);

DataLaundry dataLaundry = new DataLaundry(latitude, longitude, namaTempat, alamat, jenis, harga, jamOperasi, noTelp);

LatLng latLng = new LatLng(Double.parseDouble(latitude), Double.parseDouble(longitude));
Marker marker = googleMap.addMarker(new MarkerOptions()
.position(latLng)
.title(namaTempat)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker))
.snippet(jenis));

detailMarkerMap.put(marker, dataLaundry);
}
}

@Override
public void onCancelled(DatabaseError databaseError) {

}
});

LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();

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;
}
Location location = locationManager.getLastKnownLocation(locationManager.getBestProvider(criteria, false));
if(location != null) {
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(location.getLatitude(), location.getLongitude()))
.zoom(17)
.build();
googleMap.moveCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}

googleMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
@Override
public void onInfoWindowClick(Marker marker) {
marker.getTitle();

DataLaundry dataLaundry = detailMarkerMap.get(marker);

Intent i = new Intent(NearbyActivity.this, DetailsActivity.class);
i.putExtra("nama", dataLaundry.getNama());
i.putExtra("jenis", dataLaundry.getJenis());
i.putExtra("alamat", dataLaundry.getAlamat());
i.putExtra("harga", dataLaundry.getHarga());
i.putExtra("jam_operasi", dataLaundry.getJamOperasi());
i.putExtra("no_telp", dataLaundry.getNoTelp());
startActivity(i);
}
});
}
}

DataLaundry.java

 package com.example.rizkafs.laundrize;

import com.google.android.gms.maps.model.LatLng;

/**
* Created by Abi FS on 6/3/2017.
*/

public class DataLaundry {

String nama;
String alamat;
String jenis;
String harga;
String jamOperasi;
String no_telp;
String latitude;
String longitude;

public DataLaundry() {
}

public DataLaundry(String latitude, String longitude, String nama, String alamat, String jenis, String harga, String jamOperasi, String no_telp) {
this.latitude = latitude;
this.longitude = longitude;
this.nama = nama;
this.alamat = alamat;
this.jenis = jenis;
this.harga = harga;
this.jamOperasi = jamOperasi;
this.no_telp = no_telp;
}

public String getLatitude() {
return latitude;
}

public void setLatitude(String latitude) {
this.latitude = latitude;
}

public String getLongitude() {
return longitude;
}

public void setLongitude(String longitude) {
this.longitude = longitude;
}

public String getNama() {
return nama;
}

public void setNama(String nama) {
this.nama = nama;
}

public String getAlamat() {
return alamat;
}

public void setAlamat(String alamat) {
this.alamat = alamat;
}

public String getJenis() {
return jenis;
}

public void setJenis(String jenis) {
this.jenis = jenis;
}

public String getHarga() {
return harga;
}

public void setHarga(String harga) {
this.harga = harga;
}

public String getJamOperasi() {
return jamOperasi;
}

public void setJamOperasi(String jamOperasi) {
this.jamOperasi = jamOperasi;
}

public String getNoTelp() {
return no_telp;
}

public void setNoTelp(String no_telp) {
this.no_telp = no_telp;
}
}

详细 Activity

   package com.example.rizkafs.laundrize;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;

public class DetailsActivity extends AppCompatActivity {
TextView alamat, jenis, harga, jamOperasi, noTelp;

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

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});

alamat = (TextView) findViewById(R.id.detail_alamat);
jenis = (TextView) findViewById(R.id.detail_jenis);
harga = (TextView) findViewById(R.id.detail_harga);
jamOperasi = (TextView) findViewById(R.id.detail_jam);
noTelp = (TextView) findViewById(R.id.detail_notelp);

setTitle(getIntent().getStringExtra("nama"));
alamat.setText(getIntent().getStringExtra("alamat"));
jenis.setText(getIntent().getStringExtra("jenis"));
harga.setText(getIntent().getStringExtra("harga"));
jamOperasi.setText(getIntent().getStringExtra("jam_operasi"));
noTelp.setText(getIntent().getStringExtra("no_telp"));
}
}

Android Monitor 错误

    java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object java.util.HashMap.put(java.lang.Object, java.lang.Object)' on a null object reference
at com.example.rizkafs.laundrize.NearbyActivity$1.onDataChange(NearbyActivity.java:108)
at com.google.firebase.database.Query$1.onDataChange(Unknown Source)
at com.google.android.gms.internal.zzbpx.zza(Unknown Source)
at com.google.android.gms.internal.zzbqx.zzZV(Unknown Source)
at com.google.android.gms.internal.zzbra$1.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5847)
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:1010)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)

最佳答案

您必须初始化 HashMap<Marker, DataLaundry>首先,然后您可以向其中添加值。像这样尝试

HashMap<Marker, DataLaundry> detailMarkerMap = new HashMap<>();

然后使用 detailMarkerMap.put(marker, dataLaundry);onDataChange(DataSnapshot dataSnapshot)里面

关于android - 从 firebase 数据库检索数据的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44459075/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com