01/10/2018, 11:04

Start an Activity from a fragment

Em đang làm 1 list video lấy API từ youtube trong fragment nhưng không hiểu sao lúc click vô từng video chạy thì nó bào lỗi dòng getActivity().startActivity(intent); và không đọc được id như dự kiến của từng video =_= Ai giúp em vì sao với ạ ! em đang cần gấp

public class Video_Fragment extends Fragment {

    public static  String API_KEY = "AIzaSyCB59pO7FTdpna3aHMICxqKLtIKjvNH1q8";
    String ID_PLAYLIST = "PL-PpZh8TcUi64LzNDIpvTbXZp5XNBGrlA";
    String UrlJsonGET = "https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&playlistId=" + ID_PLAYLIST + "&key=" + API_KEY + "&maxResults=50";

    ListView lvVideo;
    ArrayList<VideoYoutube> arrayVideo;
    VideoYoutubeAdapter adapter;

    public Video_Fragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        ((MainActivity) getActivity()).getSupportActionBar().setTitle("Video");
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_video_, container, false);




    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        lvVideo = (ListView)getActivity().findViewById(R.id.listViewVideo);
        addVideoYou();
        GetJsonYouTube(UrlJsonGET);

        lvVideo.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(getActivity(), Player_videoFragment.class);
                intent.putExtra("idVideoYouTube", arrayVideo.get(i).getIdVideo());
                getActivity().startActivity(intent);

            }
        });



    }
Tynk Huynk viết 13:11 ngày 01/10/2018

Intent intent = new Intent(getActivity(), Player_videoFragment.class);

Player_videoFragment.class có phải là class Activity không vậy ? nếu không thì chạy lỗi là đúng rồi

Tien Tran viết 13:07 ngày 01/10/2018

Mình cũng từng gặp lỗi như bạn nhưng không hiểu sao getActivity() thay bằng this vào là nó hết liền

Phạm Vinh viết 13:06 ngày 01/10/2018

Log nói gì? crash khi nào? ở đâu? thông tin bạn cung cấp ít quá

Vũ Nội viết 13:14 ngày 01/10/2018

Cảm ơn ! ả nhà đã đọc bài biết của em. Em fix được rồi ạ !

du viết 13:15 ngày 01/10/2018

Bạn fix được rồi thì post solution lên cho người sau gặp phải ko phải lập topic hỏi nữa

Bài liên quan
0