FragmentDrawer fragmentDrawer = getActivity()。getSupportFragmentManager()在我的编码中显示错误

时间:2015-09-09 17:28:26

标签: android android-fragments android-activity

在下一个代码中:

import android.support.v4.app.Fragment;
    import android.content.res.Configuration;
    import android.support.design.widget.NavigationView;
    import android.support.v4.app.FragmentManager;
    import android.support.v4.app.DialogFragment;
    import android.support.v7.app.ActionBar;
    import android.app.Activity;
    import android.os.Bundle;
    import android.support.v4.view.GravityCompat;
    import android.support.v4.widget.DrawerLayout;
    import android.support.v7.app.ActionBarDrawerToggle;
    import android.support.v7.app.AppCompatActivity;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.support.v7.widget.Toolbar;

    public class MainActivity extends AppCompatActivity {
        private DrawerLayout mDrawer;
        private Toolbar toolbar;
        private DrawerLayout dlDrawer;
        private ActionBarDrawerToggle drawerToggle;
        private NavigationView nvDrawer;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            toolbar=(Toolbar)findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);

            mDrawer =(DrawerLayout)findViewById(R.id.drawer_layout);
            dlDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);
            drawerToggle = setupDrawerToggle();
            nvDrawer = (NavigationView) findViewById(R.id.nvView);
            setupDrawerContent(nvDrawer);
            final ActionBar ab=getSupportActionBar();
            ab.setHomeAsUpIndicator(R.drawable.ic_one);
            ab.setDefaultDisplayHomeAsUpEnabled(true);

            dlDrawer.setDrawerListener(drawerToggle);

        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {

            getMenuInflater().inflate(R.menu.drawer_view, menu);
            return true;
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            switch(item.getItemId())
            {
                case R.id.home:
                    mDrawer.openDrawer(GravityCompat.START);
            }
            return super.onOptionsItemSelected(item);
        }

        @Override
        protected void onPostCreate(Bundle savedInstanceState) {
            super.onPostCreate(savedInstanceState);
            drawerToggle.syncState();

        }

        @Override
        public void onConfigurationChanged(Configuration newConfig) {
            super.onConfigurationChanged(newConfig);
            drawerToggle.onConfigurationChanged(newConfig);
        }

        private void setupDrawerContent(NavigationView navigationView) {
            navigationView.setNavigationItemSelectedListener(
                    new NavigationView.OnNavigationItemSelectedListener() {
                        @Override
                        public boolean onNavigationItemSelected(MenuItem menuItem) {
                            selectDrawerItem(menuItem);
                            return true;
                        }
                    });
        }

        public void selectDrawerItem(MenuItem menuItem) {
            Fragment fragment = null;
            Class fragmentClass;
            switch(menuItem.getItemId()) {
                case R.id.nav_first_fragment:
                    fragmentClass = FirstFragment.class;
                    break;
                case R.id.nav_second_fragment:
                    fragmentClass = SecondFragment.class;
                    break;
                case R.id.nav_third_fragment:
                    fragmentClass = ThirdFragment.class;
                    break;
                default:
                    fragmentClass = FirstFragment.class;
            }

            try {
                fragment = (Fragment) fragmentClass.newInstance();
            } catch (Exception e) {
                e.printStackTrace();
            }


            FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
            fragmentManager.beginTransaction().replace(R.id.flContent, fragment).commit();

            menuItem.setChecked(true);
            setTitle(menuItem.getTitle());
            mDrawer.closeDrawers();
        }

        private ActionBarDrawerToggle setupDrawerToggle() {
            return new ActionBarDrawerToggle(this, dlDrawer, toolbar, R.string.drawer_open,  R.string.drawer_close);
           }

       }

App_main.xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/main_activity_appbar">
    <include layout="@layout/app_bar"
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"></include>

    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawer_layout"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        xmlns:android="http://schemas.android.com/apk/res/android"
      >
        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#E3F2FD"
            android:id="@+id/main_container"
            >
        <fragment
            android:id="@+id/nav_drawer"
            android:name="askselva.com.sampematerial.NavigationDrawerFragment"
            android:layout_width="280dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            tools:layout="@layout/fragment_navigation_drawer"></fragment>
        </FrameLayout>
    </android.support.v4.widget.DrawerLayout>

</LinearLayout>

错误

