在片段和活动之间导航

时间:2017-03-06 11:08:42

标签: android android-fragments android-fragmentactivity android-navigation-drawer fragmentmanager

我无法在片段和活动之间导航,我使用了 Android工作室2.2.3 的自动生成模板,并对其进行了自定义以在片段之间导航。

我添加了五个片段并使用了本教程Using Fragments with the Navigation Drawer Activity

碎片工作得很好,一切都很好,所以我想使用相同的导航抽屉去另一个活动,但它一直在崩溃,我使用了Intent。

public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();
    Fragment fragment = null;
    Class fragmentClass = null;
    if (id == R.id.nav_schedule) {
     fragmentClass = ScheduleFragment.class;
    } else if (id == R.id.nav_assingment) {
        fragmentClass = AssignmentsFragment.class;
    } else if (id == R.id.nav_teachers) {
        fragmentClass = TeachersFragment.class;
    } else if (id == R.id.nav_score) {
        fragmentClass = ScoreFragment.class;
    } else if (id == R.id.nav_events) {
        fragmentClass = EventsFragment.class;
    } else if (id == R.id.nav_setting) {

    }else if (id == R.id.nav_about) {
        Intent intent = new Intent(getApplicationContext(),AboutActivity.class);
        startActivity(intent);
    }else if (id == R.id.nav_logout) {
        Intent i = new Intent(getApplicationContext(),LoginActivity.class);
        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(i);
    }
         try {
             try {
                 fragment = (Fragment) fragmentClass.newInstance();
             } catch (InstantiationException e) {
                 e.printStackTrace();
             } catch (IllegalAccessException e) {
                 e.printStackTrace();
             }
             FragmentManager fragmentManager = getSupportFragmentManager();
             fragmentManager.beginTransaction().replace(R.id.container, fragment).commit();
         }catch (android.app.Fragment.InstantiationException e){
             e.printStackTrace();
         }
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}

logcat的

E / AndroidRuntime:致命异常:主要                   过程:com.zer0ll.demo.studentapp,PID:31638                   java.lang.NullPointerException:尝试调用虚方法' java.lang.Object java.lang.Class.newInstance()'在null对象引用上                       at com.zer0ll.demo.studentapp.Activities.HomeActivity.onNavigationItemSelected(HomeActivity.java:95)                       在android.support.design.widget.NavigationView $ 1.onMenuItemSelected(NavigationView.java:156)                       在android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:822)                       在android.support.v7.view.menu.SubMenuBuilder.dispatchMenuItemSelected(SubMenuBuilder.java:88)                       在android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:156)                       在android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:969)                       在android.support.design.internal.NavigationMenuPresenter $ 1.onClick(NavigationMenuPresenter.java:342)                       在android.view.View.performClick(View.java:5156)                       在android.view.View $ PerformClick.run(View.java:20755)                       在android.os.Handler.handleCallback(Handler.java:739)                       在android.os.Handler.dispatchMessage(Handler.java:95)                       在android.os.Looper.loop(Looper.java:145)                       在android.app.ActivityThread.main(ActivityThread.java:5835)                       at java.lang.reflect.Method.invoke(Native Method)                       在java.lang.reflect.Method.invoke(Method.java:372)                       在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1399)                       在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)

任何帮助将不胜感激。 谢谢。

3 个答案:

答案 0 :(得分:0)

您必须在清单中定义活动。

答案 1 :(得分:0)

尝试在清单文件中添加您的活动

<activity android:name="Packagename.AboutActivity">

        </activity>

    <activity android:name="Packagename.LoginActivity">

        </activity>

答案 2 :(得分:0)

使用活动上下文启动活动而不使用应用程序上下文,并在启动活动后返回true,并使用正确的包名称声明在清单中

@RequestMapping(value={ "/", "/tdk/login"}, method = RequestMethod.POST)