Resource ID #0x0 in AlertDialog

老子叫甜甜 提交于 2019-12-13 18:33:50

问题


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>

回答1:


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.




回答2:


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

It have alot of possibilities. Such as sometimes we store files style-v21 , values-v21 or more.

Note:- Just check your layout_loading_dialog.xml inside the layout folder. It should not be inside the layout-v21 folder.

It above picture fragment_sign_up_and_login.xml is inside your layout folder but fragment_splash.xml is inside the layout-v21 folder.



来源:https://stackoverflow.com/questions/54103552/resource-id-0x0-in-alertdialog

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!