11-05 00:30:33.925 18019-18019/askselva.com.sampematerial E/AndroidRuntime: FATAL EXCEPTION: main
11-05 00:30:33.925 18019-18019/askselva.com.sampematerial E/AndroidRuntime: Process: askselva.com.sampematerial, PID: 18019
11-05 00:30:33.925 18019-18019/askselva.com.sampematerial E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{askselva.com.sampematerial/askselva.com.sampematerial.MainActivity}: android.view.InflateException: Binary XML file line #33: Error inflating class fragment
11-05 00:30:33.925 18019-18019/askselva.com.sampematerial E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2198)
11-05 00:30:33.925 18019-18019/askselva.com.sampematerial E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257)
11-05 00:30:33.925 18019-18019/askselva.com.sampematerial E/AndroidRuntime:     at android.app.ActivityThread.access$800(ActivityThread.java:139)
11-05 00:30:33.925 18019-18019/askselva.com.sampematerial E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
11-05 00:30:33.925 18019-18019/askselva.com.sampematerial E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102)
11-05 00:30:33.925 18019-18019/askselva.com.sampematerial E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:136)
11-05 00:30:33.925 18019-18019/askselva.com.sampematerial E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5086)
11-05 00:30:33.925 18019-18019/askselva.com.sampematerial E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method)
11-05 00:30:33.925 18019-18019/askselva.com.sampematerial E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:515)
11-05 00:30:33.925 18019-18019/askselva.com.sampematerial E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
11-05 00:30:33.925 18019-18019/askselva.com.sampematerial E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
11-05 00:30:33.925 18019-18019/askselva.com.sampematerial E/AndroidRuntime:     at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
11-05 00:30:33.925 18019-18019/askselva.com.sampematerial E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method)
11-05 00:30:33.925 18019-18019/askselva.com.sampematerial E/AndroidRuntime:  Caused by: android.view.InflateException: Binary XML file line #33: Error inflating class fragment
11-05 00:30:33.925 18019-18019/askselva.com.sampematerial E/AndroidRuntime:     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
11-05 00:30:33.925 18019-18019/askselva.com.sampematerial E/AndroidRuntime:     at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
11-05 00:30:33.925 18019-18019/askselva.com.sampematerial E/AndroidRuntime:     at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
11-05 00:30:33.925 18019-18019/askselva.com.sampematerial E/AndroidRuntime:     at de.robv.android.xposed.XposedBridge.invokeOriginalMethodNative(Native Method)
11-05 00:30:33.925 18019-18019/askselva.com.sampematerial E/AndroidRuntime:     at de.robv.android.xposed.XposedBridge.handleHookedMethod(XposedBridge.java:631)
11-05 00:30:33.925 18019-18019/askselva.com.sampematerial E/AndroidRuntime:     at android.view.LayoutInflater.inflate(Native Method)
11-05 00:30:33.925 18019-18019/askselva.com.sampematerial E/AndroidRuntime:     at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
11-05 00:30:33.925 18019-18019/askselva.com.sampematerial E/AndroidRuntime:     at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
11-05 00:30:33.925 18019-18019/askselva.com.sampematerial E/AndroidRuntime:     at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:257)
11-05 00:30:33.925 18019-18019/askselva.com.sampematerial E/AndroidRuntime:     at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)
11-05 00:30:33.925 18019-18019/askselva.com.sampematerial E/AndroidRuntime:     at askselva.com.sampematerial.MainActivity.onCreate(MainActivity.java:26)
11-05 00:30:33.925 18019-18019/askselva.com.sampematerial E/AndroidRuntime:     at android.app.Activity.performCreate(Activity.java:5248)
11-05 00:30:33.925 18019-18019/askselva.com.sampematerial E/AndroidRuntime:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110)
11-05 00:30:33.925 18019-18019/askselva.com.sampematerial E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2162)
11-05 00:30:33.925 18019-18019/askselva.com.sampematerial E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257) 
11-05 00:30:33.925 18019-18019/askselva.com.sampematerial E/AndroidRuntime:     at android.app.ActivityThread.access$800(ActivityThread.java:139) 
11-05 00:30:33.925 18019-18019/askselva.com.sampematerial E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210) 
11-05 00:30:33.925 18019-18019/askselva.com.sampematerial E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102) 
11-05 00:30:33.925 18019-18019/askselva.com.sampematerial E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:136) 
11-05 00:30:33.925 18019-18019/askselva.com.sampematerial E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5086) 
11-05 00:30:33.925 18019-18019/askselva.com.sampematerial E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method) 
11-05 00:30:33.925 18019-18019/askselva.com.sampematerial E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:515) 
11-05 00:30:33.925 18019-18019/askselva.com.sampematerial E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) 
11-05 00:30:33.925 18019-18019/askselva.com.sampematerial E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 
11-05 00:30:33.925 18019-18019/askselva.com.sampematerial E/AndroidRuntime:     at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132) 
11-05 00:30:33.925 18019-18019/askselva.com.sampematerial E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method) 
11-05 00:30:33.925 18019-18019/askselva.com.sampematerial E/AndroidRuntime:  Caused by: java.lang.NullPointerException
11-05 00:30:33.925 18019-18019/askselva.com.sampematerial E/AndroidRuntime:     at askselva.com.sampematerial.NavigationDrawerFragment.onCreateView(NavigationDrawerFragment.java:99)
11-05 00:30:33.925 18019-18019/askselva.com.sampematerial E/AndroidRuntime:     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1965)
11-05 00:30:33.925 18019-18019/askselva.com.sampematerial E/AndroidRuntime:     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1047)
11-05 00:30:33.925 18019-18019/askselva.com.sampematerial E/AndroidRuntime:     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1237)
11-05 00:30:33.925 18019-18019/askselva.com.sampematerial E/AndroidRuntime:     at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1339)
11-05 00:30:33.925 18019-18019/askselva.com.sampematerial E/AndroidRuntime:     at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2295)
11-05 00:30:33.925 18019-18019/askselva.com.sampematerial E/AndroidRuntime:     at android.support.v4.app.FragmentController.onCreateView(FragmentController.java:111)
11-05 00:30:33.925 18019-18019/askselva.com.sampematerial E/AndroidRuntime:     at android.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:314)
11-05 00:30:33.925 18019-18019/askselva.com.sampematerial E/AndroidRuntime:     at android.support.v4.app.BaseFragmentActivityHoneycomb.onCreateView(BaseFragmentActivityHoneycomb.java:31)
11-05 00:30:33.925 18019-18019/askselva.com.sampematerial E/AndroidRuntime:     at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:79)
11-05 00:30:33.925 18019-18019/askselva.com.sampematerial E/AndroidRuntime:     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
11-05 00:30:33.925 18019-18019/askselva.com.sampematerial E/AndroidRuntime:     at android.view.LayoutInflater.rInflate(LayoutInflater.java:755) 
11-05 00:30:33.925 18019-18019/askselva.com.sampematerial E/AndroidRuntime:     at android.view.LayoutInflater.inflate(LayoutInflater.java:492) 
11-05 00:30:33.925 18019-18019/askselva.com.sampematerial E/AndroidRuntime:     at de.robv.android.xposed.XposedBridge.invokeOriginalMethodNative(Native Method) 
11-05 00:30:33.925 18019-18019/askselva.com.sampematerial E/AndroidRuntime:     at de.robv.android.xposed.XposedBridge.handleHookedMethod(XposedBridge.java:631) 
11-05 00:30:33.925 18019-18019/askselva.com.sampematerial E/AndroidRuntime:     at android.view.LayoutInflater.inflate(Native Method) 
11-05 00:30:33.925 18019-18019/askselva.com.sampematerial E/AndroidRuntime:     at android.view.LayoutInflater.inflate(LayoutInflater.java:397) 
11-05 00:30:33.925 18019-18019/askselva.com.sampematerial E/AndroidRuntime:     at android.view.LayoutInflater.inflate(LayoutInflater.java:353) 
11-05 00:30:33.925 18019-18019/askselva.com.sampematerial E/AndroidRuntime:     at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:257) 
11-05 00:30:33.925 18019-18019/askselva.com.sampematerial E/AndroidRuntime:     at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109) 
11-05 00:30:33.925 18019-18019/askselva.com.sampematerial E/AndroidRuntime:     at askselva.com.sampematerial.MainActivity.onCreate(MainActivity.java:26) 
11-05 00:30:33.925 18019-18019/askselva.com.sampematerial E/AndroidRuntime:     at android.app.Activity.performCreate(Activity.java:5248) 
11-05 00:30:33.925 18019-18019/askselva.com.sampematerial E/AndroidRuntime:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110) 
11-05 00:30:33.925 18019-18019/askselva.com.sampematerial E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2162) 
11-05 00:30:33.925 18019-18019/askselva.com.sampematerial E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257) 
11-05 00:30:33.925 18019-18019/askselva.com.sampematerial E/AndroidRuntime:     at android.app.ActivityThread.access$800(ActivityThread.java:139) 
11-05 00:30:33.925 18019-18019/askselva.com.sampematerial E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210) 
11-05 00:30:33.925 18019-18019/askselva.com.sampematerial E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102) 
11-05 00:30:33.925 18019-18019/askselva.com.sampematerial E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:136) 
11-05 00:30:33.925 18019-18019/askselva.com.sampematerial E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5086) 
11-05 00:30:33.925 18019-18019/askselva.com.sampematerial E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method) 
11-05 00:30:33.925 18019-18019/askselva.com.sampematerial E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:515) 
11-05 00:30:33.925 18019-18019/askselva.com.sampematerial E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) 
11-05 00:30:33.925 18019-18019/askselva.com.sampematerial E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 
11-05 00:30:33.925 18019-18019/askselva.com.sampematerial E/AndroidRuntime:     at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132) 
11-05 00:30:33.925 18019-18019/askselva.com.sampematerial E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method) 

这是我在LogCat中遇到的错误,我在如何解决这个错误中遇到错误。我应该如何在活动中使用片段。片段活动还是活动?我是Fragments的新手请帮助我.. 提前谢谢!

1 个答案:

答案 0 :(得分:2)

getActivity()在片段类中可用。由于你已经在你的活动中,只需删除对getActivity()的调用。片段管理器的Ur代码将是

FragmentManager fragmentManager = getSupportFragmentManager();

此外,ur mainactivity应扩展FragmentActivity以获取支持片段管理器实例。即

public class MainActivity extends FragmentActivity {

此外,您需要先了解片段的基础知识,然后再继续。

查看此链接http://developer.android.com/guide/components/fragments.html了解详情。