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
Aucun commentaire:
Enregistrer un commentaire