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
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
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();
}
}