Kotlin-java.lang.IllegalStateException:ViewStub必须具有非null的ViewGroup viewParent

时间:2018-09-20 11:54:38

标签: android android-layout kotlin viewstub

我正在使用ViewStub通过Spinner保持主布局的基础来更改布局

代码如下:

     val viewStub = findViewById<ViewStub>(R.id.layout_stub) 
        val layoutParams = LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        viewStub.layoutParams = layoutParams
        viewStub.layoutResource = R.layout.activity_meccanica 
        viewStub.inflate()

//...set spinner adapter and itemSelectedListener

       if (selectedItem == "ItemTwo") {
       viewStub.layoutResource = R.layout.activity_termica
        viewStub.inflate() 

这是activity_main布局:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#e6e6e6"
    android:minWidth="25dp"
    android:id="@+id/rootLayout">
<ViewStub
                android:id="@+id/layout_stub"
                android:inflatedId="@+id/message_layout"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="0.75" />
</LinearLayout>

这是activity_termica:

    <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent">
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Termica"/>
</android.support.constraint.ConstraintLayout>

第一个viewStub.layoutResource工作正常,但是当selectedItem == "ItemTwo"与第二个viewStub.layoutResource崩溃时,出现此错误:

java.lang.IllegalStateException: ViewStub must have a non-null ViewGroup viewParent
                      at android.view.ViewStub.inflate(ViewStub.java:287)
                      at fisicapp.arfmann.com.fisicapp.MainActivity$onCreate$1.onItemSelected(MainActivity.kt:39)
                      at android.widget.AdapterView.fireOnSelected(AdapterView.java:934)
                      at android.widget.AdapterView.dispatchOnItemSelected(AdapterView.java:923)
                      at android.widget.AdapterView.-wrap1(AdapterView.java)
                      at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:893)
                      at android.os.Handler.handleCallback(Handler.java:761)
                      at android.os.Handler.dispatchMessage(Handler.java:98)
                      at android.os.Looper.loop(Looper.java:156)
                      at android.app.ActivityThread.main(ActivityThread.java:6523)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:942)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832)

1 个答案:

答案 0 :(得分:0)

尝试

val viewStub = findViewById<ViewStub>(R.id.layout_stub) 
        val layoutParams = LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        viewStub.layoutParams = layoutParams
    if (selectedItem .equals("ItemTwo")) {
       viewStub.layoutResource = R.layout.activity_termica
     }
else{

        viewStub.layoutResource = R.layout.activity_meccanica 

  }
viewStub.inflate()