Android Fragment no view found for ID?

前端 未结 30 1945
逝去的感伤
逝去的感伤 2020-11-22 05:33

I have a fragment I am trying to add into a view.

FragmentManager fragMgr=getSupportFragmentManager();
feed_parser_activity content = (feed_parser_activity)f         


        
相关标签:
30条回答
  • 2020-11-22 05:54

    In my case. I have an Activity with serveral Fragments some time I need to recreate Fragments when

  • language is change
  • fragment layout some need some not need or content change need to recreate
  • other changes
  • I clear all Fragments and set all to null in activity but Fragment already create itself, while it host activty is bean set null, so before call Fragment view check it null

    for example

    Activity{
        fragment
        recreate{
           fragment = null then new instance
        }
    
    }
    
    Fragment{
        if((activity).fragment != null) {
           findViewById()
        }
    
    }
    
0 讨论(0)
  • 2020-11-22 05:55

    Sometimes it is because you are using a BottomNavigationView. If you open an Intent from the navigation and in that activity you open a fragment lets say

    transaction.replace(R.id.container,new YourFragment());
    

    then the Activity won't be able to find the navigation method you are using.

    SOLUTION: Change the activity to fragment and handle navigation with addOnBackStack in your app. If you've implemented the Jetpack Navigation just use fragments in your project.

    0 讨论(0)
  • 2020-11-22 06:02

    I got this error when I upgraded from com.android.support:support-v4:21.0.0 to com.android.support:support-v4:22.1.1.

    I had to change my layout from this:

    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/container_frame_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </FrameLayout> 
    

    To this:

    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <FrameLayout
            android:id="@+id/container_frame_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
        </FrameLayout>
    
    </FrameLayout> 
    

    So the layout MUST have a child view. I'm assuming they enforced this in the new library.

    0 讨论(0)
  • 2020-11-22 06:02

    It happens also when you have two views in two fragments with the same ids

    0 讨论(0)
  • 2020-11-22 06:05

    This error also occurs when having nested Fragments and adding them with getSupportFragmentManager() instead of getChildFragmentManager().

    0 讨论(0)
  • 2020-11-22 06:06

    I had this same issue, let me post my code so that you can all see it, and not do the same thing that I did.

    @Override
    protected void onResume()
    {
        super.onResume();
    
        fragManager = getSupportFragmentManager();
    
        Fragment answerPad=getDefaultAnswerPad();
        setAnswerPad(answerPad);
        setContentView(R.layout.abstract_test_view);
    }
    protected void setAnswerPad(AbstractAnswerFragment pad)
    {
        fragManager.beginTransaction()
            .add(R.id.AnswerArea, pad, "AnswerArea")
            .commit();
        fragManager.executePendingTransactions();
    }
    

    Note that I was setting up fragments before I setContentView. Ooops.

    0 讨论(0)
  • 提交回复
    热议问题