Android 实例化 DialogFragment 时出现 OutOfMemoryError
Android getting OutOfMemoryError when instantiating a DialogFragment
我正在阅读 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个元素。我猜是某处发生了内存泄漏,但我找不到它。
有人有想法吗?
回复评论及解决方案:
@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")
}
}
这听起来像循环。
尝试删除 getDialog().cancel()
并只删除 return null。
您不需要明确关闭 Dialog
es。
我认为你做错了。您不需要使用 AlertDialog.Builder,因为您已经从 DialogFragment 进行了扩展。
你可以关注这个link。
它在 Java 中,但在 Kotlin 中它会类似。
不要使用警报生成器创建对话框。
删除您对 onCreateDialog
的覆盖。
在 onCreateView
中放大您的视图:
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.dialog_layout, container, false)
}
自己在布局中添加一些按钮,并在展开视图后手动设置 onClickListeners。
我正在阅读 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个元素。我猜是某处发生了内存泄漏,但我找不到它。
有人有想法吗?
回复评论及解决方案:
@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")
}
}
这听起来像循环。
尝试删除 getDialog().cancel()
并只删除 return null。
您不需要明确关闭 Dialog
es。
我认为你做错了。您不需要使用 AlertDialog.Builder,因为您已经从 DialogFragment 进行了扩展。
你可以关注这个link。
它在 Java 中,但在 Kotlin 中它会类似。
不要使用警报生成器创建对话框。
删除您对 onCreateDialog
的覆盖。
在 onCreateView
中放大您的视图:
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.dialog_layout, container, false)
}
自己在布局中添加一些按钮,并在展开视图后手动设置 onClickListeners。