从另一个片段中打开一个片段

时间:2018-10-15 00:25:51

标签: android android-studio android-fragments fragment

我开发了一个健身应用程序,因此,当用户单击片段中的“初学者”按钮时,它应该会显示一系列练习。我的第二个片段(片段2)最初在工作,但是一旦我自定义该片段并添加了图像等,它就停止了工作,我无法找出错误所在。

这是给我老师的。

以下是出现错误的部分:

public class Fragment2 extends Fragment {

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment2,container,false);
        return view;
    }
}

这是我的错误代码:

10-14 20:15:28.697 3565-3602/harsh.banthia.myofficialbwf E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008cdf 10-14 20:15:28.698 3565-3602/harsh.banthia.myofficialbwf E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008824 10-14 20:15:53.445 3565-3565/harsh.banthia.myofficialbwf E/AndroidRuntime: FATAL EXCEPTION: main
    Process: harsh.banthia.myofficialbwf, PID: 3565
    java.lang.OutOfMemoryError: Failed to allocate a 451584012 byte allocation with 4194304 free bytes and 149MB until OOM
        at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
        at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
        at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:609)
        at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)
        at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:1080)
        at android.content.res.Resources.loadDrawableForCookie(Resources.java:2635)
        at android.content.res.Resources.loadDrawable(Resources.java:2540)
        at android.content.res.TypedArray.getDrawable(TypedArray.java:870)
        at android.view.View.<init>(View.java:3948)
        at android.widget.TextView.<init>(TextView.java:677)
        at android.widget.Button.<init>(Button.java:109)
        at android.widget.Button.<init>(Button.java:105)
        at android.support.v7.widget.AppCompatButton.<init>(AppCompatButton.java:71)
        at android.support.v7.widget.AppCompatButton.<init>(AppCompatButton.java:67)
        at android.support.v7.app.AppCompatViewInflater.createButton(AppCompatViewInflater.java:187)
        at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:110)
        at android.support.v7.app.AppCompatDelegateImpl.createView(AppCompatDelegateImpl.java:1266)
        at android.support.v7.app.AppCompatDelegateImpl.onCreateView(AppCompatDelegateImpl.java:1316)
        at android.view.LayoutInflater$FactoryMerger.onCreateView(LayoutInflater.java:189)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:746)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:835)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
        at harsh.banthia.myofficialbwf.Fragment2.onCreateView(Fragment2.java:20)
        at android.support.v4.app.Fragment.performCreateView(Fragment.java:2439)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1460)
        at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1784)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1852)
        at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:802)
        at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2625)
        at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2411)
        at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2366)
        at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2273)
        at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:733)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5417)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

1 个答案:

答案 0 :(得分:0)

这是由于您在Fragment中添加了图片。因此,出于您的应用程序的考虑,请参考https://developer.android.com/topic/performance/graphics/load-bitmap以将bitmapsdrawables有效地加载到application中。