Android 按下按钮会导致无法解释的';重定向';在firebase中使用数据库时

Android 按下按钮会导致无法解释的';重定向';在firebase中使用数据库时,android,firebase-realtime-database,Android,Firebase Realtime Database,我在一个Android应用程序上工作,被一个按钮的无法解释的效果卡住了 这涉及到三个活动:(您可以在pastebin上找到完整的代码) TripListActivity.java //removed imports due to body limitation at 30000 chas public class TripListActivity extends AppCompatActivity { @BindView(R.id.rlvTrips) Recycler

我在一个Android应用程序上工作,被一个按钮的无法解释的效果卡住了

这涉及到三个活动:(您可以在pastebin上找到完整的代码)

TripListActivity.java

//removed imports due to body limitation at 30000 chas
public class TripListActivity extends AppCompatActivity {

    @BindView(R.id.rlvTrips)
    RecyclerView rlvTrips;

    private DatabaseReference databaseReference;
    private FirebaseAuth firebaseAuth;
    private FirebaseStorage firebaseStorage;

    private List<Trip> recentTrips;
    private List<Trip> pastTrips;
    private List<StorageReference> imageRefsRecent;
    private List<StorageReference> imageRefsPast;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //create instance of firebase auth
        firebaseAuth = FirebaseAuth.getInstance();

        //create instance of firebase storage
        firebaseStorage = FirebaseStorage.getInstance();

        //create instance of firebase database
        databaseReference = FirebaseDatabase.getInstance().getReference();

        recentTrips = new ArrayList<>();
        pastTrips = new ArrayList<>();
        imageRefsRecent = new ArrayList<>();
        imageRefsPast = new ArrayList<>();

        getAllTrips();
    }

    private void getAllTrips() {
        final Date currentDate = new Date();
        final long currentTime = currentDate.getTime();
        databaseReference.child("users/" + firebaseAuth.getCurrentUser().getUid() + "/").addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                DataSnapshot tripsDataSnapshot = dataSnapshot.child("trips");
                for (DataSnapshot tripDataSnapshot : tripsDataSnapshot.getChildren()) {
                    Trip trip = new Trip();

                    trip.setTitle((String) tripDataSnapshot.child("title").getValue());
                    trip.setDescription((String) tripDataSnapshot.child("description").getValue());


                    DataSnapshot dateDataSnapshot = tripDataSnapshot.child("date");
                    Date date = new Date();
                    if (dateDataSnapshot.child("time").getValue() != null) {
                        date.setTime((Long) dateDataSnapshot.child("time").getValue());
                    }
                    trip.setDate(date);

                    DataSnapshot imagesDataSnapshot = tripDataSnapshot.child("images");
                    List<String> imageList = new ArrayList<>();
                    for (int i = 1; i <= imagesDataSnapshot.getChildrenCount(); i++) {
                        imageList.add(String.valueOf(imagesDataSnapshot.child("img" + i).getValue()));
                    }
                    trip.setImages(imageList);

                    DataSnapshot placesDataSnapshot = tripDataSnapshot.child("places");
                    List<Place> placeList = new ArrayList<>();
                    for (int i = 0; i < placesDataSnapshot.getChildrenCount(); i++) {
                        Place place = new Place();
                        place.setLat((String) placesDataSnapshot.child(String.valueOf(i)).child("lat").getValue());
                        place.setLng((String) placesDataSnapshot.child(String.valueOf(i)).child("lng").getValue());
                        placeList.add(place);
                    }
                    trip.setPlaces(placeList);

                    Log.d(TripListActivity.class.getSimpleName(), "Trip date = " + date.getTime() + "   current time = " + currentTime);
                    if (currentTime - date.getTime() <= SEVEN_DAYS_IN_MILISECONDS) {
                        recentTrips.add(trip);
                        //get first image form each trip
                        imageRefsRecent.add(firebaseStorage.getReferenceFromUrl(imageList.get(0)));
                    } else {
                        pastTrips.add(trip);
                        //get first image form each trip
                        imageRefsPast.add(firebaseStorage.getReferenceFromUrl(imageList.get(0)));
                    }

                }

                provideRecentTripsUI();
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
                Log.d("-----Error-----", databaseError.getMessage());
            }
        });
    }

    private void populateTripList(final List<Trip> tripList, List<StorageReference> imageRefs) {
        TripAdapter tripAdapter = new TripAdapter(tripList, imageRefs, getApplicationContext());

        RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getApplicationContext(), 2);
        //set on item click listener
        tripAdapter.setItemClickListener(new TripAdapter.ItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {

                SharedPreferences.Editor sharedPreferencesEditor = getSharedPreferences(SHARED_PREFERENCES, MODE_PRIVATE).edit();
                sharedPreferencesEditor.putString(TRIP_CLICKED_TITLE, tripList.get(position).getTitle());
                sharedPreferencesEditor.putString(TRIP_CLICKED_DESCRIPTION, tripList.get(position).getDescription());
                sharedPreferencesEditor.apply();

                Intent tripDetailIntent = new Intent(TripListActivity.this, TripDetailActivity.class);
                tripDetailIntent.putExtra("tripClicked", tripList.get(position));
                tripDetailIntent.putExtra("tripId", position + 1);
                tripDetailIntent.putExtra("userUID", firebaseAuth.getCurrentUser().getUid());
                startActivity(tripDetailIntent);
            }
        });
        rlvTrips.setLayoutManager(layoutManager);
        rlvTrips.setItemAnimator(new DefaultItemAnimator());
        rlvTrips.setAdapter(tripAdapter);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        switch (id) {
            case R.id.recentTrips:
                provideRecentTripsUI();
                return true;
            case R.id.pastTrips:
                providePastTripsUI();
                return true;
            case R.id.addTrip:
                Intent intent = new Intent(this, TripAdderActivity.class);
                startActivity(intent);
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    private void provideRecentTripsUI() {
        if (recentTrips.size() != 0) {
            setContentView(R.layout.activity_trip_list);
            ButterKnife.bind(TripListActivity.this);
            populateTripList(recentTrips, imageRefsRecent);
        } else {
            setContentView(R.layout.no_recent_trips_layout);
        }
    }

    public void allTripsMode(View view) {
        providePastTripsUI();
    }

    private void providePastTripsUI() {
        setContentView(R.layout.activity_trip_list);
        ButterKnife.bind(TripListActivity.this);
        populateTripList(pastTrips, imageRefsPast);
        if (pastTrips.size() == 0) {
            ToastUtil.showToast("No past trips!", this);
        }
    }

}
//removed imports due to body limitation at 30000 chas
public class TripAdderActivity extends AppCompatActivity {


