java.lang.IllegalArgumentException:找不到id 0x7f080076的视图

时间:2018-04-10 15:16:43

标签: android android-fragments

我有一个底部导航活动,在底部导航活动中,我有3个图标Home,Reminders,settings。

为此我有3个片段,Home片段,Reminder片段和设置片段。除了Home片段之外,我没有片段中的任何片段都是空片段。在Home Fragment中我有一个Tab布局,但我没有添加任何标签布局。

运行项目后,不显示Home片段(默认情况下启动app)。它仅显示底部导航视图。

如果我点击提醒,图标应用会因以下错误而崩溃。

错误:

java.lang.IllegalArgumentException: No view found for id 0x7f080076 (com.aviz.www.reminder:id/reminders_Fragment) for fragment Reminders{2eca87b #0 id=0x7f080076}
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1413)
        at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1740)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1809)
        at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:799)
        at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2580)
        at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2367)
        at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2322)
        at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2229)
        at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:700)
        at android.os.Handler.handleCallback(Handler.java:789)
        at android.os.Handler.dispatchMessage(Handler.java:98)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6541)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

MainActivity.xml

 <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/Conslayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

 <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

        <android.support.design.widget.BottomNavigationView
            android:id="@+id/navigation"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginEnd="0dp"
            android:layout_marginStart="0dp"
            android:background="?android:attr/windowBackground"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:menu="@menu/navigation" />

    </android.support.constraint.ConstraintLayout>

MainActivity

public class MainActivity extends AppCompatActivity
                          implements Home.OnFragmentInteractionListener,
                          Reminders.OnFragmentInteractionListener,
                          Settings.OnFragmentInteractionListener {
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
    }

    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.navigation_home:
                    switchToHomeFragment();
                    break;
                case R.id.navigation_dashboard:
                    switchToRemindersFragment();
                    break;
                case R.id.navigation_notifications:
                    switchToSettingsFragment();
                    break;
            }
            return false;
        }
    };

    public void switchToHomeFragment() {
        FragmentManager manager = getSupportFragmentManager();
        manager.beginTransaction().replace(R.id.home_Fragment, new Home()).commit();
    }
    public void switchToRemindersFragment() {
        FragmentManager manager = getSupportFragmentManager();
        manager.beginTransaction().replace(R.id.reminders_Fragment, new Reminders()).commit();
    }
    public void switchToSettingsFragment() {
        FragmentManager manager = getSupportFragmentManager();
        manager.beginTransaction().replace(R.id.settings_Fragment, new Settings()).commit();
    }

    @Override
    public void onFragmentInteraction(Uri uri) {

    }
}

Home Fragment XML

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/home_Fragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".Fragments.Home">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_weight="1"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/AppTheme.PopupOverlay"
            app:title="@string/app_name">

        </android.support.v7.widget.Toolbar>

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            style="@style/MyCustomTabLayout"
            android:layout_height="wrap_content">

            <android.support.design.widget.TabItem
                android:id="@+id/tabItem_Today"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Today" />

            <android.support.design.widget.TabItem
                android:id="@+id/tabItem_ThisWeek"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="This Week" />

            <android.support.design.widget.TabItem
                android:id="@+id/tabItem_ThisMonth"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="This Month" />

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

    <android.support.v4.view.ViewPager
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />



</FrameLayout>

Home.java

public class Home extends Fragment {

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    private String mParam1;
    private String mParam2;

    private OnFragmentInteractionListener mListener;

    public Home() {

    }

    public static Home newInstance(String param1, String param2) {
        Home fragment = new Home();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

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

    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    public interface OnFragmentInteractionListener {
        void onFragmentInteraction(Uri uri);
    }
}

在Home.java中,当我创建Home片段时,所有代码都是自动生成的代码。

1 个答案:

答案 0 :(得分:1)

在这一行:

manager.beginTransaction().replace(R.id.reminders_Fragment, new Reminders()).commit();

R.id.reminders_Fragment 

是一个容器ID,它将被Remainders Fragment包裹,但我看不到你的Home布局中有这个id的任何容器

即使下面的代码也有同样的问题,因为你没有使用任何容器 您家庭布局中的settings_Fragment ID:

manager.beginTransaction().replace(R.id.settings_Fragment, new Settings()).commit();

如果您希望每次显示一个片段时添加一个FrameLayout并将其id用于容器,否则对每个片段使用一个FrameLayout并使用FrameLayouts的id进行事务

您可以使用以下代码选择简历中的默认标签:

onResume(){
   super.onResume();
   if(tabLayout.getSelectedTabPosition == -1)
       tabLayout.getTabAt(0).select();
}