1

I am trying to make a fragment that will chang everytime I pick another item from a listview. when my file is complying I get this error.

(part of) my code:

public class SongListActivity : AppCompatActivity, ListView.IOnItemClickListener, ListView.IOnItemLongClickListener
    {
....
AndroidX.Fragment.App.FragmentManager fragmentManager;
        infoFragment infoFragment1;  
....
protected override void OnCreate(Bundle savedInstanceState)
        {    
....  
SetContentView(Resource.Layout.SongList);
            fragmentManager = SupportFragmentManager;
            FragmentTransaction transaction = fragmentManager.BeginTransaction();
             infoFragment1 = new infoFragment();
            transaction.Add(Resource.Id.info, infoFragment1, "tag1");
            transaction.Commit();
          }
....

this is the XML file of this activity:

<?xml version="1.0" encoding="utf-8" ?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
    android:padding="5dp"
    android:background="@drawable/background3"
    
   >
    <androidx.appcompat.widget.Toolbar
        android:minWidth="30px"
        android:minHeight="30px"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/toolbar1" />

    <EditText  
        android:id="@+id/evSearch"  
        android:layout_width="fill_parent"  
        android:layout_height="40dp"  
        android:hint="  "  
        android:inputType="textVisiblePassword"  
        android:background="#FDF3FA"
        android:layout_marginBottom="10dp"   
        android:layout_marginTop="10dp"
        android:fontFamily="serif"
        android:textColor="#000000"/>
    <fragment
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:id="@+id/fragmentinfo"
         />

   <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:id="@id/info"
         />

        <ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/listView"
    android:divider="#dddddd"
    android:dividerHeight="1dp"
    />
       
</LinearLayout>

this is the fragment's class

public class infoFragment : Fragment
    {
        View view;
        Communicator c;
        TextView tvTitle, tvArtists;
        ImageView iv1, iv2, iv3, iv4, iv5, iv6, iv7, iv8, iv9, iv10;
        List<Chord> list, chordlist;
        public override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            Log.Debug("app", "OnCreate fragment");
            // Create your fragment here
            list = new List<Chord>();
            chordlist = new List<Chord>();
            chordlist = MainActivity.chordList;
        }
        public infoFragment()
        {

        }
        public override void OnAttach(Context context)
        {
            base.OnAttach(context);

            if (context.GetType() == typeof(AppCompatActivity)){
                c = (Communicator)context;
                Log.Debug("Freddie", "Mercury");
            }
            else Log.Debug("Freddie", "mmmmm");

        }
        public override View OnCreateView(LayoutInflater inflater, ViewGroup container,
                    Bundle savedInstanceState)
        {
            // Inflate the layout for this fragment
            View view = inflater.Inflate(Resource.Layout.fragment_info, container, false);
            tvTitle = (TextView)view.FindViewById(Resource.Id.tvFragment);
            tvArtists = (TextView)view.FindViewById(Resource.Id.tvArtist);
            iv1 = (ImageView)view.FindViewById(Resource.Id.iv1);
            iv2 = (ImageView)view.FindViewById(Resource.Id.iv2);
            iv3 = (ImageView)view.FindViewById(Resource.Id.iv3);
            iv4 = (ImageView)view.FindViewById(Resource.Id.iv4);
            iv5 = (ImageView)view.FindViewById(Resource.Id.iv5);
            iv6 = (ImageView)view.FindViewById(Resource.Id.iv6);
            iv7 = (ImageView)view.FindViewById(Resource.Id.iv7);
            iv8 = (ImageView)view.FindViewById(Resource.Id.iv8);
            iv9 = (ImageView)view.FindViewById(Resource.Id.iv9);
            iv10 = (ImageView)view.FindViewById(Resource.Id.iv10);
            return view;

        }
        public override void OnActivityCreated(Bundle savedInstanceState)
        {
            base.OnActivityCreated(savedInstanceState);
            Log.Debug("app", "OnActivityCreated");
        }
        public List<Chord> convertStringToList()// פעולה שתחזיר רשימה של פריטים מטיפוס אקורד, לאחר המרה של מחרוזת
        {
            List<Chord> l = new List<Chord>();// רשימת אקורדים שאליה נכניס את כל הפריטים
            string ch = "";// משתנה מטיפוס מחרוזת שאליו נשרשר תווים 
            string cl = "";
            cl = AddNewSongActivity.cl;
            for (int i = 0; i < cl.Length; i++)// לולאה שתרוץ כאורך המחרוזת
            {
                while (cl[i] != '/')//לולאה שתרוץ עד שיופיע הסימן המפריד שקבעתי
                {
                    ch += cl[i];//שרשור של תת המחרוזת למחרוזת
                }
                foreach (var item in chordlist)//לולאה שתרוץ על כל פריט ברשימת האקורדים 
                {
                    if (item.getChordName() == ch)//אם שם הפריט תואם את המחרוזת בדיוק
                        l.Add(item);  //נוסיף את הפריט לרשימה
                }
                ch = ""; // אתחול הערך של המחרוזת כדי שנוכל לעשות את הפעולות האלו על כל המחרוזת

            }
            return l;//החזרת הרשימה
        }
        public override void OnStart()
        {
            base.OnStart();
            Log.Debug("app", "OnStart activity");
        }
        public override void OnResume()
        {
            base.OnResume();
            Log.Debug("app", "OnResume activity");

        }
        public void displaySong(Song s)
        {
            tvTitle.Text = s.getSongName();
            tvArtists.Text = s.GetArtistName();
            list = convertStringToList();
            displayChords(s);

        }

I was trying to compile. When I try it, I get "error inflating class Fragment" and I can't understand why.I hope it is detailed enough:/. I would love to get your help. Thank you!

2 Answers2

0

You can try to make the SongListActivity inherit from the FragmentActivity, such as:

using AndroidX.Fragment.App;

public class SongListActivity : FragmentActivity, ListView.IOnItemClickListener, ListView.IOnItemLongClickListener
    {
....
AndroidX.Fragment.App.FragmentManager fragmentManager;
        infoFragment infoFragment1;  
....
protected override void OnCreate(Bundle savedInstanceState)
        {    
....  
SetContentView(Resource.Layout.SongList);
            fragmentManager = SupportFragmentManager;
            FragmentTransaction transaction = fragmentManager.BeginTransaction();
             infoFragment1 = new infoFragment();
            transaction.Add(Resource.Id.info, infoFragment1, "tag1");
            transaction.Commit();
          }
....

For more information, you can refer to the official document about using Fragment in the xamarin.android and the old case about Error inflating class fragment in the native android.

Liyun Zhang - MSFT
  • 8,271
  • 1
  • 2
  • 14
0

You are really trying to do this the hard way. Why not use Google's NavigationComponent (SingleActvity/multiple fragments) where you don't have to be concerned with Fragment Transactions?

Take a look at the modern way of writing either Xamarin.Android or android-net7 apps at https://github.com/gmck - specifically the NavigationGraph series.

user2153142
  • 431
  • 1
  • 4
  • 7