1

I've got an activity which should show a random fragment.

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import java.util.Random;
import android.support.v4.app.*;

public class RandomActivity extends FragmentActivity {

private Random1 random1;
private Random2 random2;
private Random3 random3;
private Random4 random4;
private Random5 random5;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_random);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

    initUI();
}

private void initUI() {
    FragmentTransaction = getSupportFragmentManager().openTransaction();
    setRandomFragment();
}

private void setRandomFragment() {
    Random random = new Random();
    int randomFragmentNum = random.nextInt(250);
    if (randomFragmentNum < 50) {
        randomFragmentNum = 0;
    } else if (randomFragmentNum >= 50 && randomFragmentNum < 100) {
        randomFragmentNum = 1;
    } else if (randomFragmentNum >= 100 && randomFragmentNum < 150) {
        randomFragmentNum = 2;
    } else if (randomFragmentNum >= 150 && randomFragmentNum < 200) {
        randomFragmentNum = 3;
    } else if (randomFragmentNum >= 200 && randomFragmentNum <= 250) {
        randomFragmentNum = 4;
    }

    switch (randomFragmentNum) {
        case 0: {
            random1 = new Random1();
            FragmentTransaction.add(R.id.frag_rand_home, random1);
            FragmentTransaction.commit()
            break;
        }
        case 1: {
            random2 = new Random2();
            FragmentTransaction.add(R.id.frag_rand_home, random2);
            FragmentTransaction.commit()
            break;
        }
        case 2: {
            random3 = new Random3();
            FragmentTransaction.add(R.id.frag_rand_home, random3);
            FragmentTransaction.commit()
            break;
        }
        case 3: {
            random4 = new Random4();
            FragmentTransaction.add(R.id.frag_rand_home, random4);
            FragmentTransaction.commit()
            break;
        }
        case 4: {
            random5 = new Random5();
            FragmentTransaction.add(R.id.frag_rand_home, random5);
            FragmentTransaction.commit()
            break;
        }
    }
}
}

So that's my code but I still get some errors :-(

Here error number 1:

private void initUI() {
    FragmentTransaction = getSupportFragmentManager().openTransaction();
    setRandomFragment();
}

Expression expected | FragmentManager.openTransaction can only be called from within the same library group (groupId=com.android.support). This API has been flagged with a restriction that has not been met. Examples of API restrictions: * Method can only be invoked by a subclass * Method can only be accessed from within the same library (defined by the Gradle library group id) .* Method can only be accessed from tests. . You can add your own API restrictions with the @RestrictTo annotation.

Here error number 2 (on case 0,1,2,3,4 the same):

case 0: {
            random1 = new Random1();
            FragmentTransaction.add(R.id.frag_rand_home, random1);
            FragmentTransaction.commit()
            break;
        }

Non-static method 'add(int, android.support.v4.app.Fragment)' cannot be referenced from a static context. | Non-static method 'commit()' cannot be referenced from a static context.

I'm stucking on this two errors since some hours and but I still don't know how to fix them.

It would be very nice if somebody can help me with my problems... Best regards Jeremy

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Jeremy Brehe
  • 117
  • 1
  • 12
  • I suggest you learn about arrays and `List`. When you start naming variables with numbers on the end, then you should look at other possible solutions. With an array, you can reduce `setRandomFragment()` to two lines of code. – Code-Apprentice Apr 19 '18 at 17:30
  • You should also just do `int randomFragmentNum = random.nextInt(5);` then you don't need the `if...else` chain. – Code-Apprentice Apr 19 '18 at 17:31
  • `FragmentTransaction = getSupportFragmentManager().openTransaction();` this line does not make sense. its like `String = "hello";` – Nongthonbam Tonthoi Apr 19 '18 at 17:34

2 Answers2

2

You need to create an instance averrable instead of using it with class as

FragmentTransaction fm = getSupportFragmentManager().beginTransaction();

and since you want to use it inside other methods so

private FragmentTransaction fm;
private void initUI() {
    FragmentTransaction fm = getSupportFragmentManager().beginTransaction();
    setRandomFragment();
}

later

case 0: {
        random1 = new Random1();
        fm(R.id.frag_rand_home, random1);
        fm()
        break;
    }

Note: openTransaction() should be beginTransaction() and you have to get a new instance of transaction before loading another fragment and also use replace to change fragment second time and later

FragmentManager.openTransaction can only be called from within the same library group (groupId=com.android.support)

From the source code the opentransaction has been deprecated so no longer in use and not even there in docs as well but internally it does call beginTranscation and also it is applied with @hide annotation so seems like you are using some old android platform to build your app

Reference :

What exactly does Android's @hide annotation do?

Difference between add(), replace(), and addToBackStack()

Pavneet_Singh
  • 36,884
  • 5
  • 53
  • 68
  • There's still FragmentManager.openTransaction can only be called from within the same library group (groupId=com.android.support)... – Jeremy Brehe Apr 19 '18 at 17:34
  • @JeremyBrehe I am unable to find the documentation of `openTransaction` so I don't know what it is – Pavneet_Singh Apr 19 '18 at 17:36
  • from the [source code](https://android.googlesource.com/platform/frameworks/support/+/cef09fe/v4/java/android/support/v4/app/FragmentManager.java) the `opentransaction` has been deprecated so no longer in use and not even there in docs as well but internally it does call `beginTranscation` and also it is applied with @hide annotation so seems like you are using some old android platform to build your app – Pavneet_Singh Apr 19 '18 at 17:51
0

So thank you very much for your attention and help! Now I was able to find a solution for myself. I think it's posssible to do this with less code but it works!

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import java.util.Random;
import android.support.v4.app.*;
import android.support.v4.app.Fragment;

public class RandomActivity extends FragmentActivity {

private Random1 random1;
private Random2 random2;
private Random3 random3;
private Random4 random4;
private Random5 random5;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_random);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

    FragmentTransaction fm = getSupportFragmentManager().beginTransaction();

    initUI();
}

private void initUI() {
    setRandomFragment();
}

private void setRandomFragment() {
    Random random = new Random();
    int randomFragmentNum = random.nextInt(250);
    if (randomFragmentNum < 50) {
        randomFragmentNum = 0;
    } else if (randomFragmentNum >= 50 && randomFragmentNum < 100) {
        randomFragmentNum = 1;
    } else if (randomFragmentNum >= 100 && randomFragmentNum < 150) {
        randomFragmentNum = 2;
    } else if (randomFragmentNum >= 150 && randomFragmentNum < 200) {
        randomFragmentNum = 3;
    } else if (randomFragmentNum >= 200 && randomFragmentNum <= 250) {
        randomFragmentNum = 4;
    }

    switch (randomFragmentNum) {
        case 0: {
            random1 = new Random1();
            FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.frag_rand_home, random1, "Home");
            fragmentTransaction.commit();
            break;
        }
        case 1: {
            random2 = new Random2();
            FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.frag_rand_home, random2, "Home");
            fragmentTransaction.commit();
            break;
        }
        case 2: {
            random3 = new Random3();
            FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.frag_rand_home, random3, "Home");
            fragmentTransaction.commit();
            break;
        }
        case 3: {
            random4 = new Random4();
            FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.frag_rand_home, random4, "Home");
            fragmentTransaction.commit();
            break;
        }
        case 4: {
            random5 = new Random5();
            FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.frag_rand_home, random5, "Home");
            fragmentTransaction.commit();
            break;
        }
    }
}
}
Jeremy Brehe
  • 117
  • 1
  • 12