2017-03-14 2 views
-1
nicht

Hier ist mein Code für ein Profil Aktivität Tabbed Fragmente:Imageupload-Taste funktioniert

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)); 
     } 
    } 
} 

und hier ist mein Fragment:

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 upload image funktioniert nicht. Wenn ich auf den Knopf klicke, passiert nichts.

+0

agarwal wurde dein Problem gelöst? – tahsinRupam

+0

nein es war nicht .... fragment activity dosent hört für onclick listener ... kann man mich zu einem link dat hat einen richtigen code mit buttons in fragmenten ... oder github..thank you –

+0

Hast du es versucht meine Lösung ? Übrigens, warum hast du onCreated() im Fragment verwendet? Bitte erkläre. – tahsinRupam

Antwort

0

1) Schaltfläche global deklarieren;

Button button; 

2) entfernen onCreate() und modifizieren onCreateView() wie folgt:

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) Ändern onClick Methode wie folgt:

@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; 
    } 
} 

Hoffnung, das hilft.

Verwandte Themen