没有为片段的id找到视图

时间:2016-02-03 12:39:55

标签: android android-fragments

我对此错误感到非常困惑,我正在做的是尝试替换DetailActivity中的片段,这是一个由QuickFacts片段替换的DetailActivityFragment

public class DetailActivityFragment extends Fragment{

    TextView textView;
    ImageView imageView;
    TextView details;
    Button button;
    public DetailActivityFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_detail, container, false);


       Bundle getBundle = getActivity().getIntent().getExtras();

        String name = getBundle.getString("NAME");
        textView= (TextView) view.findViewById(R.id.bioname);
        textView.setText(name);
        imageView= (ImageView) view.findViewById(R.id.imageView);
        imageView.setImageResource(getBundle.getInt("IMAGE"));
        Log.v("test", "images are coming");
         button= (Button) view.findViewById(R.id.button_quickfacts);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Fragment fragment = new QuickFacts();
                FragmentTransaction fragmentTransaction =
                       getFragmentManager().beginTransaction();

                fragmentTransaction.replace(R.id.fragment_quickfacts, fragment);
                fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
                fragmentTransaction.addToBackStack(null);
                fragmentTransaction.commit();
            }
        });
        return view;
    }
}

我的quickfacts片段

public class QuickFacts extends Fragment {

    TextView detailText;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
              View view=inflater.inflate(R.layout.fragment_quick_facts,  container, false);
              Bundle getBundle = getActivity().getIntent().getExtras();
               String details=getBundle.getString("DETAIL");
               detailText= (TextView) container.findViewById(R.id.fragment_add);
               detailText.setText(details);

               return view;
    }
}

fragment_quick_facts.xml

<FrameLayout 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:id="@+id/fragment_quickfacts"
      tools:context="com.example.rahul.famousbiography.fragments.QuickFacts">

    <!-- TODO: Update blank fragment layout -->
       <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/hello_blank_fragment"
        android:id="@+id/fragment_add"
        />

 </FrameLayout>

我的logcat

FATAL EXCEPTION: main
   Process: com.example.rahul.famousbiography, PID: 11751
   java.lang.IllegalArgumentException: No view found for id 0x7f0c007d (com.example.rahul.famousbiography:id/fragment_quickfacts) for fragment QuickFacts{2ff08f2b #1 id=0x7f0c007d}
       at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1059)
       at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1248)
       at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
       at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1613)
       at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:517)
       at android.os.Handler.handleCallback(Handler.java:739)
       at android.os.Handler.dispatchMessage(Handler.java:95)
       at android.os.Looper.loop(Looper.java:135)
       at android.app.ActivityThread.main(ActivityThread.java:5254)
       at java.lang.reflect.Method.invoke(Native Method)
       at java.lang.reflect.Method.invoke(Method.java:372)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698

1 个答案:

答案 0 :(得分:3)

此处容器是您的ViewGroup,因此,容器不会让您的faragment_addfragment_quick_facts.xml现在只有它,而且当前只有view。 所以请更改

detailText= (TextView) container.findViewById(R.id.fragment_add);

detailText= (TextView) view.findViewById(R.id.fragment_add);

还要看

fragmentTransaction.replace(R.id.fragment_quickfacts, fragment);

您正在尝试替换fragment_quickfacts中的fragment_quick_facts.xml,而不是fragment_detail.xml。因此,请检查它或发布您的fragment_detail.xml

如果您不清楚,请通过Building a Dynamic UI with Fragments