在启动时加载片段会导致应用崩溃

时间:2018-11-07 00:33:58

标签: android android-fragments

我目前正在学习android,在我当前的项目中,我正在使用片段加载起始页面。

这是我的FrameLayout:

 <FrameLayout
    android:id="@+id/frame_container"   
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" /><![CDATA[
/>

我在活动中使用了一个空位来加载片段,这是代码:

private void loadFragment(Fragment fragment) {
    // load fragment
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    transaction.replace(R.id.frame_container, fragment);
    transaction.addToBackStack(null);
    transaction.commit();
}

在oncreate活动中,我使用以下代码:

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

    toolbar = getSupportActionBar();
    toolbar.setTitle("Home");
    loadFragment(new HomeFragment());

    BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
    navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
}

但是似乎我的应用程序总是在启动时崩溃。有什么想法吗?

对不起,英语不好!

编辑: 问题似乎在代码transaction.replace()

transaction.replace(R.id.frame_container, fragment);

当我将其注释掉时,我的应用程序可以运行,但是那时我无法加载片段

编辑2: 这是我的应用崩溃的原因:

  

2018-11-07 08:44:44.469 20414-20414 / com.example.han.xvit   D / AndroidRuntime:关闭VM 2018-11-07 08:44:44.470   20414-20414 / com.example.han.xvit E / AndroidRuntime:致命异常:   主要       流程:com.example.han.xvit,PID:20414       java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.han.xvit / com.example.han.xvit.home_activity}:   java.lang.RuntimeException:com.example.han.xvit.home_activity@a800718   必须实现OnFragmentInteractionListener           在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2721)           在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2782)           在android.app.ActivityThread.-wrap12(ActivityThread.java)           在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1521)           在android.os.Handler.dispatchMessage(Handler.java:102)           在android.os.Looper.loop(Looper.java:163)           在android.app.ActivityThread.main(ActivityThread.java:6228)           在java.lang.reflect.Method.invoke(本机方法)           在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:904)           在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)        引起原因:java.lang.RuntimeException:com.example.han.xvit.home_activity@a800718必须实现   OnFragmentInteractionListener           在com.example.han.xvit.HomeFragment.onAttach(HomeFragment.java:83)           在android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1404)           在android.support.v4.app.FragmentTransition.addToFirstInLastOut(FragmentTransition.java:1195)           在android.support.v4.app.FragmentTransition.calculateFragments(FragmentTransition.java:1078)           在android.support.v4.app.FragmentTransition.startTransitions(FragmentTransition.java:117)           在android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2408)           在android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2366)           在android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2273)           在android.support.v4.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3273)           在android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:3229)           在android.support.v4.app.FragmentController.dispatchActivityCreated(FragmentController.java:201)           在android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:620)           在android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:178)           在android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1249)           在android.app.Activity.performStart(Activity.java:6897)           在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2684)           在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2782)           在android.app.ActivityThread.-wrap12(ActivityThread.java)           在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1521)           在android.os.Handler.dispatchMessage(Handler.java:102)           在android.os.Looper.loop(Looper.java:163)           在android.app.ActivityThread.main(ActivityThread.java:6228)           在java.lang.reflect.Method.invoke(本机方法)           在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:904)           在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)

编辑3: 我成功解决了这个问题。看来我的家庭活动缺少几个片段实现方式

可以在here

看到解决方案

1 个答案:

答案 0 :(得分:0)

首先,删除<![CDATA[/>,以使布局如下所示:

 <FrameLayout
    android:id="@+id/frame_container"   
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />

然后,从以下错误日志中:

  

com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:904)at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)原因:java.lang.RuntimeException :com.example.han.xvit.home_activity@a800718必须在

的com.example.han.xvit.HomeFragment.onAttach(HomeFragment.java:83)处实现OnFragmentInteractionListener

这意味着您需要在“活动”中实现OnFragmentInteractionListener,因为您的HomeFragment需要它。请检查您的HomeFragment onAttach