Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
116 views
in Technique[技术] by (71.8m points)

android - converting activity into fragment

This is a simple code to play a sound on click off a button, this code was initially written in Activity but now i want to change it to Fragments.

errors

1) The method setContentView(int) is undefined for the type Rajathmusic.

2) The method create(Context, int) in the type MediaPlayer is not applicable for the arguments (Rajathmusic, int).

3)The method findViewById(int) is undefined for the type Rajathmusic.

I am just starting off with android development, any help would be appreciated!

public class Rajathmusic extends Fragment {

private static final String TAG = "MyActivity";

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

    setContentView(R.layout.activity_main);

    Log.v(TAG, "Initializing sounds...");

    final MediaPlayer mp = MediaPlayer.create(this, R.raw.rajath);

    Button play_button = (Button)this.findViewById(R.id.button3);

    play_button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Log.v(TAG, "Playing sound...");
            mp.start();
        }
    });
    Log.v(TAG, "Sounds initialized.");
}}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
  1. Fragment has a method called onCreateView(LayoutInflater, ViewGroup, Bundle). Override it, inflate using the layout and return the view.
  2. Since create method expects a Context, pass it using getActivity()
  3. findViewById(int) can be called as getView().findViewById(R.id.button3)

Here is a sample code:

public class Rajathmusic extends Fragment {

    private static final String TAG = "MyActivity";

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        return inflater.inflate(R.layout.activity_main, container, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        Log.v(TAG, "Initializing sounds...");

        final MediaPlayer mp = MediaPlayer.create(getActivity(), R.raw.rajath);

        View v = getView();

        Button play_button = (Button) v.findViewById(R.id.button3);

        play_button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Log.v(TAG, "Playing sound...");
                mp.start();
            }
        });
        Log.v(TAG, "Sounds initialized.");
    }

}

Read more about Fragment lifecycle here to know why I've put the code in onActivityCreated and not onCreate


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...