-1

I am getting an error

 public void on_Image_Button_9_Clicked(View view)
    {
        Intent click 5=new Intent(home.this,category 2.class);
        start Activity(click 5);
    }

// category 2 is fragment

Raedwald
  • 46,613
  • 43
  • 151
  • 237
Mr.A
  • 1
  • 2
  • 1
    What error are you getting? This guide may help in writing your question: https://stackoverflow.com/help/mcve – John Mercier Mar 29 '19 at 15:15
  • I think you did not understand the point of Activities and Fragments. You should take a look at the documentation. https://developer.android.com/guide/components/fragments. You can also take a look at this answer: https://stackoverflow.com/a/25822815/3780625 – Maxouille Mar 29 '19 at 15:52

1 Answers1

0

Your approach is wrong. To access a fragment you need to create a FragmentTransaction.

I invite you to read this guide on how fragments work to prevent any bad habits using it.

So, either you call your activity containing the fragment you want or you load the fragment in your current container.

To add a fragment to an activity you can declare it in you activity's layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment android:name="com.example.news.ArticleListFragment"
        android:id="@+id/list"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="match_parent" />
<fragment android:name="com.example.news.ArticleReaderFragment"
        android:id="@+id/viewer"
        android:layout_weight="2"
        android:layout_width="0dp"
        android:layout_height="match_parent" />
</LinearLayout>

Or add a container to your activity and display your fragment inside the container using a FragmentTransaction:

myActivity_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout android:id="@+id/myContainer"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

myActivity.java

 public class MyActivity extends AppCompatActivity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.myActivity_layout);
            setFragment()
        }


        public void setFragment() {
            // Create new fragment and transaction
            Fragment newFragment = new ExampleFragment();
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

            // Replace whatever is in the fragment_container view with this fragment, and add the transaction to the back stack
            transaction.replace(R.id.myContainer, newFragment);
            transaction.addToBackStack(null);

            // Commit the transaction
            transaction.commit();
        }
    }
Robert LaFondue
  • 191
  • 1
  • 9