片段因未知原因而关闭(不是方向更改)

时间:2017-10-30 21:03:10

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

在下面的代码中,片段将在scavengerHunt方法中关闭(出于某种原因)。我相信它与我在代码中使用valueEventListeners的方式有关,但我不太确定。如果我不尝试在这些方法中调用switch语句,片段将不会关闭,但我的逻辑也不会工作。请帮忙,如果需要更多代码,请告诉我。我真的很茫然,觉得我已经用我所拥有的一切来尝试,尽管事实并非如此。

public class scavengerHunt extends Fragment implements View.OnClickListener {



Button letsGo, submit;

ImageButton uploadImage;

TextView welcomeScav;

TextView instructions;

FirebaseAuth mAuth;

DatabaseReference mDatabase;

StorageReference mStorage;

private String user_id;

private String winner;

private static final int PICK_IMAGE = 1;

private Uri imageURI;



public scavengerHunt() {

    // Required empty public constructor

}



@Override

public View onCreateView(LayoutInflater inflater, ViewGroup container,

                         Bundle savedInstanceState) {

    // Inflate the layout for this fragment

    View view = inflater.inflate(R.layout.fragment_scavenger_hunt, container, false);

    //initialization code

    letsGo = (Button) view.findViewById(R.id.scav_Button);

    submit = (Button) view.findViewById(R.id.submit_Image);

    uploadImage = (ImageButton) view.findViewById(R.id.scav_Pic);

    winner = "complete";

    welcomeScav = (TextView) view.findViewById(R.id.welcome_scav_Text);

    instructions = (TextView) view.findViewById(R.id.instruction_Scav_Text);

    mAuth = FirebaseAuth.getInstance();

    user_id = mAuth.getCurrentUser().getUid();

    mDatabase = FirebaseDatabase.getInstance().getReference().child("Users").child(user_id);

    mStorage = FirebaseStorage.getInstance().getReference();





    letsGo.setOnClickListener(this);

    uploadImage.setOnClickListener(this);

    submit.setOnClickListener(this);

    submit.setClickable(false);



    return view;

}



@Override

public void onClick(View v) {

    if(v.getId() == R.id.scav_Button){



        letsGo.setText("Find it!");

        letsGo.setClickable(false);

        scavengerHunt();



    } else if(v.getId() == R.id.scav_Pic){



        openGallery();

        submit.setClickable(true);



    } else if(v.getId() == R.id.submit_Image){

        StorageReference filepath = mStorage.child("Scavenger Hunt Pictures").child(imageURI.getLastPathSegment());

        filepath.putFile(imageURI).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {

            @Override

            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {

                Toast.makeText(getContext(), "Your offering has been accepted.", Toast.LENGTH_LONG).show();

                letsGo.setText("Next Clue");

                letsGo.setClickable(true);

                submit.setClickable(false);

                //@SuppressWarnings("VisibleForTests")



                //The following commented out code is a template for saving the photo URL.

                // Uri downloadUrl = taskSnapshot.getDownloadUrl();

                //DatabaseReference newSpeaker = mDatabase.push();

                //newSpeaker.child("name").setValue(name);

                //newSpeaker.child("bio").setValue(bio);

                //newSpeaker.child("photoURL").setValue(downloadUrl.toString());

            }

        });

    }

}



public void scavengerHunt() {

    //Get a snapshot of the database to see where the user is at on the clues.



    mDatabase.addListenerForSingleValueEvent(new ValueEventListener() {

        @Override

        public void onDataChange(DataSnapshot dataSnapshot) {

            //get the current value of scavenger hunt instructions

            String currentInstruction = dataSnapshot.child("Scavenger Hunt Instructions").getValue(String.class);



            //if this is the first time the user has started the scavenger hunt

            if (dataSnapshot.child("Scavenger Hunt Instructions").getValue(String.class) == null) {

                mDatabase.child("Scavenger Hunt Instructions").setValue(getInstruction("Start"));

                updateInstructions();

            } else



                //if the user has finished the hunt

                if (dataSnapshot.child("Scavenger Hunt Instructions").getValue(String.class) == "Winner") {

                    Toast.makeText(getContext(), "You have completed the challenge!", Toast.LENGTH_LONG).show();

                    //exit the fragment

                } else {



                    //this is normal operation



                    //Change the database value to the next instruction

                    mDatabase.child("Scavenger Hunt Instructions").setValue(getInstruction(currentInstruction));

                    updateInstructions();

                }

        }

        @Override

        public void onCancelled(DatabaseError databaseError) {

            //TODO: Error catch

        }

    });

//maybe something needs to go here

}



private void updateInstructions(){

    //Take a new (and freshly updated) snapshot

    mDatabase.addValueEventListener(new ValueEventListener() {

        @Override

        public void onDataChange(DataSnapshot dataSnapshot) {

            //Change the instruction text to the instruction the user needs to see.

            instructions.setText(dataSnapshot.child("Scavenger Hunt Instructions").getValue(String.class));

        }



        @Override

        public void onCancelled(DatabaseError databaseError) {

            //TODO: Error catch

        }

    });

}





public String getInstruction(String current){

    String instruction = current;

    switch (current){

        case "Start":

            instruction =  "Grab a drink with the Presidents of the United States";

            break;

        case "Grab a drink with the Presidents of the United States":

            instruction = "Turns out Lincoln is a bit clingy. Go somewhere he wouldn't want to go.";

            break;

        case "Turns out Lincoln is a bit clingy. Go somewhere he wouldn't want to go.":

            instruction = "Winner";

            break;

    }

    return instruction;

}



private void openGallery(){

    //Intent gallery = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);

    //startActivityForResult(gallery, PICK_IMAGE);

    Intent photoPickerIntent = new Intent(Intent.ACTION_GET_CONTENT);

    photoPickerIntent.setType("image/*");

    startActivityForResult(photoPickerIntent, PICK_IMAGE);

}



public void onActivityResult(int requestCode, int resultCode, Intent data){

    super.onActivityResult(requestCode, resultCode, data);

    if(resultCode == RESULT_OK && requestCode == PICK_IMAGE){

        imageURI = data.getData();

        uploadImage.setImageURI(imageURI);

    }

}

}

0 个答案:

没有答案