Fragment Layout Not Adding Up More Views

101 views Asked by At

I'm trying to add a FrameLayout in my fragment layout, just to reserve space to add another fragment, programmatically. Here's code :

FRAGMENT XML LAYOUT

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/detail"
        android:text="" />

    <FrameLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/fragment_container"/>

</LinearLayout>

But the problem is that, when I'm using FragmentTransaction to replace the FrameLayout with a fragment, it gives an error:

FATAL EXCEPTION: main
              Process: com.platform.apple, PID: 2448
              java.lang.IllegalArgumentException: No view found for id 0x7f080005 (com.platform.apple:id/fragment_container) for fragment Stopwatch1Fragment{e0be34c #0 id=0x7f080005}

Here's the Fragment class code:

Fragment Class Code

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
    Stopwatch1Fragment stopwatch1Fragment = new Stopwatch1Fragment();
    transaction.replace(R.id.fragment_container, stopwatch1Fragment);
    transaction.addToBackStack(null);
    transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
    transaction.commit();
    return inflater.inflate(R.layout.fragment_apple_detail, container, false);
}

Now, when I tried using the R.id.detail instead of R.id.fragment_container, the Fragment Class found that TextView while when I created another TextView after the FrameLayout in the Fragment's xml layout and tried using it, it still didn't work(Fragment didn't find the view by it's Id). So it seems like, only the first view(R.id.detail) is working while the Fragment class can't find any other added view after that. Please help!

2

There are 2 answers

0
sohan shetty On BEST ANSWER

Make width and height as match parent in fragment xml layoug like below

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/detail"
    android:text="" />

<FrameLayout
    android:layout_width="match_content"
    android:layout_height="match_content"
    android:id="@+id/fragment_container"/>

Then intialize fragment programatically like below

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                 Bundle savedInstanceState) {

View root = inflater.inflate(R.layout.fragment_apple_detail, container, false);

FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
Stopwatch1Fragment stopwatch1Fragment = new Stopwatch1Fragment();
transaction.replace(R.id.fragment_container, stopwatch1Fragment);
transaction.addToBackStack(null);
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
transaction.commit();

return root;
}
2
UMESH0492 On

try to replicate the onCreateViewcode by following

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                     Bundle savedInstanceState) {

    View root = inflater.inflate(R.layout.fragment_apple_detail, container, false);

    FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
    Stopwatch1Fragment stopwatch1Fragment = new Stopwatch1Fragment();
    transaction.replace(R.id.fragment_container, stopwatch1Fragment);
    transaction.addToBackStack(null);
    transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
    transaction.commit();

    return root;
}