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

时间:2016-05-14 18:04:31

标签: java android android-fragments

我正在尝试使用add(R.id.containerId, fragment);从活动中添加片段。但是,日志会显示java.lang.IllegalArgumentException: No view found for id错误。

片段布局在根元素中包含id。我知道将容器id放在子元素中应该可以解决问题,但这对我没有任何影响。

以下是片段布局xml文件中的根元素

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#f2f2f2"
    android:orientation="vertical"
    tools:context=".view.fragment.Fragment">

以下是我的活动中的 onCreate()方法:

FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.add(R.id.fragment_container, fragment);
currentFragment = R.id.fragment_container;
transaction.commit();

以下是片段中的 onCreateView()方法:

return inflater.inflate(R.layout.fragment, container, false);

以下是整个活动代码(命名和ID不同,因为我在上面的代码段中更改了它们,为简洁起见,还缺少rails语句):

public class MainActivity extends AppCompatActivity {

    private int currentFragmentId;

    private FeedFragment feedFragment = new FeedFragment();
    private EventsFragment eventsFragment = new EventsFragment();
    private SearchFragment searchFragment = new SearchFragment();
    private MoreFragment moreFragment = new MoreFragment();
    // Add other fragments

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Toolbar toolbar = (Toolbar) findViewById(R.id.main_toolbar);
        setSupportActionBar(toolbar);

        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.add(R.id.fragment_feed_container, feedFragment);
        currentFragmentId = R.id.fragment_feed_container;
        transaction.commit();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.menu.main_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_feed:
                // Add the feed fragment
                replaceFragment(feedFragment, currentFragmentId);
                currentFragmentId = R.id.fragment_feed_container;
                return true;

            case R.id.action_events:
                // Add the events fragment
                replaceFragment(eventsFragment, currentFragmentId);
                currentFragmentId = R.id.fragment_events_container;
                return true;

            case R.id.action_search:
                // Add the search fragment
                replaceFragment(searchFragment, currentFragmentId);
                currentFragmentId = R.id.fragment_search_container;
                return true;

            case R.id.action_more:
                // Add the more fragment
                replaceFragment(moreFragment, currentFragmentId);
                currentFragmentId = R.id.fragment_more_container;
                return true;

            default:
                // If we got here, the user's action was not recognized.
                // Invoke the superclass to handle it.
                return super.onOptionsItemSelected(item);
        }
    }

    /**
     * Replaces the current fragment with a new fragment
     *
     * @param newFragment
     * @param currentFragmentId
     */
    private void replaceFragment(Fragment newFragment, int currentFragmentId) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(currentFragmentId, newFragment);
        transaction.commit();
    }
}

以下是完整片段类(命名和ID不同,因为我在上面的片段中更改了它们,为简洁起见,也缺少imports语句):

public class FeedFragment extends Fragment {

    public View mView;

    public FeedFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        mView = inflater.inflate(R.layout.fragment_feed, container, false);
        return mView;
    }
}

以下是完整fragment_feed.xml 文件的代码(命名和ID不同,因为我在上面的代码段中更改了它们,为简洁起见,也缺少imports语句):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/fragment_feed_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#f2f2f2"
    android:orientation="vertical"
    tools:context=".view.fragment.FeedFragment">

    <!-- A CardView that contains a TextView -->
    <android.support.v7.widget.CardView
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:id="@+id/card_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="15dp"
        card_view:cardBackgroundColor="#fff"
        card_view:cardCornerRadius="0dp"
        card_view:cardElevation="3dp">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <LinearLayout
                android:id="@+id/top_card_linear_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="12dp"
                android:layout_marginRight="12dp"
                android:layout_marginTop="6dp"
                android:orientation="horizontal">

                <ImageView
                    android:id="@+id/card_profile_image_view"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="start" />

                <TextView
                    android:id="@+id/card_info_text"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="6"
                    android:gravity="start"
                    tools:text="Julien Durrand invited you" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="end"
                    tools:text="3 months" />

            </LinearLayout>

            <ImageView
                android:id="@+id/card_main_image_view"
                android:layout_width="match_parent"
                android:layout_height="150dp"
                android:layout_below="@+id/top_card_linear_layout"
                android:layout_marginLeft="12dp"
                android:layout_marginRight="12dp"
                android:layout_marginTop="12dp" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/card_main_image_view"
                android:layout_marginLeft="12dp"
                android:layout_marginRight="12dp"
                android:layout_marginTop="6dp"
                android:orientation="horizontal">

                <ImageView
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="start" />

                <TextView
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="2"
                    android:gravity="start"
                    tools:text="30K" />

                <ImageButton
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="2"
                    android:gravity="center" />

                <ImageButton
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="2"
                    android:gravity="center" />

                <ImageButton
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="2"
                    android:gravity="center" />

            </LinearLayout>

        </RelativeLayout>

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

</LinearLayout>

以下是logcat堆栈跟踪的主要部分(命名和ID不同,因为我在上面的代码段中更改了它们,为简洁起见,还缺少import语句):

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tomfinet.magpie/com.tomfinet.magpie.view.MainActivity}: java.lang.IllegalArgumentException: No view found for id 0x7f0c0076 (com.tomfinet.magpie:id/fragment_feed_container) for fragment FeedFragment{c9899d7 #0 id=0x7f0c0076}
Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f0c0076 (com.tomfinet.magpie:id/fragment_feed_container) for fragment FeedFragment{c9899d7 #0 id=0x7f0c0076}

我检查过的东西:

  1. 碎片布局在片段中的onCreateView()方法中是正确的。
  2. 片段xml文件的子片段中的片段布局容器ID。
  3. 如何解决此错误?

    谢谢。

0 个答案:

没有答案