Android "No view found for id for fragment"

Android "No view found for id for fragment"

好的。我被卡住了并且头疼......我不确定如何访问其他布局的视图,因为膨胀不起作用。 这是我的代码。

WriteRouteActivity.java

public class WriteRouteActivity extends AppCompatActivity {

    private Toolbar tb;
    private TextView txt_toolbar_title;
    private Button btnSearchPlaces;
    private LinearLayout parentLayout, placesCoverLayout;
    private View popupView;
    private ImageView imgShowPlaces;
    private boolean isKeyBoardVisible;
    private int keyboardHeight;
    private EditText edtSearchPlaces;
    private PopupWindow popupWindow;

    //popupView
    private TabLayout tabLayout;
    private FrameLayout frameLayout;

    //prework
    private int minusVal;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_write_route);
        initView();

    }

    private void initView() {
        //for activity and native back button

        tb = (Toolbar) findViewById(R.id.nav_toolbar);
        setSupportActionBar(tb);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        txt_toolbar_title = (TextView) findViewById(R.id.txt_toolbar);

        parentLayout = (LinearLayout) findViewById(R.id.layout_parent);
        placesCoverLayout = (LinearLayout) findViewById(R.id.footer_for_places);
        imgShowPlaces = (ImageView) findViewById(R.id.img_show_places);
        edtSearchPlaces =(EditText) findViewById(R.id.edt_search_place);
        btnSearchPlaces = (Button) findViewById(R.id.btn_search_place);

        popupView = getLayoutInflater().inflate(R.layout.places_popup, null);
        tabLayout = (TabLayout) popupView.findViewById(R.id.tab_layout);
        frameLayout = (FrameLayout) popupView.findViewById(R.id.frame_layout);

        doWorkForLayotus();
    }

    private void doWorkForLayotus(){
        final float popUpheight = getResources().getDimension(R.dimen.keyboard_height);
        changeKeyboardHeight((int) popUpheight);
        enablePopUpView();
        setTabLayout();
        checkKeyboardHeight(parentLayout);
        enableFooterView();
    }

    public void setCurrentTabFragment(int position) throws IllegalAccessException, InstantiationException {
        String tag="";
        Fragment fr = null;
        Class frClass = null;
        FragmentManager frManager = getSupportFragmentManager();
        switch (position) {
            case 0:
                tag = "first";
                //hide
                if(frManager.findFragmentByTag("second")!=null){
                    frManager.beginTransaction().hide(frManager.findFragmentByTag("second")).commit();
                }
                if(frManager.findFragmentByTag("third")!=null){
                    frManager.beginTransaction().hide(frManager.findFragmentByTag("third")).commit();
                }
                if(frManager.findFragmentByTag("fourth")!=null){
                    frManager.beginTransaction().hide(frManager.findFragmentByTag("fourth")).commit();
                }

                //show
                if(frManager.findFragmentByTag("first")!=null){
                    frManager.beginTransaction().show(frManager.findFragmentByTag("first")).commit();
                }else{  //add
                    try {
                        frManager.beginTransaction().add(frameLayout.getId(), ((Fragment) Fragment_zasin.class.newInstance()), tag).commit();
                    }catch(Exception e){
                        Log.e("why", e.getMessage().toString());
                    }
                }
                break;
            case 1:
                tag = "second";
                //hide
                if(frManager.findFragmentByTag("first")!=null){
                    frManager.beginTransaction().hide(frManager.findFragmentByTag("first")).commit();
                }
                if(frManager.findFragmentByTag("third")!=null){
                    frManager.beginTransaction().hide(frManager.findFragmentByTag("third")).commit();
                }
                if(frManager.findFragmentByTag("fourth")!=null){
                    frManager.beginTransaction().hide(frManager.findFragmentByTag("fourth")).commit();
                }

                //show
                if(frManager.findFragmentByTag("second")!=null){
                    frManager.beginTransaction().show(frManager.findFragmentByTag("second")).commit();
                }else{  //add
                frManager.beginTransaction().add(frameLayout.getId(), ((Fragment) Fragment_zasin.class.newInstance()), tag).commit();
                }
                break;
            case 2:
                tag = "third";
                //hide
                if(frManager.findFragmentByTag("first")!=null){
                    frManager.beginTransaction().hide(frManager.findFragmentByTag("first")).commit();
                }
                if(frManager.findFragmentByTag("second")!=null){
                    frManager.beginTransaction().hide(frManager.findFragmentByTag("second")).commit();
                }
                if(frManager.findFragmentByTag("fourth")!=null){
                    frManager.beginTransaction().hide(frManager.findFragmentByTag("fourth")).commit();
                }

                //show
                if(frManager.findFragmentByTag("third")!=null){
                    frManager.beginTransaction().show(frManager.findFragmentByTag("third")).commit();
                }else{  //add
                    frManager.beginTransaction().add(frameLayout.getId(), ((Fragment) Fragment_zasin.class.newInstance()), tag).commit();
                }
                break;
            case 3:
                tag = "fourth";
                //hide
                if(frManager.findFragmentByTag("first")!=null){
                    frManager.beginTransaction().hide(frManager.findFragmentByTag("first")).commit();
                }
                if(frManager.findFragmentByTag("second")!=null){
                    frManager.beginTransaction().hide(frManager.findFragmentByTag("second")).commit();
                }
                if(frManager.findFramentByTag("third")!=null){
                    frManager.beginTransaction().hide(frManager.findFragmentByTag("third")).commit();
                }

                //show
                if(frManager.findFragmentByTag("fourth")!=null){
                    frManager.beginTransaction().show(frManager.findFragmentByTag("fourth")).commit();
                }else{  //add
                    frManager.beginTransaction().add(R.id.frame_layout, ((Fragment) Fragment_zasin.class.newInstance()), tag).commit();
                }
                break;
        }
        //frManager.beginTransaction().replace(R.id.frame_container, fr, tag).setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN).commit();
    }

    private void setTabLayout(){
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                try {
                    setCurrentTabFragment(tab.getPosition());
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InstantiationException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    private void enablePopUpView() {

        // Creating a pop window for emoticons keyboard
        popupWindow = new PopupWindow(popupView, ViewGroup.LayoutParams.MATCH_PARENT,
                (int) keyboardHeight, false);

        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {

            @Override
            public void onDismiss() {
                placesCoverLayout.setVisibility(LinearLayout.GONE);
            }
        });
    }

    int previousHeightDiffrence = 0;
    private void checkKeyboardHeight(final View parentLayout) {

        parentLayout.getViewTreeObserver().addOnGlobalLayoutListener(
                new ViewTreeObserver.OnGlobalLayoutListener() {
                    @Override
                    public void onGlobalLayout() {
                        Rect r = new Rect();
                        parentLayout.getWindowVisibleDisplayFrame(r);

                        int screenHeight = parentLayout.getRootView()
                                .getHeight();
                        minusVal=screenHeight-r.bottom;
                        int heightDifference = screenHeight - (r.bottom+(minusVal));

                        if (previousHeightDiffrence - heightDifference > 50) {
                            popupWindow.dismiss();
                        }

                        previousHeightDiffrence = heightDifference;
                        if (heightDifference > 100) {

                            isKeyBoardVisible = true;
                            changeKeyboardHeight(heightDifference);

                        } else {

                            isKeyBoardVisible = false;

                        }

                    }
                });
    }

    private void changeKeyboardHeight(int height) {

        if (height > 100) {
            keyboardHeight = height;
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, keyboardHeight);
            placesCoverLayout.setLayoutParams(params);
        }

    }

    private void enableFooterView() {
        edtSearchPlaces.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (popupWindow.isShowing()) {
                    popupWindow.dismiss();
                }
            }
        });

        btnSearchPlaces.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                hideSoftKeyboard(WriteRouteActivity.this);
                if(!popupWindow.isShowing()){
                    popupWindow.setHeight((int) (keyboardHeight));

                    if (isKeyBoardVisible) {
                        placesCoverLayout.setVisibility(LinearLayout.GONE);
                    } else {
                        placesCoverLayout.setVisibility(LinearLayout.VISIBLE);

                    }
                    popupWindow.setSoftInputMode(PopupWindow.INPUT_METHOD_NEEDED);
                    popupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
                    popupWindow.showAtLocation(parentLayout, Gravity.BOTTOM, 0, 0);

                    try {
                        setCurrentTabFragment(0);
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    } catch (InstantiationException e) {
                        e.printStackTrace();
                    }

                } else {
                    //popupWindow.dismiss();
                }

            }
        });
    }

    @Override
    protected void onDestroy() {
        popupWindow.dismiss();
        super.onDestroy();
    }


    public static void hideSoftKeyboard(Activity activity) {
        InputMethodManager inputMethodManager =
                (InputMethodManager) activity.getSystemService(
                        Activity.INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(
                activity.getCurrentFocus().getWindowToken(), 0);
    }

    @Override
    public void onBackPressed() {
        if(popupWindow.isShowing()){
            popupWindow.dismiss();
        }else {
            super.onBackPressed();
        }
    }
}

