代码之家  ›  专栏  ›  技术社区  ›  Torantula

加载片段时应用程序崩溃

  •  1
  • Torantula  · 技术社区  · 6 年前

    当试图用片段替换占位符容器时,我的应用程序立即崩溃。

    我加载片段的方法很简单:

    private void loadFragment(Fragment fragment) {
    getSupportFragmentManager().beginTransaction().replace(R.id.fragHolder, 
    fragment).commit();
    }
    

    我只是这么说:

    Fragment fragment = new MyFragment();
    loadFragment(fragment);
    

    我要替换的容器fragHolder是一个FrameLayout,它位于水平线性布局内。您是否可以只将碎片放入某些容器中?

    根据网络上的消息来源,我加载片段的方式似乎没有什么问题。

    如果需要,请询问更多信息。

    编辑:

    myapp的LogCat输出错误:

       03-17 03:38:02.758 3179-3179/? E/android.os.Debug: failed to load memtrack module: -2
    03-17 03:38:03.049 1530-2967/system_process E/eglCodecCommon: glUtilsParamSize: unknow param 0x000082da
    03-17 03:38:03.049 1530-2967/system_process E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008cdf
    03-17 03:38:03.049 1530-2967/system_process E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008824
    03-17 03:38:03.146 1530-2967/system_process E/OpenGLRenderer: Corrupted GPU pixel buffer
    03-17 03:38:05.505 3190-3190/? E/AndroidRuntime: FATAL EXCEPTION: main
                                                     Process: mycompany.myname.myapp, PID: 3190
                                                     java.lang.RuntimeException: Unable to start activity ComponentInfo{mycompany.myname.myapp/mycompany.myname.myapp.MainActivity}: java.lang.RuntimeException: mycompany.myname.myapp.MainActivity@285a72d1 must implement OnFragmentInteractionListener
                                                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
                                                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
                                                         at android.app.ActivityThread.access$800(ActivityThread.java:144)
                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
                                                         at android.os.Handler.dispatchMessage(Handler.java:102)
                                                         at android.os.Looper.loop(Looper.java:135)
                                                         at android.app.ActivityThread.main(ActivityThread.java:5221)
                                                         at java.lang.reflect.Method.invoke(Native Method)
                                                         at java.lang.reflect.Method.invoke(Method.java:372)
                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
                                                      Caused by: java.lang.RuntimeException: mycompany.myname.myapp.MainActivity@285a72d1 must implement OnFragmentInteractionListener
                                                         at mycompany.myname.myapp.MyFragment.onAttach(MyFragment.java:83)
                                                         at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1231)
                                                         at android.support.v4.app.FragmentTransition.addToFirstInLastOut(FragmentTransition.java:1085)
                                                         at android.support.v4.app.FragmentTransition.calculateFragments(FragmentTransition.java:976)
                                                         at android.support.v4.app.FragmentTransition.startTransitions(FragmentTransition.java:95)
                                                         at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2146)
                                                         at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2103)
                                                         at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2013)
                                                         at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:388)
                                                         at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:607)
                                                         at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:178)
                                                         at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1220)
                                                         at android.app.Activity.performStart(Activity.java:5953)
                                                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2261)
                                                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) 
                                                         at android.app.ActivityThread.access$800(ActivityThread.java:144) 
                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) 
                                                         at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                         at android.os.Looper.loop(Looper.java:135) 
                                                         at android.app.ActivityThread.main(ActivityThread.java:5221) 
                                                         at java.lang.reflect.Method.invoke(Native Method) 
                                                         at java.lang.reflect.Method.invoke(Method.java:372) 
                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) 
                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 
    03-17 03:38:05.811 1530-1912/system_process E/ActivityManager: Invalid thumbnail dimensions: 504x504
    03-17 03:38:05.877 1530-2967/system_process E/eglCodecCommon: glUtilsParamSize: unknow param 0x000082da
    03-17 03:38:05.878 1530-2967/system_process E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008cdf
    03-17 03:38:05.878 1530-2967/system_process E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008824
    03-17 03:38:05.926 1530-2967/system_process E/OpenGLRenderer: Corrupted GPU pixel buffer
    03-17 03:38:18.278 3445-3445/? E/memtrack: Couldn't load memtrack module (No such file or directory)
    03-17 03:38:18.278 3445-3445/? E/android.os.Debug: failed to load memtrack module: -2
    03-17 03:38:18.698 3465-3465/? E/memtrack: Couldn't load memtrack module (No such file or directory)
    03-17 03:38:18.698 3465-3465/? E/android.os.Debug: failed to load memtrack module: -2
    03-17 03:38:19.178 3482-3482/? E/memtrack: Couldn't load memtrack module (No such file or directory)
    03-17 03:38:19.178 3482-3482/? E/android.os.Debug: failed to load memtrack module: -2
    03-17 03:38:22.537 3497-3497/? E/memtrack: Couldn't load memtrack module (No such file or directory)
    03-17 03:38:22.537 3497-3497/? E/android.os.Debug: failed to load memtrack module: -2
    03-17 03:38:22.891 1530-2967/system_process E/eglCodecCommon: glUtilsParamSize: unknow param 0x000082da
    03-17 03:38:22.891 1530-2967/system_process E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008cdf
    03-17 03:38:22.891 1530-2967/system_process E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008824
    03-17 03:38:22.963 1530-2967/system_process E/OpenGLRenderer: Corrupted GPU pixel buffer
    03-17 03:38:26.068 3508-3508/mycompany.myname.myapp E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                          Process: mycompany.myname.myapp, PID: 3508
                                                                                          java.lang.RuntimeException: Unable to start activity ComponentInfo{mycompany.myname.myapp/mycompany.myname.myapp.MainActivity}: java.lang.RuntimeException: mycompany.myname.myapp.MainActivity@285a72d1 must implement OnFragmentInteractionListener
                                                                                              at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
                                                                                              at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
                                                                                              at android.app.ActivityThread.access$800(ActivityThread.java:144)
                                                                                              at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
                                                                                              at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                              at android.os.Looper.loop(Looper.java:135)
                                                                                              at android.app.ActivityThread.main(ActivityThread.java:5221)
                                                                                              at java.lang.reflect.Method.invoke(Native Method)
                                                                                              at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
                                                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
                                                                                           Caused by: java.lang.RuntimeException: mycompany.myname.myapp.MainActivity@285a72d1 must implement OnFragmentInteractionListener
                                                                                              at mycompany.myname.myapp.MyFragment.onAttach(MyFragment.java:83)
                                                                                              at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1231)
                                                                                              at android.support.v4.app.FragmentTransition.addToFirstInLastOut(FragmentTransition.java:1085)
                                                                                              at android.support.v4.app.FragmentTransition.calculateFragments(FragmentTransition.java:976)
                                                                                              at android.support.v4.app.FragmentTransition.startTransitions(FragmentTransition.java:95)
                                                                                              at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2146)
                                                                                              at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2103)
                                                                                              at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2013)
                                                                                              at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:388)
                                                                                              at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:607)
                                                                                              at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:178)
                                                                                              at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1220)
                                                                                              at android.app.Activity.performStart(Activity.java:5953)
                                                                                              at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2261)
                                                                                              at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) 
                                                                                              at android.app.ActivityThread.access$800(ActivityThread.java:144) 
                                                                                              at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) 
                                                                                              at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                              at android.os.Looper.loop(Looper.java:135) 
                                                                                              at android.app.ActivityThread.main(ActivityThread.java:5221) 
                                                                                              at java.lang.reflect.Method.invoke(Native Method) 
                                                                                              at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                                              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) 
                                                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 
    03-17 03:38:26.373 1530-1718/system_process E/ActivityManager: Invalid thumbnail dimensions: 504x504
    03-17 03:38:26.427 1530-2967/system_process E/eglCodecCommon: glUtilsParamSize: unknow param 0x000082da
    03-17 03:38:26.428 1530-2967/system_process E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008cdf
    03-17 03:38:26.428 1530-2967/system_process E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008824
    03-17 03:38:26.487 1530-2967/system_process E/OpenGLRenderer: Corrupted GPU pixel buffer
    03-17 03:39:00.025 1638-2034/com.android.systemui E/OpenGLRenderer: Corrupted GPU pixel buffer
    03-17 03:39:33.922 2611-2628/com.google.android.gms.unstable E/eglCodecCommon: glUtilsParamSize: unknow param 0x000082da
    03-17 03:39:33.922 2611-2628/com.google.android.gms.unstable E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008cdf
    03-17 03:39:33.922 2611-2628/com.google.android.gms.unstable E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008824
    03-17 03:39:33.968 2611-2628/com.google.android.gms.unstable E/eglCodecCommon: glUtilsParamSize: unknow param 0x000082da
    03-17 03:39:33.968 2611-2628/com.google.android.gms.unstable E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008cdf
    03-17 03:39:33.968 2611-2628/com.google.android.gms.unstable E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008824
    03-17 03:40:00.025 1638-2034/com.android.systemui E/OpenGLRenderer: Corrupted GPU pixel buffer
    03-17 03:41:00.026 1638-2034/com.android.systemui E/OpenGLRenderer: Corrupted GPU pixel buffer
    03-17 03:41:33.885 2611-2628/com.google.android.gms.unstable E/eglCodecCommon: glUtilsParamSize: unknow param 0x000082da
    03-17 03:41:33.886 2611-2628/com.google.android.gms.unstable E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008cdf
    03-17 03:41:33.886 2611-2628/com.google.android.gms.unstable E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008824
    03-17 03:41:33.933 2611-2628/com.google.android.gms.unstable E/eglCodecCommon: glUtilsParamSize: unknow param 0x000082da
    03-17 03:41:33.933 2611-2628/com.google.android.gms.unstable E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008cdf
    03-17 03:41:33.933 2611-2628/com.google.android.gms.unstable E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008824
    03-17 03:42:00.027 1638-2034/com.android.systemui E/OpenGLRenderer: Corrupted GPU pixel buffer
    03-17 03:43:26.673 8147-8147/? E/memtrack: Couldn't load memtrack module (No such file or directory)
    03-17 03:43:26.673 8147-8147/? E/android.os.Debug: failed to load memtrack module: -2
    03-17 03:45:00.033 1638-2034/com.android.systemui E/OpenGLRenderer: Corrupted GPU pixel buffer
    03-17 03:45:34.116 2611-2630/com.google.android.gms.unstable E/eglCodecCommon: glUtilsParamSize: unknow param 0x000082da
    03-17 03:45:34.116 2611-2630/com.google.android.gms.unstable E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008cdf
    03-17 03:45:34.116 2611-2630/com.google.android.gms.unstable E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008824
    03-17 03:45:34.159 2611-2630/com.google.android.gms.unstable E/eglCodecCommon: glUtilsParamSize: unknow param 0x000082da
    03-17 03:45:34.159 2611-2630/com.google.android.gms.unstable E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008cdf
    03-17 03:45:34.159 2611-2630/com.google.android.gms.unstable E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008824
    03-17 03:46:00.028 1638-2034/com.android.systemui E/OpenGLRenderer: Corrupted GPU pixel buffer
    

    之前还有很多,但不允许我全部发布,我假设有这么多,因为我正在运行模拟器?我已经选择了与我相关的内容,但请不要犹豫,提出任何具体要求。

    1 回复  |  直到 6 年前
        1
  •  1
  •   Navneet Krishna    6 年前

    只需实现您的活动 OnFragmentInteractionListener 例如

    public class MainActivity extends AppCompatActivity implements  YourFragment.OnFragmentInteractionListener
    {
    .
    .
    @Override
    public void onFragmentInteraction(Uri uri) {
    
    }
    }
    

    实施提供的方法 OnFragmentInteractionListener 在您的活动中也是如此