找不到碎片的视图

时间:2014-12-04 02:45:30

标签: java android android-fragments fragment

许多人都在质疑修复片段,但我的困惑可能是基于片段API的演变本质,或者我只是误解了它们。我正在修剪此代码以显示尝试如何使用片段在大屏幕上具有多个布局或在较小屏幕上使用单个布局通过按钮转到其他布局。我的日志的重要部分是

    12-03 21:17:29.711: E/FragmentManager(13440): No view found for id 0x7f090046     
    (com.ruleagents.rpgsheet:id/main_fragment) for fragment MainFragment{4053b368 #0 ....
     java.lang.RuntimeException: Unable to start activity    
    ComponentInfo{com.ruleagents.rpgsheet/com.ruleagents.rpgsheet.SheetActivity}: 
    java.lang.IllegalArgumentException: No view found for id 0x7f090046 
    (com.ruleagents.rpgsheet:id/main_fragment) for fragment MainFragment{4053b368 #0 id=0x7f090046}
    12-03 21:17:29.721: E/AndroidRuntime(13440): at 
    android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1821)

我的activity_main.xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >    
      <FrameLayout 
       android:id="@+id/container"
        android:layout_width="0px"
        android:layout_height="match_parent"
        tools:context="com.ruleagents.rpgsheet.MainActivity"
        tools:ignore="MergeRootFrame" />
    </RelativeLayout>

attribute_fragment.xml

    <?xml version="1.0" encoding="utf-8"?>

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

    ... nested layout that works independently     

       </LinearLayout>  

main_fragment.xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent" android:orientation="vertical"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:id="@+id/main_fragment" >

          <LinearLayout

        ...
          </LinearLayout>

         <LinearLayout
        ...
         </LinearLayout>  

         <LinearLayout
         ...      
        </RelativeLayout>

将MainActivity.java作为:

    package com.ruleagents.rpgsheet;

    import java.io.BufferedReader;
    ...

    public class MainActivity extends FragmentActivity { //ActionBarActivity {

        SectionsPagerAdapter mSectionsPagerAdapter;
        FragmentManager fragmentManager; // = getFragmentManager();
        AttributeFragment af = new AttributeFragment();
        MainFragment mf = new MainFragment();

        String characterClass, level, att1, att2, att3;
        //other local variables

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

            Configuration config = getResources().getConfiguration();

            af.setArguments(getIntent().getExtras());
            mf.setArguments(getIntent().getExtras());
            fragmentManager = getSupportFragmentManager();

            if (config.orientation == Configuration.ORIENTATION_LANDSCAPE) {  
                fragmentManager.beginTransaction().add(R.id.main_fragment, mf).commit();
                fragmentManager.beginTransaction().add(R.layout.attribute_fragment, af).commit();
             }else{     
                System.err.println("AAA creating portrait view");
                fragmentManager.beginTransaction().add(R.id.container, mf).commit();
             }
        }

        public void onMainButton(View view) {
            android.support.v4.app.FragmentTransaction ft = fragmentManager.beginTransaction();
            ft.replace(R.id.main_fragment, mf);
            ft.addToBackStack(null);
            ft.commit();
        }

        public void onAttributesButton(View view) {
            android.support.v4.app.FragmentTransaction ft = fragmentManager.beginTransaction();
            ft.replace(R.layout.attribute_fragment, af);
            ft.addToBackStack(null);
            ft.commit();
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            int id = item.getItemId();
            if (id == R.id.action_settings) {
                return true;
            }
            return super.onOptionsItemSelected(item);
        }

        // Unused PlaceholderFragment method ...    
        // unused FragmentPageAdapter ...
        // local methods
    }

我的MainFragment.java

    package com.ruleagents.rpgsheet;

    ... imports

    public class MainFragment extends Fragment {

        public MainFragment() {
        }

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

        @Override
        public void onPause() {
            super.onPause();
        }

    }

我的SheetActivity.java

    package com.ruleagents.rpgsheet;

    ... imports

    public class SheetActivity extends ActionBarActivity {

         // Local variables...

            FragmentManager fragmentManager; // = getFragmentManager();
            AttributeFragment af = new AttributeFragment();
            MainFragment mf = new MainFragment();

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

                System.err.println("AAA in onCreate of SheetActivity");
                Configuration config = getResources().getConfiguration();

                //FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

                //setContentView(R.layout.activity_main);
                //setContentView(R.layout.whole_sheet);
                System.err.println("AAA got config");

                af.setArguments(getIntent().getExtras());
                mf.setArguments(getIntent().getExtras());
                fragmentManager = getSupportFragmentManager();

                System.err.println("AAA got frag manager");
                if (config.orientation == Configuration.ORIENTATION_LANDSCAPE) {  
                    fragmentManager.beginTransaction().add(R.id.container, mf).commit();
                    fragmentManager.beginTransaction().add(R.id.container, af).commit();
                 }else{     
                        System.err.println("AAA creating portrait view");
                        fragmentManager.beginTransaction().add(R.id.main_fragment, mf).commit();
                 }
            }

            public void onMainButton(View view) {
                android.support.v4.app.FragmentTransaction ft = fragmentManager.beginTransaction();
                ft.replace(R.id.main_fragment, mf);
                ft.addToBackStack(null);
                ft.commit();
            }

            public void onSkillsButton(View view) {
                android.support.v4.app.FragmentTransaction ft = fragmentManager.beginTransaction();
                ft.addToBackStack(null);
                ft.commit();
            }

            public void onAttributesButton(View view) {
                android.support.v4.app.FragmentTransaction ft = fragmentManager.beginTransaction();
                ft.replace(R.layout.attribute_fragment, af);
                ft.addToBackStack(null);
                ft.commit();
            }

            @Override
            public boolean onCreateOptionsMenu(Menu menu) {
                // Inflate the menu; this adds items to the action bar if it is present.
                getMenuInflater().inflate(R.menu.main, menu);
                return true;
            }

            @Override
            public boolean onOptionsItemSelected(MenuItem item) {
                int id = item.getItemId();
                if (id == R.id.action_settings) {
                    return true;
                }
                return super.onOptionsItemSelected(item);
            }

        // Local methods ...
        // Unused PlaceholderFragment  ...
        // Unused SectionsPagerAdapter 
        }

1 个答案:

答案 0 :(得分:0)

当您添加片段时,add()方法需要容器ID位于片段将进入的活动布局中。因此,您的错误是因为您的activity_main.xml中没有main_fragment id。