ImageUpload按钮不起作用

时间:2017-03-14 17:12:01

标签: android

以下是我的带有标签片段的个人资料活动的代码:

  

PROFILE_ACTIVITY.java

public class ProfileActivity extends AppCompatActivity implements View.OnClickListener {


    private FirebaseAuth firebaseAuth;
    private Button buttonswitch;
    private Button buttons;
    private Button buttonLogout;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profile);
        firebaseAuth = FirebaseAuth.getInstance();

        //if the user is not logged in
        //that means current user will return null
        if (firebaseAuth.getCurrentUser() == null) {
            //closing this activity
            finish();
            //starting login activity
            startActivity(new Intent(this, LoginActivity.class));

        }
        FirebaseUser user = firebaseAuth.getCurrentUser();

        //initializing views

        TextView textViewUserEmail = (TextView) findViewById(R.id.textViewUserEmail);
        buttonLogout = (Button) findViewById(R.id.buttonLogout);
        buttonswitch = (Button) findViewById(R.id.dashboard);

        //displaying logged in user name
        textViewUserEmail.setText("Welcome " + user.getEmail());

        //adding listener to button
        buttonLogout.setOnClickListener(this);
        buttonswitch.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {
        //if logout is pressed
        if (view == buttonLogout) {
            //logging out the user
            firebaseAuth.signOut();
            //closing activity
            finish();
            //starting login activity
            startActivity(new Intent(this, LoginActivity.class));
        }

        if (view == buttonswitch) {
            startActivity(new Intent(this, tabbed.class));
        }
    }
}

这是我的片段:

    public class fupload extends Fragment implements View.OnClickListener{

        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

        // TODO: Rename parameter arguments, choose names that match
        // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
        private static final String ARG_PARAM1 = "param1";
        private static final String ARG_PARAM2 = "param2";
        Activity activity;

        // TODO: Rename and change types of parameters
        private String mParam1;
        private String mParam2;
        private StorageReference mstorage;

        private OnFragmentInteractionListener mListener;

        public fupload() {
            // Required empty public constructor
            activity = getActivity();
        }

        /**
         * Use this factory method to create a new instance of
         * this fragment using the provided parameters.
         *
         * @param param1 Parameter 1.
         * @param param2 Parameter 2.
         * @return A new instance of fragment fupload.
         */
        // TODO: Rename and change types and number of parameters
        public static fupload newInstance(String param1, String param2) {
            fupload fragment = new fupload();
            Bundle args = new Bundle();
            args.putString(ARG_PARAM1, param1);
            args.putString(ARG_PARAM2, param2);
            fragment.setArguments(args);
            return fragment;
        }


        private static final int GALLERY_INTENT = 2;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            if (getArguments() != null) {
                mParam1 = getArguments().getString(ARG_PARAM1);
                mParam2 = getArguments().getString(ARG_PARAM2);
            }
             mstorage = FirebaseStorage.getInstance().getReference();
             Button buttons = (Button) getView().findViewById(button1);
             buttons.setOnClickListener(this);


        }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_fupload, container, false);
        mstorage = FirebaseStorage.getInstance().getReference();
        Button buttons = (Button) getView().findViewById(button1);
        buttons.setOnClickListener(this);
        onClick(view);
        return view;
    }

    // TODO: Rename method, update argument and hook method into UI event
    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent(Intent.ACTION_PICK);
        intent.setType("images/*");
        startActivityForResult(intent, GALLERY_INTENT);

    }

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

        if(requestCode == GALLERY_INTENT && resultCode == RESULT_OK){
            Uri uri =data.getData();
            StorageReference filepath = mstorage.child("photos").child(uri.getLastPathSegment());
            filepath.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                    Toast.makeText(getActivity(), "upload done", Toast.LENGTH_LONG).show();
                }
            });
        }
    }

    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void onFragmentInteraction(Uri uri);
    }
}

onclick上传图片无效。当我点击按钮时,没有任何反应。

1 个答案:

答案 0 :(得分:0)

1)全局声明按钮;

Button button;

2)删除onCreate()并修改onCreateView(),如下所示:

 public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_fupload, container,   false);

        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
         mstorage = FirebaseStorage.getInstance().getReference();
         buttons = (Button) view.findViewById(button1);
         buttons.setOnClickListener(this);
         return view;
 }

3)修改onClick方法,如下所示:

@Override
public void onClick(View v) {
    switch(v.getId()){
        case R.id.button1:
            Toast.makeText(getActivity(), "Button is working!", Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(Intent.ACTION_PICK);
            intent.setType("images/*");
            startActivityForResult(intent, GALLERY_INTENT);
            break;
    }
}

希望这有帮助。