How to adapt an Activity to Fragmentactivity

Asked

Viewed 100 times

1

I’m making a NavigationDrawer, and then I have to make some adaptations, because I’m extending Fragment, but I need to use int’s for Activity to work, I tried calling inside OnCreateView the FragmentActivity but it didn’t work out so well.

It’s this class right here:

package player.kmk.com.kmk;


import android.content.Context;
import android.media.AudioManager;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.SeekBar;

import java.io.IOException;

/**
 * Created by Z0NEN on 10/22/2014.
 */
public class menu1_Fragment extends Fragment {
    View rootview;
    protected static final String TAG = null;
    private AudioManager audioManager;
    private StreamingMediaPlayer audioStreamer;
    private boolean isPlaying;
    private ImageButton playButton;
    private String urlStreaming;
    private menu1_Fragment station;



    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rootview = inflater.inflate(R.layout.menu1_layout, container, false);
     //   listen.ouvir();
    //    listen.definirVolumeSlider();
         Bundle bundle;

        playButton = ((ImageButton).findViewById(R.id.play_button));
        this.playButton.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View paramAnonymousView)
            {
                if (isPlaying)
                {
                    audioStreamer.interrupt();
                    playButton.setImageResource(R.drawable.play);
                }
                if (!isPlaying)
                {
                    startStreamingAudio();
                    playButton.setImageResource(R.drawable.pause);
                }
                isPlaying=!isPlaying;
            }
        });
        this.audioManager = ((AudioManager)getSystemService(Context.AUDIO_SERVICE));
        int i = this.audioManager.getStreamMaxVolume(3);
        int j = this.audioManager.getStreamVolume(3);
        setVolumeControlStream(3);
        SeekBar localSeekBar = (SeekBar)findViewById(R.id.seekBar);
        localSeekBar.setMax(i);
        localSeekBar.setProgress(j);
        localSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener()
        {
            public void onProgressChanged(SeekBar paramAnonymousSeekBar, int paramAnonymousInt, boolean paramAnonymousBoolean)
            {
                audioManager.setStreamVolume(3, paramAnonymousInt, 0);
            }

            public void onStartTrackingTouch(SeekBar paramAnonymousSeekBar)
            {
            }

            public void onStopTrackingTouch(SeekBar paramAnonymousSeekBar)
            {
            }
        });
        return rootview;


    }



    public void startStreamingAudio()
    {
        try
        {
            if (this.audioStreamer != null)
                this.audioStreamer.interrupt();
            this.audioStreamer = new StreamingMediaPlayer(this, playButton);
            this.audioStreamer.startStreaming("http://sh.upx.com.br:10369", 5208L, 216L);
            return;
        }
        catch (IOException localIOException)
        {
            while (true)
                Log.e(getClass().getName(), "Error starting to stream audio.", localIOException);
        }
    }



    public void onDestroy()
    {
        super.onDestroy();
        if (this.audioStreamer != null)
            this.audioStreamer.interrupt();
    }

}

The way it is is unsustainable, as it is like Fragment, as I do adaptations with Activity of certain functions like set the player to play with findByViewId or control the volume with Seek Bar.

1 answer

1


Try using findViewById in the onCreateView() fragment method like this:

rootView.findViewById(id);

Inside the fragment but outside onCreateView() do so:

getView().findViewById(id);

For other situations where an Activity or Context is required to use getActivity().

  • I will try and in the case of getSistemService()?

  • 1

    Do getActivity(). getSystemService();

  • You can show an example with getView()?

  • Managed: playButton = (Imagebutton) getView(). findViewById(R.id.play_button); in the first case.

  • Could not in this situation: this.audioManager = ((Audiomanager)getSystemService(Context.AUDIO_SERVICE)); .

  • 1

    Well, the answer was accepted so I’m guessing it did. Anyway, it would be this.audioManager = (Audiomanager)(getActivity(). getSystemService(Context.AUDIO_SERVICE));

  • Thank you. No . setVolumeControlStream(3) I put a getActivity() to pick up the activity and did not mark error, that’s right?

  • That’s right yes.

  • And in these cases it appears the error "Cannot resolve method get Intent`" what I do still using Activity in Fragment: feed = (Rssfeed) getIntent(). getExtras(). get("feed"); int pos = getIntent(). getExtras(). getInt("pos");

  • Also, getActivity(). getIntent(). get...

  • I had tried before asking and tried again but error appears where it invokes

  • There I can no longer say what may be happening. From the error you quoted it seems that getIntent() is spelled wrong: "get Intent". Create a new question and provide more details.

  • There is no spelling error, now it is to run. See here:http://answall.com/questions/109202/erro-call.

Show 8 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.