0

I've created a button which will open a new activity, but when I start the app and click on the button, the application just immediately restarts without any logcat errors. Here is my code:

public class amumu extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    return inflater.inflate(R.layout.amumu, container, false);
}

public void OnClickAmumuRunes(View view){
    Intent GoToRunes = new Intent(view.getContext(), amumurunes.class);
    startActivity(GoToRunes);
}

public void OnClickAmumuBuild(View view){
    Intent GoToRunes = new Intent(view.getContext(), amumubuild.class);
    startActivity(GoToRunes);
}

This is the code which I want to open, but I can't:

public class amumubuild extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    return inflater.inflate(R.layout.amumubuild, container, false);
}

}

and this is a fragmentclass where is tablayout in which first class is

public class FragmentClass extends AppCompatActivity {
private ViewPager viewPager;
private TabLayout tabLayout;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fragments);
    initViews();
    setuppager();

}

private void initViews(){
    viewPager = findViewById(R.id.ViewPager);
    tabLayout = findViewById(R.id.tab);
}

private void setuppager(){
    PagerAdapter pagerAdapter = new SlideAdapter(getSupportFragmentManager());
    viewPager.setAdapter(pagerAdapter);
    tabLayout.setupWithViewPager(viewPager);
}
public void OnClickDisplayToastAmumu(View view) {
    Toast.makeText(this,"Amumu",Toast.LENGTH_SHORT).show();
}

public void OnClickDisplayToastLee(View view) {
    Toast.makeText(this,"Lee Sin",Toast.LENGTH_SHORT).show();
}

public void OnClickDisplayToastPantheon(View view) {
    Toast.makeText(this,"Pantheon",Toast.LENGTH_SHORT).show();
}

public void OnClickDisplayToastNami(View view) {
    Toast.makeText(this,"Nami",Toast.LENGTH_SHORT).show();
}
Matt
  • 2,953
  • 3
  • 27
  • 46
jjj
  • 1
  • 1
  • If you Google it, you’ll find tutorials that can explain it much better than we can in an answer here.Which tutorial you follow can you share this and can show your output of this problem – Samad Talukder Apr 02 '20 at 11:59
  • 1
    "application just immediately restart without any logcat errors" -- try playing with the various filtering options in the Logcat tool of Android Studio. I guarantee you that there is an error there. Specifically, `amumubuild` is a `Fragment`, and you are trying to use `startActivity()` to show it. – CommonsWare Apr 02 '20 at 11:59

2 Answers2

2

You are using startActivity() to navigate to a Fragment. This doesn't work. You can only specify Activity classes in an Intent.

Additionally, make sure that you register each Activity in the AndroidManifext.xml file like this:

<application>
...
    <activity android:name="com.example.SecondActivity">
</application>

Check out this post to see how to navigate between multiple Fragments in one Activity: How to start Fragment from an Activity

Alexander Hoffmann
  • 5,104
  • 3
  • 17
  • 23
1

You can not start fragment by startActivity function so you need this:

private void loadFragment(final Fragment fragment) {
    // load fragment
    try {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.frame_container, fragment);
        transaction.commit();
        currentFragment = fragment;

    } catch (Exception e) {
        Log.d("mal", e.toString());
    }
}