Error in fixing dynamic fragments and API 27 is not working

I'm working on a tutorial that teaches me to set Dynamic fragments. In this the fragments change depending on the orientation. I have two fragments.

Here's the code of the MainActivity

package com.universal.fragments;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.app.Activity;

public class MainActivity extends Activity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        //get the current display info

        DisplayMetrics display =  this. getResources().getDisplayMetrics();
        int width = display.widthPixels;
        int height = display.heightPixels;
        if (width > height){
            //Landscape mode
            Fragment1 fragment1 = new Fragment1();
            fragmentTransaction.replace(android.R.id.content, **fragment1**);
        }
        else{
            Fragment2 fragment2 = new Fragment2();
            fragmentTransaction.replace(android.R.id.content, **fragment2**);
        }
        fragmentTransaction.commit();
    }

}

This is the Fragment1 code

package com.universal.fragments;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.LayoutInflater;
import androidx.fragment.app.Fragment;

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

This is the Fragment2 code

package com.universal.fragments;
import androidx.fragment.app.Fragment;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.LayoutInflater;

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

The problem is that...

fragmentTransaction.replace(android.R.id.content, fragment1);

does not work... it gives an error under the fragment1 and fragment2

Solution I tried: I thought this is because the FragmentTransaction and FragmentManager packages have deprecated I reduced the minimum SDK to API 27 and another problem popped out.

 When I run the project it gives the error below

Resource Linking failed (When debugging resources)

I use a physical device to run the app on API30 before but I reduced it to 27 because of the tutorial. I also tried Invalidating caches and restarting it and had no luck.

I would be really grateful if someone give a solution for this...!



Read more here: https://stackoverflow.com/questions/64401100/error-in-fixing-dynamic-fragments-and-api-27-is-not-working

Content Attribution

This content was originally published by Roma at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: