Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing fragments

I am trying to show different fragments depending on whether GPS is enabled or not. But I am getting this error :-

Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f0e00a1 

Please advise me how to do this:

Here is my MainActivity class:

public class MainActivity extends Activity {

Fragment fr;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final LocationManager mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    if (!mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
        fr = new FragmentTwo();
        getFragment();
    } else {
        fr = new FragmentOne();
        getFragment();
    }
}

public void getFragment() {
    FragmentManager fm = getFragmentManager();
    FragmentTransaction fragmentTransaction = fm.beginTransaction();
    fragmentTransaction.replace(R.id.fragment_place, fr);
    fragmentTransaction.commit();
  }
}

FragmentOne:-

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

FragmentTwo:-

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

My activity_main:-

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<fragment
    android:id="@+id/fragment_place"
    android:name="com.test.FragmentTwo"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>
</LinearLayout>

And the fragment_one.xml and fragment_two.xml are same just the different text:-

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
    android:id="@+id/textView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="GPS IS ENABLE"/>
 </RelativeLayout>

Thanks in advance.

like image 782
shivam Avatar asked Jun 23 '26 15:06

shivam


1 Answers

Replace

<fragment
    android:id="@+id/fragment_place"
    android:name="com.test.FragmentTwo"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

With

<FrameLayout
    android:id="@+id/fragment_place"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

As static Fragment can not be replace at run time. But you can add or replace fragment in FrameLayout.

like image 160
Dhaval Patel Avatar answered Jun 26 '26 05:06

Dhaval Patel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!