activity_write_wroute.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_parent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

    <include layout="@layout/nav_toolbar" />

    <fragment
        android:id="@+id/google_map"
        class="com.google.android.gms.maps.MapFragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />


    <include
        android:id="@+id/footer_layout"
        layout="@layout/footer_edittext"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <LinearLayout
        android:id="@+id/footer_for_places"
        android:layout_width="match_parent"
        android:layout_height="@dimen/keyboard_height"
        android:background="@android:color/transparent"
        android:orientation="vertical"
        android:visibility="gone" />
</LinearLayout>

Fragment_Zasin

public class Fragment_zasin  extends Fragment {
    public Fragment_zasin newInstance() {
        Fragment_zasin fr = new Fragment_zasin();
        return fr;
    }

    public Fragment_zasin() {

    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_zasin, container, false);
        return rootView;
    }
}

places_popup.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/linear_layout_top"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#fff"
    android:orientation="vertical">

    <android.support.design.widget.TabLayout
        android:id="@+id/tab_layout"
        android:layout_width="fill_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="#ffffff"
        app:tabGravity="fill"
        app:tabIndicatorColor="@color/colorPrimary"
        app:tabIndicatorHeight="4dp"
        app:tabMode="fixed"
        app:tabSelectedTextColor="@color/colorPrimary">

        <android.support.design.widget.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:icon="@drawable/tab_pin_selector"
            android:text="11">

        </android.support.design.widget.TabItem>

        <android.support.design.widget.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:icon="@drawable/tab_mainroute_selector"
            android:text="22" />

        <android.support.design.widget.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:icon="@drawable/tab_talk_selector"
            android:text="33" />

        <android.support.design.widget.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:icon="@drawable/tab_my_selector"
            android:text="44" />

    </android.support.design.widget.TabLayout>


    <FrameLayout
        android:id="@+id/frame_layout"
        android:layout_width="match_parent"
        android:layout_height="0px"
        android:layout_weight="1" />

