Using Fragment and AppCompatActivity classes together

Ollie Atkins picture Ollie Atkins · May 16, 2016 · Viewed 13.2k times · Source

Hi I'm trying to make an app which uses two different classes I'm aware I can't use extend with two classes in Java. How would I go about separating the below code into two different classes so one can extend Fragment and the other AppCompatActivity?

package com.example.oliver.myapplication;

import android.support.v4.app.Fragment;
import android.app.AlertDialog;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

import java.util.Random;


public class MyFragment extends AppCompatActivity, Fragment {

    Button b, b2;
    MediaPlayer nice, burp;
    ImageButton img;
    int n;
    MediaPlayer [] s = new MediaPlayer[6];
    AlertDialog.Builder adb;

    public static MyFragment newInstance() {
        MyFragment fragment = new MyFragment();
        return fragment;
    }

        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.my_fragment, container, false);
            img = (ImageButton) rootView.findViewById(R.id.img);

            s[0] = MediaPlayer.create(MyFragment.this, R.raw.burp);
            s[1] = MediaPlayer.create(MyFragment.this, R.raw.robert);
            s[2] = MediaPlayer.create(MyFragment.this, R.raw.burp2);



        img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                for (int i = 0; i < 1; i++) {
                    Random r = new Random();
                    n = r.nextInt(3);

                    s[n].start();
                }
            }
        });return rootView;


}}

Answer

Karthiksrndrn picture Karthiksrndrn · May 16, 2016

The above code is not valid Java.

A class can only extend one class. You activity/fragment is extending 2 classes at the same time.

The class given above is a valid Fragment subclass, except for these lines :

        s[0] = MediaPlayer.create(MyFragment.this, R.raw.burp);
        s[1] = MediaPlayer.create(MyFragment.this, R.raw.robert);
        s[2] = MediaPlayer.create(MyFragment.this, R.raw.burp2);

Replace "MyFragment.this" with "MyFragment.getActivity()".

Now you can replace "extends AppCompatActivity, Fragment" with "extends Fragment"

Now you have a valid Fragment.

Create the AppCompatActivity yourself. Look up how you can add a fragment to an activity.

As a tip in good programming practice, I suggested that you move the following code in onCreateView() to onActivityCreated() :

        img = (ImageButton) rootView.findViewById(R.id.img);

        s[0] = MediaPlayer.create(MyFragment.this, R.raw.burp);
        s[1] = MediaPlayer.create(MyFragment.this, R.raw.robert);
        s[2] = MediaPlayer.create(MyFragment.this, R.raw.burp2);



    img.setOnClickListener(new View.OnClickListener() {
        @Override 
        public void onClick(View view) {
            for (int i = 0; i < 1; i++) {
                Random r = new Random();
                n = r.nextInt(3);

                s[n].start(); 
            } 
        } 
    });

Change

        img = (ImageButton) rootView.findViewById(R.id.img);

to

        img = (ImageButton) getView().findViewById(R.id.img);