Android Activity的 - 如何在android中打开另一个活动?

时间:2016-04-17 07:39:29

标签: android android-fragments

嘿,我仍然是Android的新手,并且在主要活动中打开另一项活动时遇到了问题。

主类:

    import android.content.Intent;
    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.AdapterView;
    import android.widget.ArrayAdapter;
    import android.widget.ListView;
    import android.widget.Toast;

    import java.util.HashMap;

    import layout.SongsPage;

    public class MainActivityFragment extends Fragment {

        ListView listView;
        HashMap<String, String> songs;

        Singer[] singeres=new Singer[4];

        public Singer[] getSingeres() {
            singeres[0]=new Singer("Dave Brubeck",1);
            singeres[0].AddingSong("Take Five","http://bff.vr2.net/jazz/Dave%20Brubeck%20Quartet%20featuring%20Paul%20Desmond%20-%20Buried%20Treasures%20()/07%20%20-%20Take%20Five.mp3");

            singeres[1]=new Singer("Jefferson Airplane",2);
            singeres[1].AddingSong("White Rabbit", "http://bff.vr2.net/jazz/Dave%20Brubeck%20Quartet%20featuring%20Paul%20Desmond%20-%20Buried%20Treasures%20()/07%20%20-%20Take%20Five.mp3");

            singeres[2]=new Singer("2Pac",3);
            singeres[2].AddingSong("Revolution","http://bff.vr2.net/jazz/Dave%20Brubeck%20Quartet%20featuring%20Paul%20Desmond%20-%20Buried%20Treasures%20()/07%20%20-%20Take%20Five.mp3");

            singeres[3]=new Singer("Mr. Probz",4);
            singeres[3].AddingSong("Waves","http://bff.vr2.net/jazz/Dave%20Brubeck%20Quartet%20featuring%20Paul%20Desmond%20-%20Buried%20Treasures%20()/07%20%20-%20Take%20Five.mp3");

            return singeres;
        }

        Singer[] Getsingeres=getSingeres();

        public MainActivityFragment() {
            songs = new HashMap<>();
            songs.put("Dave Brubeck - Take Five",
                    "http://bff.vr2.net/jazz/Dave%20Brubeck%20Quartet%20featuring%20Paul%20Desmond%20-%20Buried%20Treasures%20()/07%20%20-%20Take%20Five.mp3");
            songs.put("Jefferson Airplane - White Rabbit", "http://www.rraurl.com.br/media/musica/tracks/Jefferson%20Airplane%20-%201967%20-%20Surrealistic%20Pillow%20-%2010%20-%20White%20Rabbit.mp3");
            songs.put("2Pac - Revolution","http://thug2pac.narod.ru/mp3/Phenomeno/02_Revolution_www.thug2pac.narod.ru.mp3");
            songs.put("Mr. Probz - Waves", "http://mp3light.net/assets/songs/393000-393999/393222-waves-mr-probz.mp3");

            for(int i=0;i<singeres.length;i++){
                songs.put(Getsingeres[i].name,"http://bff.vr2.net/jazz/Dave%20Brubeck%20Quartet%20featuring%20Paul%20Desmond%20-%20Buried%20Treasures%20()/07%20%20-%20Take%20Five.mp3");
            }
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            final View v = inflater.inflate(R.layout.fragment_main, container, false);
            listView = (ListView) v.findViewById(R.id.listView);
            listView.setAdapter(new ArrayAdapter<String>(
                    getActivity(),
                    R.layout.listview_item,
                    R.id.textView,
                    songs.keySet().toArray(new String[songs.keySet().size()])));//keySet=key
            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    //starting the mediaPlayer Activity with the song url
                    Intent intent = new Intent(getActivity(), SongsPage.class);



                    **startActivityForResult(intent, 1);**
                   //getActivity().overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.fade_out);
                }
            });
            return v;
        }

        @Override
        public void onResume(){
            super.onResume();
        }

        @Override
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            getActivity();
           if(resultCode == -1){
               Toast.makeText(
                       getActivity(),
                       "can't play that song",
                       Toast.LENGTH_LONG).show();

           }
        }
    }

SongsPage类:

package layout;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.telhai.eiran.mediaplayer.R;

/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link SongsPage.OnFragmentInteractionListener} interface
 * to handle interaction events.
 */
public class SongsPage extends Fragment{

    private OnFragmentInteractionListener mListener;

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


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_songs_page, container, false);
    }

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

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

当我点击主页面中的歌曲时,它会在我的手机上显示错误

  

“不幸的是,[项目名称]已停止”

当我调试时,我发现问题在于startActivityForResult(intent, 1);

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:1)

您正在尝试打开片段而不是活动。

碎片是可以存在于活动中的陈述类,如果您在其他活动中也需要它们,这些活动可以重复使用。 片段是可以放置在活动中的应用程序的用户界面或行为的一部分,但片段无法在没有活动的情况下实例化。

要在您的Activity中实现Fragment,只需在您的活动的容器中进行类似的交易:

FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();

YourFragment frag = new YourFragment ();
ft.replace(R.id.container_for_fragment, frag);
ft.commit();

如果您不想要片段,只需将SongsPage类更改为Activity即可。 在这种情况下,不要忘记在Manifest中声明它。 或者如果您确实需要它作为片段,请参考this