    @BindView(R.id.etTitle)
    EditText etTitle;
    @BindView(R.id.etDescription)
    EditText etDescription;
    @BindView(R.id.lvMedia)
    ListView lvMedia;

    private FirebaseAuth firebaseAuth;
    private FirebaseStorage firebaseStorage;
    private DatabaseReference databaseReference;

    private ArrayList<Uri> imageURIs;
    private Trip trip;
    private Date date;
    private long tripId;

    public static final int PICK_IMAGE_REQUEST = 1;
    private String imageEncoded;
    private List<String> imagesEncodedList;

    static boolean placesAdded = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_trip_adder);

        //bind views
        ButterKnife.bind(this);

        trip = new Trip();
        imageURIs = new ArrayList<>();

        //get current time
        date = Calendar.getInstance().getTime();

        //create instance of firebase auth
        firebaseAuth = FirebaseAuth.getInstance();

        //create instance of firebase storage
        firebaseStorage = FirebaseStorage.getInstance();

        //get database reference
        databaseReference = FirebaseDatabase.getInstance().getReference();

        //read number of trips from the database
        databaseReference.child("users/" + firebaseAuth.getCurrentUser().getUid() + "/").addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                tripId = (long) dataSnapshot.child("tripNumber").getValue();
            }

            @Override
            public void onCancelled(DatabaseError error) {
                // Failed to read value
                Log.w(TripListActivity.class.getSimpleName(), "Failed to read trip.");
            }
        });
    }

    /**
     * @param view This method sends the user to a MapActivity.
     */
    public void addPlace(View view) {
        Intent intent = new Intent(this, MapsAdderActivity.class);
        intent.putExtra("tripId", tripId);
        startActivity(intent);
    }

    /**
     * @param view This method uses an intent to allow the user to pick images that he wants to add to the Trip object
     *             and stores the images in firebase storage.
     */
    public void addMedia(View view) {
        (new AddImagesTask() {
            @Override
            protected void onPreExecute() {
                super.onPreExecute();

            }

            @Override
            protected void onPostExecute(Void aVoid) {
                super.onPostExecute(aVoid);
            }
        }).execute();


        Intent intent = new Intent();
        intent.setType("image/*");
        intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        try {
            // When an Image is picked
            if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK
                    && null != data) {
                // Get the Image from data

                String[] filePathColumn = {MediaStore.Images.Media.DATA};
                imagesEncodedList = new ArrayList<String>();
                if (data.getData() != null) {

                    Uri mImageUri = data.getData();

                    // Get the cursor
                    Cursor cursor = getContentResolver().query(mImageUri,
                            filePathColumn, null, null, null);
                    // Move to first row
                    cursor.moveToFirst();

                    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                    imageEncoded = cursor.getString(columnIndex);
                    cursor.close();

                } else {
                    if (data.getClipData() != null) {
                        ClipData mClipData = data.getClipData();
                        ArrayList<Uri> mArrayUri = new ArrayList<Uri>();
                        for (int i = 0; i < mClipData.getItemCount(); i++) {

                            ClipData.Item item = mClipData.getItemAt(i);
                            Uri uri = item.getUri();
                            mArrayUri.add(uri);
                            // Get the cursor
                            Cursor cursor = getContentResolver().query(uri, filePathColumn, null, null, null);
                            // Move to first row
                            cursor.moveToFirst();

                            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                            imageEncoded = cursor.getString(columnIndex);
                            imagesEncodedList.add(imageEncoded);
                            cursor.close();

                        }
                        Log.v("LOG_TAG", "Selected Images" + mArrayUri.size());

                        imageURIs = mArrayUri;

                        uploadImagesToFirebase();
                    }
                }
            } else {
                ToastUtil.showToast("You haven't picked Image", this);
            }
        } catch (Exception e) {
            ToastUtil.showToast("Something went wrong", this);
        }

        super.onActivityResult(requestCode, resultCode, data);
    }

    /**
     * This method is used to upload images to Firebase Storage.
     */
    private void uploadImagesToFirebase() {
        //create storage reference from our app
        //points to the root reference
        StorageReference storageReference = firebaseStorage.getReference();
        //create storage reference for user folder
        //points to the trip folder
        StorageReference userReference = storageReference.child("user/" + firebaseAuth.getCurrentUser().getUid()).child("trips").child("trip" + tripId);
        StorageReference imageReference;
        UploadTask uploadTask;

        //array list used to store images paths
        final ArrayList<String> strings = new ArrayList<>();
        int i = 0;
        for (Uri imageURI : imageURIs) {

            //create storage reference for user's image folder
            //points to the images folder
            imageReference = userReference.child("images/" + "img" + i);
            i++;
            uploadTask = imageReference.putFile(imageURI);
            strings.add(imageURI.getPath());
            // Register observers to listen for when the download is done or if it fails
            uploadTask.addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception exception) {
                }
            }).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, android.R.id.text1, strings);
                    lvMedia.setAdapter(adapter);
                }
            });

            databaseReference.child("users").child(firebaseAuth.getUid()).child("trips").child("trip" + tripId).child("images").child("img" + i).setValue(imageReference.toString());
        }
    }

    /**
     * @param view This method saves the Trip object to firebase database.
     */
    public void saveTrip(View view) {
        String title = null;
        String description = null;
        boolean ok;

        if ((!etTitle.getText().toString().isEmpty()) &&
                (!etDescription.getText().toString().isEmpty()) &&
                (imageURIs.size() != 0) &&
                (placesAdded)) {
            title = etTitle.getText().toString();
            description = etDescription.getText().toString();
            ok = true;
            placesAdded = false;
        } else {
            ok = false;
        }

        if (ok) {
            trip.setTitle(title);
            trip.setDescription(description);
            trip.setDate(date);
            databaseReference.child("users").child(firebaseAuth.getUid()).child("trips").child("trip" + tripId).child("title").setValue(trip.getTitle());
            databaseReference.child("users").child(firebaseAuth.getUid()).child("trips").child("trip" + tripId).child("description").setValue(trip.getDescription());
            databaseReference.child("users").child(firebaseAuth.getUid()).child("trips").child("trip" + tripId).child("date").setValue(trip.getDate());
            tripId++;
            databaseReference.child("users").child(firebaseAuth.getUid()).child("tripNumber").setValue(tripId);

            ToastUtil.showToast("Trip saved!", getApplicationContext());

            Log.d(TripAdderActivity.class.getSimpleName(), "Current trip id = " + tripId);
            Intent intentRecentTrips = new Intent(this, TripListActivity.class);
            intentRecentTrips.putExtra("tripId", tripId);
            startActivity(intentRecentTrips);
        } else {
            ToastUtil.showToast("Trip couldn't be saved! Please check fields!", getApplicationContext());
        }

    }
}
public class MapsAdderActivity extends FragmentActivity implements OnMapReadyCallback {

