Android Studio 使用Spinner组件 Error inflating class Spinner问题解决

        使用spinner组件做到下拉菜单选择的功能,在打开Spinner所在的界面时却出现了Error inflating class Spinner的报错并闪退。具体的报错内容如下:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.hui.tally, PID: 13711
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.hui.tally/com.test.RegisterActivity}: android.view.InflateException: Binary XML file line #148 in com.hui.tally:layout/activity_register: Binary XML file line #148 in com.hui.tally:layout/activity_register: Error inflating class Spinner
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
     Caused by: android.view.InflateException: Binary XML file line #148 in com.hui.tally:layout/activity_register: Binary XML file line #148 in com.hui.tally:layout/activity_register: Error inflating class Spinner
     Caused by: android.view.InflateException: Binary XML file line #148 in com.hui.tally:layout/activity_register: Error inflating class Spinner
     Caused by: android.content.res.Resources$NotFoundException: Text array resource ID #0x7f140009
        at android.content.res.Resources.getTextArray(Resources.java:660)
        at android.content.res.TypedArray.getTextArray(TypedArray.java:1063)
        at android.widget.AbsSpinner.(AbsSpinner.java:88)
        at android.widget.Spinner.(Spinner.java:246)
        at android.widget.Spinner.(Spinner.java:213)
        at android.widget.Spinner.(Spinner.java:163)
        at androidx.appcompat.widget.AppCompatSpinner.(AppCompatSpinner.java:209)
        at androidx.appcompat.widget.AppCompatSpinner.(AppCompatSpinner.java:180)
        at androidx.appcompat.widget.AppCompatSpinner.(AppCompatSpinner.java:160)
        at androidx.appcompat.widget.AppCompatSpinner.(AppCompatSpinner.java:144)
        at androidx.appcompat.app.AppCompatViewInflater.createSpinner(AppCompatViewInflater.java:214)
        at androidx.appcompat.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:131)
        at androidx.appcompat.app.AppCompatDelegateImpl.createView(AppCompatDelegateImpl.java:1551)
        at androidx.appcompat.app.AppCompatDelegateImpl.onCreateView(AppCompatDelegateImpl.java:1602)
        at android.view.LayoutInflater.tryCreateView(LayoutInflater.java:1059)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:995)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:959)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:1121)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1082)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:680)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:532)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:479)
        at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:696)
        at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:170)
        at com.test.RegisterActivity.onCreate(RegisterActivity.java:31)
        at android.app.Activity.performCreate(Activity.java:8000)
        at android.app.Activity.performCreate(Activity.java:7984)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
E/AndroidRuntime:     at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
I/Process: Sending signal. PID: 13711 SIG: 9

        Spinner组件的xml代码如下:

Android Studio 使用Spinner组件 Error inflating class Spinner问题解决_第1张图片

        在网络上寻找到的教程均无法解决此问题。后来发现是因为我把数组文件放在了xml文件夹下,这样虽然不会报错,甚至当写@xml时,系统还会给予对应的提示,但是会出现报错。

        应该把保存数组的xml文件存入values文件夹下,并在Spinner中定义 :android:entries="@array/type"。问题即可解决。

Android Studio 使用Spinner组件 Error inflating class Spinner问题解决_第2张图片

 

你可能感兴趣的:(android,android,studio,前端,xml,ui)