I am opening the application and it crashed
Asked
Active
Viewed 66 times
-4
-
Show your activity_main.xml file – Son Truong Aug 18 '18 at 14:06
-
Possible duplicate of [Replacing a fragment with another fragment inside activity group](https://stackoverflow.com/questions/5658675/replacing-a-fragment-with-another-fragment-inside-activity-group) – Khaledonia Aug 18 '18 at 14:28
-
show java class and xml aslo show logcat error – Aug 18 '18 at 14:38
-
please, show us your logcat output. we need to see your error. – Nazarii Moshenskiy Aug 29 '18 at 11:56
1 Answers
0
You can be write FragmentTransaction inside the onClick of button. Follow below code,
public class MainActivity extends AppCompatActivity {
Button frag1, frag2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
frag1 = (Button) findViewById(R.id.frag1btn);
frag2 = (Button) findViewById(R.id.frag2btn);
frag1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentTransaction trans = getSupportFragmentManager().beginTransaction();
trans.replace(R.id.fragment_container, new Fragment1());
trans.commit();
}
});
frag2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentTransaction trans = getSupportFragmentManager().beginTransaction();
trans.replace(R.id.fragment_container, new Fragment2());
trans.commit();
}
});
}
}

Virendra Kachhi
- 184
- 1
- 12