【问题标题】:Android getting OutOfMemoryError when instantiating a DialogFragmentAndroid 在实例化 DialogFragment 时出现 OutOfMemoryError
【发布时间】:2019-02-13 15:01:49
【问题描述】:

我正在阅读来自 Google 的 this 关于使用 DialogFragment 的信息。在创建自定义布局部分之前一切正常:当调用完成以显示具有自定义布局的 DialogFragment 时,我得到一个OutOfMemoryError 异常。

我只是稍微修改了文章中的代码,我的应用只包含以下 3 个元素:

MainActivity.kt

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        setSupportActionBar(toolbar)

        fab.setOnClickListener { view ->
            showDialog()
        }
    }


    fun showDialog() {
        // Create an instance of the dialog fragment and show it
        val dialog = FireMissilesDialogFragment()
        dialog.show(supportFragmentManager, "NoticeDialogFragment")
    }

}

FireMissilesDialogFragment.kt

class FireMissilesDialogFragment : DialogFragment() {

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        return activity?.let {
            val builder = AlertDialog.Builder(it)

            // Inflate and set the layout for the dialog
            // Pass null as the parent view because its going in the dialog layout
            builder.setView(layoutInflater.inflate(R.layout.dialog_layout, null))
                // Add action buttons
                .setPositiveButton(R.string.ok) { dialog, id ->
                    Log.d("FireMissiles", "TEST")
                }
                .setNegativeButton(R.string.cancel) { dialog, id ->
                    getDialog().cancel()
                }
            builder.create()
        } ?: throw IllegalStateException("Activity cannot be null")
    }
}

dialog_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content">

    <EditText
            android:id="@+id/username"
            android:inputType="textEmailAddress"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:layout_marginLeft="4dp"
            android:layout_marginRight="4dp"
            android:layout_marginBottom="4dp"
            android:hint="@string/ok" android:importantForAutofill="no"/>
    <EditText
            android:id="@+id/password"
            android:inputType="textPassword"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="4dp"
            android:layout_marginLeft="4dp"
            android:layout_marginRight="4dp"
            android:layout_marginBottom="16dp"
            android:fontFamily="sans-serif"
            android:hint="@string/cancel" android:importantForAutofill="no"/>
</LinearLayout>

我的代码只有上面这 3 个元素。我猜是某个地方发生了内存泄漏,但我找不到它。

有人有想法吗?


cmets的答案及解决方案:

@Sam 我不使用任何图像,这个项目只是尝试使用 DialogFragment, 因此它基于带有 fab 的标准空 Activity 项目,我唯一没有展示的是 activity_main.xml 但它是标准的。

@Tobias,感谢您的提示,但它没有解决问题:(

@user8159708,谢谢!!解决了这个问题。我的代码现在是(这是我唯一更改的):

class FireMissilesDialogFragment : DialogFragment() {

    lateinit var mView: View

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        mView = inflater.inflate(R.layout.dialog_layout, container, false)

        setDialog()

        return mView
    }

    fun setDialog(){
        activity?.let {
            val builder = AlertDialog.Builder(it)

            // Inflate and set the layout for the dialog
            // Pass null as the parent view because its going in the dialog layout
            builder.setView(mView)
                // Add action buttons
                .setPositiveButton(R.string.ok) { dialog, id ->
                    Log.d("FireMissiles", "TEST")
                }
                .setNegativeButton(R.string.cancel) { dialog, id ->
                    Log.d("FireMissiles", "TEST")
                    //getDialog().cancel()
                }
            builder.create()
        } ?: throw IllegalStateException("Activity cannot be null")
    }
}

【问题讨论】:

  • 这里没有加起来。我认为你没有提供足够的信息。 OutOfMemory 通常来自糟糕的图像处理或保留内存的无限循环调用。确认您没有尝试循环启动它并确认您没有使用任何图像。如果您使用的是图片,那么您将它们放在什么桶中以及它们的大小。

标签: android kotlin out-of-memory dialogfragment


【解决方案1】:

不要使用警报生成器创建对话框。

删除您对 onCreateDialog 的覆盖。

onCreateView 中放大您的视图:

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.dialog_layout, container, false)
    }

自己添加一些按钮到你的布局中,并在你的视图膨胀后手动设置你的 onClickListeners。

【讨论】:

    【解决方案2】:

    这听起来像循环。

    尝试删除 getDialog().cancel() 并返回 null。

    您无需明确关闭Dialoges。

    【讨论】:

      【解决方案3】:

      我认为你做错了。您不需要使用 AlertDialog.Builder,因为您已经从 DialogFragment 扩展。

      你可以关注这个link

      它在 Java 中,但在 Kotlin 中会类似。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-11-16
        • 2016-01-26
        • 2012-10-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多