Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
766 views
in Technique[技术] by (71.8m points)

android - No view found for id for fragment

i am very much confused by this error ,what i am doing is that trying to replace the fragment within a DetailActivity which is DetailActivityFragment which got replaced onclick by the QuickFacts fragment

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

my quickfacts fragment

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>

my 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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Here container is your ViewGroup, So, container dosen't have your faragment_add, fragment_quick_facts.xml only has it now and which acts as your view currently. So please change

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

to

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

And also look

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

You are trying to replace fragment_quickfacts, which is in your fragment_quick_facts.xml not in fragment_detail.xml. So check it also or post your fragment_detail.xml too.

If you are not clear, then please go through Building a Dynamic UI with Fragments


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...