    private GoogleMap mMap;
    private List<Place> places;
    private Place place;
    private int placeId = 0;
    private long tripId;

    private DatabaseReference databaseReference;
    private FirebaseAuth firebaseAuth;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps_adder);

        //array list used to store the places added
        places = new ArrayList<>();

        //get database reference
        databaseReference = FirebaseDatabase.getInstance().getReference();

        //create instance of firebase auth
        firebaseAuth = FirebaseAuth.getInstance();

        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            tripId = bundle.getLong("tripId");
        }

        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        //move the camera to the center of the map
        mMap = googleMap;
        mMap.moveCamera(CameraUpdateFactory.newLatLng(new LatLng(0, 0)));

        mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
            @Override
            public void onMapClick(LatLng latLng) {
                // Creating a marker
                MarkerOptions markerOptions = new MarkerOptions();

                // Setting the position for the marker
                markerOptions.position(latLng);

                // Setting the title for the marker.
                // This will be displayed on taping the marker
                markerOptions.title(latLng.latitude + " : " + latLng.longitude);

                // Clears the previously touched position
                mMap.clear();

                // Animating to the touched position
                mMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));

                // Placing a marker on the touched position
                mMap.addMarker(markerOptions);
                place = new Place(Double.toString(latLng.latitude), Double.toString(latLng.longitude));
            }
        });
    }

    public void addMarkerToMap(View view) {
        places.add(place);
    }

    public void saveMarkers(View view) {
        for (int i = placeId; i < places.size(); i++) {
            writeNewPlace(places.get(i).getLat(), places.get(i).getLng());
        }
        TripAdderActivity.placesAdded = true;
        ToastUtil.showToast("Places added!", getApplicationContext());
    }

    private void writeNewPlace(String lat, String lng) {
        Place place = new Place(lat, lng);
        databaseReference.child("users").child(firebaseAuth.getUid()).child("trips").child("trip" + tripId).child("places").child(String.valueOf(placeId)).setValue(place);
        placeId++;
    }

    public void cleanMarkers(View view) {
        places.clear();
        placeId = 0;
        databaseReference.child("users").child(firebaseAuth.getUid()).child("trips").child("trip" + tripId).child("places").removeValue();
    }
}
此崩溃是因为我没有将图像添加到trip对象,它将尝试获取列表中的第一个图像,但列表中没有对象

