AlertDialog中的资源ID#0x0

时间:2019-01-09 05:08:14

标签: android kotlin resources alertdialog android-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>

2 个答案:

答案 0 :(得分:2)

修改为以下代码后,它可以正常工作。

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()

不知道为什么。

答案 1 :(得分:1)

  

如果您的应用程序找不到资源,则会出现上述错误。

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

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

enter image description here

图片fragment_sign_up_and_login.xml上的图片位于您的layout文件夹中,而fragment_splash.xml则位于layout-v21文件夹中。