无法在片段中添加AddView

时间:2017-10-22 08:40:47

标签: java android android-layout android-fragments android-xml

我一直在尝试将我使用类创建的视图添加到布局中,但是我几天都收到了以下错误:

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

FullStackTrace如下:

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) 

我已将错误向下钻取到此代码行,并且我尝试传递给方法的变量都不为null。导致错误的以下代码如下:

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);
}

我怀疑它与小部件有关,这里是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>

我还研究过错误是否可能与AddView相关联但是我没有找到解决方案。我期待大家的建议!提前致谢 !!干杯!

1 个答案:

答案 0 :(得分:0)

此错误表示您尚未使用其关联的资源ID初始化swipeView,或者您传递给TinderCard的上下文存在一些问题。尝试在TinderCard中传递 getActivity()而不是mContext,并检查它是否有效。