应用程序崩溃的原因很明显,但我不理解这种行为(按
保存标记按钮关闭当前活动)。

有什么想法吗


LE:即使我只添加了一个位置,似乎也会出现错误。我正在更新位置的事实并没有导致崩溃。

onDataChange
将继续被调用,如果您在使用完侦听器后不删除它。完成后,您需要删除您的
onDataChange
侦听器,否则您将得到您描述的奇怪行为

您可以通过调用

databaseReference.removeEventListener(this);

在回调中

请添加崩溃日志。@vguzzi可以,但正如我提到的崩溃不是问题,而是重定向。将我重定向到
TripListActivity
会导致逻辑
BoundsException的排列
。该问题将在堆栈跟踪中发现,可能
TripAddActivity
也正在完成。我看不到你的垃圾箱,因为它在工作时被堵住了。无论如何,您都应该将它们上载到问题中,以便将所有内容都保存在内部。请添加错误和发生错误的行。使用
placeId
(实际上是
placeCount
),而不是使用
places.size()
,这将帮助您避免脱钩。我发现了一个很棒的教程,它几乎解释了事件侦听器的所有内容。
08-06 17:08:24.110 3293-3293/com.grrigore.tripback_up E/onStart ------: TripListActivity: onStart()
08-06 17:08:24.125 3293-3293/com.grrigore.tripback_up E/onResume ------: TripListActivity: onResume()
08-06 17:08:29.832 3293-3293/com.grrigore.tripback_up E/onPause ------: TripListActivity: onPause()
08-06 17:08:29.916 3293-3293/com.grrigore.tripback_up E/onStart ------: TripAdderActivity: onStart()
08-06 17:08:29.921 3293-3293/com.grrigore.tripback_up E/onResume ------: TripAdderActivity: onResume()
08-06 17:08:30.479 3293-3293/com.grrigore.tripback_up E/onStop ------: TripListActivity: onStop()
08-06 17:08:38.158 3293-3293/com.grrigore.tripback_up E/onPause ------: TripAdderActivity: onPause()
08-06 17:08:38.806 3293-3293/com.grrigore.tripback_up E/art: The String#value field is not present on Android versions >= 6.0
08-06 17:08:39.281 3293-3293/com.grrigore.tripback_up E/onStart ------: MapsAdderActivity: onStart()
08-06 17:08:39.286 3293-3293/com.grrigore.tripback_up E/onResume ------: MapsAdderActivity: onResume()
08-06 17:08:39.943 3293-3293/com.grrigore.tripback_up E/onStop ------: TripAdderActivity: onStop()
08-06 17:09:06.030 3293-3293/com.grrigore.tripback_up E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.grrigore.tripback_up, PID: 3293
    java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
        at java.util.ArrayList.get(ArrayList.java:411)
        at com.grrigore.tripback_up.TripListActivity$1.onDataChange(TripListActivity.java:119)
        at com.google.android.gms.internal.firebase_database.zzfc.zza(Unknown Source)
        at com.google.android.gms.internal.firebase_database.zzgx.zzdr(Unknown Source)
        at com.google.android.gms.internal.firebase_database.zzhd.run(Unknown Source)
        at android.os.Handler.handleCallback(Handler.java:751)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6816)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1563)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1451)
databaseReference.removeEventListener(this);