9
votes

I know similar questions like this has been answered but I seem to have done what the tutorial suggested and reading from other posts but still I get the Inflator error. Not sure what I am doing wrong. Please check the code. Thanks a lot.

Edit: I have two fragments in an activity. I have a button in one fragment and a textview in the other. I am trying to understand how fragments work so I made this code myself after reading the documentation. When the button is triggered in one fragment it sends a value to the other fragment in the activity. I am not able to get the fragment to inflate

MainActivity.java

        import com.transport.mbtalocpro.PredictedTimeFragment.ButtonClickListener;
        import android.os.Bundle;
        import android.support.v4.app.FragmentActivity;


        public class MainActivity extends FragmentActivity implements ButtonClickListener {

            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
            }


            @Override
            public void sendCounter(int count) {
                TestFrag testF = (TestFrag) getSupportFragmentManager().findFragmentById(R.id.bottomHalf);
                if(testF != null) testF.setCounter(String.valueOf(count));

            }

        }

PredictedTimeFragment.java

            import android.app.Activity;
            import android.os.Bundle;
            import android.support.v4.app.Fragment;
            import android.view.InflateException;
            import android.view.LayoutInflater;
            import android.view.View;
            import android.view.View.OnClickListener;
            import android.view.ViewGroup;
            import android.widget.Button;

            public class PredictedTimeFragment extends Fragment {


            ButtonClickListener bListener;

            public interface ButtonClickListener {
                public void sendCounter(int count);
            }

            public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
                super.onCreateView(inflater, container, savedInstanceState);
                try {
                    View view = inflater.inflate(R.layout.prediction_time, container, false);
                    Button button = (Button) view.findViewById(R.id.test);
                    button.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            test(v);

                        }
                    });
                } catch(InflateException e) {
                    e.printStackTrace();
                    System.out.println("Predicted Time Fragment");
                }
                return super.onCreateView(inflater, container, savedInstanceState);
            }

            public void onAttach(Activity activity) {
                  super.onAttach(activity);
                  try {
                    bListener = (ButtonClickListener) activity;
                  } catch (ClassCastException e) {
                     System.out.println("must implemenet Listener");
                  }
                }


            public void test(View view) {
                bListener.sendCounter(23);
            }

        }

TestFrag.java

        import android.os.Bundle;
        import android.support.v4.app.Fragment;
        import android.view.InflateException;
        import android.view.LayoutInflater;
        import android.view.View;
        import android.view.ViewGroup;
        import android.widget.TextView;

        public class TestFrag extends Fragment {

            @Override
            public View onCreateView(LayoutInflater inflator, ViewGroup container, Bundle savedInstanceState) {
                super.onCreateView(inflator, container, savedInstanceState);
                try {
                    View view = inflator.inflate(R.layout.test_frag, container, false);
                } catch(InflateException e) {
                    e.printStackTrace();
                    System.out.println("Test Fragment");
                }
                return super.onCreateView(inflator, container, savedInstanceState);
            }

            public void setCounter(String textS) {
                TextView text = (TextView) getView().findViewById(R.id.test1);
                text.setText((String) textS);
            }
        }

activity_main.xml

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

            <fragment 
                android:id="@+id/topHalf"
                android:layout_width="fill_parent"
                android:layout_height="0dp"
                android:layout_weight="0.5"
                android:name="com.transport.mbtalocpro.PredictedTimeFragment"/>

            <fragment
                android:id="@+id/bottomHalf"        
                android:layout_width="fill_parent"        
                android:layout_height="0dp"
                android:layout_weight="0.5"
                android:name ="com.transport.mbtalocpro.TestFrag"/>

        </LinearLayout>

predicted_time.xml

        <?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" >

            <Button 
                android:id="@+id/test"
                android:text="Test"
                android:layout_width="fill_parent"
                android:layout_height="30dp"
                />

        </LinearLayout>

test_frag.xml

        <?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:id="@+id/test1"
                android:layout_width="fill_parent"
                android:layout_height="40dp"/>

        </LinearLayout>

I am trying to support pre-honey comb versions too and I think I am importing all the support libraries.

5
throwing code around will probably not encourage someone to help you. Some basic details would be awesome.WarrenFaith
@WarrenFaith - I added some details about the code. Sorry about that. I was in a hurry. Thanks.Guru
You don't need either of the super calls in your fragments over ride of onCreateView; just return your view.wkhatch

5 Answers

3
votes

Delete your import statement:

import android.support.v4.app.FragmentActivity;

And import again with the pop-up import suggestion. I don´t know why but it always works for me.

import android.support.v4.app.FragmentActivity; 
1
votes

To fix the error:

In Manifest:

<application
        android:largeHeap="true"

In .xml file, you must remove the below code:

android:src="@drawable..
android:background="@drawable...

Because the error is out of memory error

1
votes

In the onCreateView callbacks you have to return the view, which you inflated. This way the system knows which View to draw. Otherwise, if you just return super.onCreateView(...); this way, no UI will appear.

Try this:

public View onCreateView(LayoutInflater inflater,
                         ViewGroup container,
                         Bundle savedInstanceState) {
    super.onCreateView(inflater, container, savedInstanceState);
    try {
        View view = inflater.inflate(R.layout.prediction_time, container, false);
        Button button = (Button) view.findViewById(R.id.test);
        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                test(v);
            }
        });
    } catch(InflateException e) {
        e.printStackTrace();
        System.out.println("Predicted Time Fragment");
    }
    return view;
}

Change the return value of the other Fragment accordingly.

0
votes

Try replacing the fragment in your xml with android.support.v4.app.Fragment

EDIT: Also try to call super when you are done with your views in onCreateView i.e only once. remove the call to the super in the beginning and see if that helps

0
votes

change your layout widget id ,it works for me.