Bayan Abuawad
Bayan Abuawad

Reputation: 134

Android Activity's - How can I open another activity in android?

Hey I am still new at Android and having problem with opening another activity from the main activity.

Main Class:

    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 Class:

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

When I click on the song that in the main page it show an error on my phone that say

"Unfortunately, [Project Name] has stopped"

When I debugged,I found that the problem where with the startActivityForResult(intent, 1);

Can someone help please?

Upvotes: 0

Views: 78

Answers (1)

Gilad Eshkoli
Gilad Eshkoli

Reputation: 1253

You are trying to open a Fragment and not an Activity.

Fragments are stated classes that can live in Activities which are good for re-use if you need them in other Activities too. A Fragment is a piece of an application's user interface or behavior that can be placed in an Activity, but a Fragment can't be instantiated without an Activity.

In order to implement a Fragment in your Activity, just make a transaction like that in a container in your activity :

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

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

If you do not want a Fragment, just change SongsPage class to be an Activity. Don't forget to declare it in the Manifest in that case. Or if you do need it as a Fragment just refer this.

Upvotes: 2

Related Questions