</LinearLayout>

LOGCAT留言

FATAL EXCEPTION: main Process: suacuration.itgotravel, PID: 20131 java.lang.IllegalArgumentException: No view found for id 0x7f100173 for fragment Fragment_zasin{d4ac39c #0 id=0x7f100173 first}

有人可以帮忙吗?

可能还有其他错误,但这是什么?

tb = (Toolbar) findViewById(R.id.nav_toolbar);

这不是工具栏:

<include layout="@layout/nav_toolbar" />

我们能看到这个 nav_toolbar 布局的内部吗?

此外,这是哪里:"R.layout.fragment_zasin"。它在抱怨一个片段,所以值得看看这是否可以。

无论如何,作为一般的故障排除策略,请尝试注释掉 initView() 方法中的所有这些行,然后一次只添加一个,直到它失败。或者,如果您进一步向下滚动错误日志,它会给您一个指向导致它失败的行的超链接。

为什么你的一行是 "frameLayout.getId()" 而另一行是 "R.id.frame_layout"?尝试对所有行使用后者,看看是不是这样。

发生此错误是因为片段管理器找不到它必须在其上扩充片段的视图。

片段事务链接到 activity,因此出现此错误是因为框架布局不是主 activity 的一部分 xml.so 它无法找到添加位置fragment.fragment 必须添加到 activity.

您需要做的是在主 activity 中提供视图的 ID。 例如你的主视图

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_parent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/mainView"//like this
android:orientation="vertical">

<include layout="@layout/nav_toolbar" />

<fragment
    android:id="@+id/google_map"
    class="com.google.android.gms.maps.MapFragment"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1" />


<include
    android:id="@+id/footer_layout"
    layout="@layout/footer_edittext"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<LinearLayout
    android:id="@+id/footer_for_places"
    android:layout_width="match_parent"
    android:layout_height="@dimen/keyboard_height"
    android:background="@android:color/transparent"
    android:orientation="vertical"
    android:visibility="gone" />
 </LinearLayout>

您尝试膨胀碎片的视图必须在 activity 内。 现在当你尝试

frManager.beginTransaction().add(R.id.mainView, ((Fragment) Fragment_zasin.class.newInstance()), tag).commit();

片段将加载到您 activity 的这个视图中。

问题是您正试图在 View 上显示 Fragment,而您在 onCreate() 中定义的布局中未定义 setContentView(layoutId)

在你的情况下,你在 WriteRouteActivity 中膨胀片段,其中布局定义为 activity_write_route,片段添加在 FrameLayout 上,FrameLayout 定义在 places_popup.xml 中,所以定义你的 framelayout 在 Activity 的视图布局中。

小说明:

--------code------
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
-------layout define for activity-------
setContentView(R.layout.activity_write_route);
}

现在您要在 FrameLayout(view)

上添加片段的位置
frManager.beginTransaction().add(R.id.frame_layout, ((Fragment) Fragment_zasin.class.newInstance()), tag).commit();

此处 R.id.frame_layout 应在您的布局中定义 activity_write_route

您收到此错误是因为 您的视图在片段 Fragment_zasin 中找到了 ID 0x7f100173,所以唯一的解决方案是:

1) 检查该特定布局中存在的所有 ID。

2) 如果所有 id 都存在于该特定布局,但您仍然面临同样的问题,请做一件事更改 id 的名称不要重构更改名称并在您的片段中访问此更改的 id 名称或 activity .

好的,我找到了答案。 问题是片段不能是 'Dialog' 的子片段。 由于我使用了弹出对话框,因此无法将片段放入对话框中。

我通过膨胀视图而不是在 tablayout 中使用片段来解决。

我扩展了错误类型的 activity,扩展的 AppCompact 解决了问题。

在事务替换方法中使用 ChildFragmentManager 从片段开始新片段并从 activity 给出容器 ID 时出现此错误。 碰巧 FragmentManager 可以访问 activity 容器,但 ChildFragmentManager 可以访问片段容器。 解决方案是使用 FragmentManager class 而不是 ChildFragmentManager.