AlertDialog 中的资源 ID #0x0

Resource ID #0x0 in AlertDialog

我在 kotlin 文件中添加了一个 AlertDialog,但出现异常

btnLogin.setOnClickListener { view ->
            login()
        }


fun login() {

        val builder = AlertDialog.Builder(this@LoginActivity)
        builder.setView(R.layout.layout_loading_dialog)
        val dialog = builder.create()
        dialog.show()
     }

异常

   android.content.res.Resources$NotFoundException: Resource ID #0x0
        at android.content.res.ResourcesImpl.getValue(ResourcesImpl.java:195)
        at android.content.res.Resources.loadXmlResourceParser(Resources.java:2133)
        at android.content.res.Resources.getLayout(Resources.java:1142)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:421)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
        at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)

xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:orientation="horizontal"
              android:padding="20dp">
    <ProgressBar
            android:id="@+id/progressBar"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1" />

    <TextView
            android:id="@+id/textView"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="4"
            android:gravity="center"
            android:text="Please wait! This may take a moment." />
</LinearLayout>

Above error comes if your application doesn't find out the resource.

它有很多可能性。例如有时我们存储文件 style-v21values-v21 或更多。

注意:- 只需检查 layout 文件夹中的 layout_loading_dialog.xml。它不应该在 layout-v21 文件夹中。

上面的图片 fragment_sign_up_and_login.xml 在您的 layout 文件夹中,但 fragment_splash.xmllayout-v21 文件夹中。

我修改为下面的代码后,效果很好。

var dialogs = Dialog(this)
dialogs.requestWindowFeature(Window.FEATURE_NO_TITLE)
dialogs.getWindow().setBackgroundDrawable(ColorDrawable(Color.WHITE));
dialogs.setCancelable(false)
dialogs.setContentView(R.layout.layout_loading_dialog)
dialogs.show()

不知道为什么。