Search code examples
androidkotlinresourcesandroid-alertdialog

Resource ID #0x0 in AlertDialog


I adding an AlertDialog in kotlin file, but get exception

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

Exception

   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>

Solution

  • It works fine after I modified to below code.

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

    No idea why.