Home » Android » Unable to AddView in fragment

Unable to AddView in fragment

Posted by: admin November 1, 2017 Leave a comment

Questions:

I have been trying to add a view that i have created using a class into the layout but i have been getting this following error for days :

10-22 08:32:16.037 2607-2607/com.parse.starter I/perme: 
android.content.res.Resources$NotFoundException: Resource ID #0x0

FullStackTrace is as follow :

10-22 08:48:31.879 18805-18805/com.parse.starter E/AndroidRuntime: FATAL EXCEPTION: main
                                                               Process: com.parse.starter, PID: 18805
                                                               java.lang.RuntimeException: Unable to start activity ComponentInfo{com.parse.starter/com.parse.starter.baseMain}: android.content.res.Resources$NotFoundException: Resource ID #0x0
                                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
                                                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
                                                                   at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4483)
                                                                   at android.app.ActivityThread.-wrap19(ActivityThread.java)
                                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1466)
                                                                   at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                   at android.os.Looper.loop(Looper.java:154)
                                                                   at android.app.ActivityThread.main(ActivityThread.java:6077)
                                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
                                                                Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x0
                                                                   at android.content.res.ResourcesImpl.getValue(ResourcesImpl.java:190)
                                                                   at android.content.res.Resources.loadXmlResourceParser(Resources.java:2094)
                                                                   at android.content.res.Resources.getLayout(Resources.java:1111)
                                                                   at android.view.LayoutInflater.inflate(LayoutInflater.java:424)
                                                                   at com.mindorks.placeholderview.SwipePlaceHolderView.addView(SwipePlaceHolderView.java:221)
                                                                   at com.parse.starter.socialPeople$override.onCreateView(socialPeople.java:152)
                                                                   at com.parse.starter.socialPeople$override.access$dispatch(socialPeople.java)
                                                                   at com.parse.starter.socialPeople.onCreateView(socialPeople.java:0)
                                                                   at android.support.v4.app.Fragment.performCreateView(Fragment.java:2192)
                                                                   at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1299)
                                                                   at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1528)
                                                                   at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1595)
                                                                   at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:2900)
                                                                   at android.support.v4.app.FragmentController.dispatchActivityCreated(FragmentController.java:201)
                                                                   at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:603)
                                                                   at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:178)
                                                                   at com.parse.starter.baseMain.onStart(baseMain.java:358)
                                                                   at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1248)
                                                                   at android.app.Activity.performStart(Activity.java:6679)
                                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2609)
                                                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
                                                                   at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4483) 
                                                                   at android.app.ActivityThread.-wrap19(ActivityThread.java) 
                                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1466) 
                                                                   at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                   at android.os.Looper.loop(Looper.java:154) 
                                                                   at android.app.ActivityThread.main(ActivityThread.java:6077) 
                                                                   at java.lang.reflect.Method.invoke(Native Method) 
                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866) 
                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756) 

i have drill the error down to this line of code and the variables which i am trying to pass into the method all are not null. The following code causing the error are as follow :

public class socialPeople extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
private SwipePlaceHolderView mSwipeView;

private String mParam1;
private String mParam2;


Context mContext;

private profileFrag.OnFragmentInteractionListener mListener;

public static socialPeople newInstance() {
    socialPeople fragment = new socialPeople();
    return fragment;
}


public socialPeople() {
    // Required empty public constructor
}

public static socialPeople newInstance(String param1, String param2) {
    socialPeople fragment = new socialPeople();
    Bundle args = new Bundle();
    args.putString(ARG_PARAM1, param1);
    args.putString(ARG_PARAM2, param2);
    fragment.setArguments(args);
    return fragment;
}


@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);





}


@Override
public void onAttach(Context context) {
    super.onAttach(context);

    mContext = context;

}
int count = 1;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragmentschool,
            container, false);



    if(count == 1) {

        mSwipeView = (SwipePlaceHolderView) view.findViewById(R.id.swipeView2);

        try {

            mSwipeView.getBuilder()
                    .setDisplayViewCount(1)
                    .setSwipeDecor(new SwipeDecor()
                            .setPaddingTop(20)
                            .setRelativeScale(0.01f)
                            .setSwipeInMsgLayoutId(R.layout.activity_swipein)
                            .setSwipeOutMsgLayoutId(R.layout.activity_swipeout));


        } catch (Exception e) {

            Log.i("permedialog", "" + e.toString());

        }



            for (TinderProfile tinderprofile : loadProfiles(getActivity())) {
                TinderCard tinderCard = new TinderCard(getActivity(), tinderprofile, mSwipeView);
                mSwipeView.addView(tinderCard);

            }

        view.findViewById(R.id.rejectBtn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mSwipeView.doSwipe(false);


            }
        });


        view.findViewById(R.id.acceptBtn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mSwipeView.doSwipe(true);


            }
        });


        count -=1;
    }

    return view;

}



// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
    if (mListener != null) {
        mListener.onFragmentInteraction(uri);
    }
}

@Override
public void onDetach() {
    super.onDetach();
    mListener = null;
}


public interface OnFragmentInteractionListener {
    // TODO: Update argument type and name
    void onFragmentInteraction(Uri uri);
}

I suspect that it has something to do with the widget and here is the widget in the XML :

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.parse.starter.socialPeople">

<LinearLayout
    android:id="@+id/search"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:id="@+id/fi"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="end"
        android:orientation="horizontal"
        android:padding="10dp">


        <EditText
            android:id="@+id/txtsearch"
            android:layout_width="314dp"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="search"
            android:inputType="textPersonName"
            android:text="Name" />

        <ImageView
            android:id="@+id/spset"
            android:layout_width="37dp"
            android:layout_height="35dp"
            android:layout_marginTop="10dp"
            android:layout_weight="0.2"
            android:baselineAlignBottom="true"
            app:srcCompat="@drawable/search" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:padding="30dp">

        <FrameLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/grey">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="80dp"
                android:layout_gravity="bottom"
                android:gravity="center"
                android:orientation="horizontal">
                <ImageButton
                    android:id="@+id/rejectBtn"
                    android:layout_width="50dp"
                    android:layout_height="50dp"
                    android:text="cancel"/>
                <ImageButton
                    android:id="@+id/acceptBtn"
                    android:layout_width="50dp"
                    android:layout_height="50dp"
                    android:layout_marginLeft="30dp"
                    android:text="accept"/>
            </LinearLayout>

            <com.mindorks.placeholderview.SwipePlaceHolderView
                android:id="@+id/swipeView2"
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>



        </FrameLayout>
    </LinearLayout>


</LinearLayout>

I have also done research of if the error might be linked to AddView however i have come up with no solution. I look forward to everyone advice! Thanks in Advance !! Cheers!

Answers:

This error says that you have not initialised the swipeView with its associated resource id or there’s some problem with the context you are passing to TinderCard. Try passing getActivity() instead of mContext in your TinderCard and check if it works.