安卓 "No view found for id for fragment"

标签 android fragment illegalargumentexception

好的。我被困住了并且头痛......我不知道如何访问其他布局的 View ,因为膨胀不起作用。 这是我的代码。

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}

有人可以帮忙吗?

最佳答案

发生此错误的原因是 fragment 管理器找不到必须在其上膨胀 fragment 的 View 。

fragment 事务链接到 Activity ,因此会发生此错误,因为框架布局不是该主 Activity xml 的一部分。因此它无法找到添加 fragment 的位置。必须在 Activity 内添加 fragment 。

您要做的就是在主要 Activity 中提供 View 的 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>

您尝试膨胀 fragment 的 View 必须位于 Activity 内部。 现在当你尝试时

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

fragment 将加载到您的 Activity 的此 View 上。

关于安卓 "No view found for id for fragment",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42549606/

相关文章:

java.lang.IllegalArgumentException : No view found for id 0x7f090047 ("project name":id/content) for fragment FmMenu 异常

android - 未找到 com.google.android.gms.internal.zzaja 的类文件

android - 如何在深色背景的 ActionBarSherlock 中更改搜索 View 的自动完成文本颜色?

android - viewpager 的第一页根本没有加载

具有动态高度的Android ViewPager

java.lang.IllegalArgumentException : class java. text.DecimalFormat 声明了多个名为 maximumIntegerDigits 的 JSON 字段

android - 应用程序未运行时如何堆叠 Firebase Cloud Messaging 通知?

java - 我需要使用操作栏上的 SearchView 小部件开始 Activity

android - 使用 AsyncTask 序列化执行多个 fragment

java - 如果类位于包中,如何从命令行运行 JUnit 测试?