扩展AppCompatSpinner时出现android.view.InflateException

时间:2017-11-17 08:25:16

标签: android android-spinner android-custom-view layout-inflater android-inflate

我在过去成功创建了自定义视图,扩展了LinearLayout或android.support.v7.widget.CardView,但是当从android.support.v7.widget.AppCompatSpinner扩展时,我似乎无法正常工作。我不明白为什么这个特定班级有问题,所以我必须忽视一些事情。

我有以下类CustomSpinner

public class CustomSpinner extends android.support.v7.widget.AppCompatSpinner {

    public CustomSpinner(Context context)
    {
       this(context, null);
    }

    public CustomSpinner(Context context, AttributeSet attrs)
    {
       super(context, attrs);
       init(context, attrs, 0);
    }

    public CustomSpinner(Context context, AttributeSet attrs, int defStyle)
    {
       super(context, attrs, defStyle);
       init(context, attrs, defStyle);
    }

    private void init(Context context, AttributeSet attrs, int defStyle)
    {
       LayoutInflater inflater = LayoutInflater.from(context);
       inflater.inflate(R.layout.view_custom_spinner, this);
    }
}

和R.layout.view_custom_spinner

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.AppCompatSpinner
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/view_custom_spinner"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="@dimen/minimum_clickable_area"
    android:gravity="center_vertical"/>

我在另一个布局中使用自定义组件来实现&#39; MyFragment&#39;像这样(stacktrace中提到的第11行):

<my.custom.namespace.CustomSpinner
    android:id="@+id/view_spinner"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/> 

奇怪的是,当我从LinearLayout扩展CustomSpinner类时,一切正常,我没有得到InflateException。

我得到以下stacktrace:

java.lang.RuntimeException: Unable to start activity ComponentInfo{my.custom.namespace.MyActivity}: android.view.InflateException: Binary XML file line #8: Binary XML file line #11: Error inflating class my.custom.namespace.CustomSpinner
                                                                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3320)
                                                                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3416)
                                                                         at android.app.ActivityThread.access$1100(ActivityThread.java:229)
                                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821)
                                                                         at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                         at android.os.Looper.loop(Looper.java:148)
                                                                         at android.app.ActivityThread.main(ActivityThread.java:7407)
                                                                         at java.lang.reflect.Method.invoke(Native Method)
                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
                                                                      Caused by: android.view.InflateException: Binary XML file line #8: Binary XML file line #11: my.custom.namespace.CustomSpinner
                                                                         at android.view.LayoutInflater.inflate(LayoutInflater.java:551)
                                                                         at android.view.LayoutInflater.inflate(LayoutInflater.java:429)
                                                                         at my.custom.namespace.MyFragment.onCreateView(MyFragment.java:183)
                                                                         at android.support.v4.app.Fragment.performCreateView(Fragment.java:2261)
                                                                         at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1419)
                                                                         at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1750)
                                                                         at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1819)
                                                                         at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:797)
                                                                         at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2590)
                                                                         at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2377)
                                                                         at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2332)
                                                                         at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2239)
                                                                         at android.support.v4.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3231)
                                                                         at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:3181)
                                                                         at android.support.v4.app.FragmentController.dispatchActivityCreated(FragmentController.java:192)
                                                                         at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:572)
                                                                         at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:177)
                                                                         at my.custom.namespace.BaseActivity.onStart(BaseActivity.java:258)
                                                                         at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1266)
                                                                         at android.app.Activity.performStart(Activity.java:6943)
                                                                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3277)
                                                                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3416) 
                                                                         at android.app.ActivityThread.access$1100(ActivityThread.java:229) 
                                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821) 
                                                                         at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                         at android.os.Looper.loop(Looper.java:148) 
                                                                         at android.app.ActivityThread.main(ActivityThread.java:7407) 
                                                                         at java.lang.reflect.Method.invoke(Native Method) 
                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 
                                                                      Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class my.custom.namespace.CustomSpinner
                                                                         at android.view.LayoutInflater.createView(LayoutInflater.java:657)
                                                                         at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:776)
                                                                         at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:716)
                                                                         at android.view.LayoutInflater.rInflate(LayoutInflater.java:847)
                                                                         at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:810)
                                                                         at android.view.LayoutInflater.parseInclude(LayoutInflater.java:1001)
                                                                         at android.view.LayoutInflater.rInflate(LayoutInflater.java:843)
                                                                         at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:810)
                                                                         at android.view.LayoutInflater.inflate(LayoutInflater.java:527)
                                                                         at android.view.LayoutInflater.inflate(LayoutInflater.java:429) 
                                                                         at my.custom.namespace.MyFragment.onCreateView(MyFragment.java:183) 
                                                                         at android.support.v4.app.Fragment.performCreateView(Fragment.java:2261) 
                                                                         at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1419) 
                                                                         at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1750) 
                                                                         at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1819) 
                                                                         at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:797) 
                                                                         at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2590) 
                                                                         at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2377) 
                                                                         at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2332) 
                                                                         at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2239) 
                                                                         at android.support.v4.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3231) 
                                                                         at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:3181) 
                                                                         at android.support.v4.app.FragmentController.dispatchActivityCreated(FragmentController.java:192) 
                                                                         at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:572) 
                                                                         at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:177) 
                                                                         at my.custom.namespace.BaseActivity.onStart(BaseActivity.java:258) 
                                                                         at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1266) 
                                                                         at android.app.Activity.performStart(Activity.java:6943) 
                                                                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3277) 
                                                                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3416) 
                                                                         at android.app.ActivityThread.access$1100(ActivityThread.java:229) 
                                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821) 
                                                                         at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                         at android.os.Looper.loop(Looper.java:148) 
                                                                         at android.app.ActivityThread.main(ActivityThread.java:7407) 
                                                                         at java.lang.reflect.Method.invoke(Native Method) 
                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 
                                                                      Caused by: java.lang.reflect.InvocationTargetException
                                                                         at java.lang.reflect.Constructor.newInstance(Native Method)
                                                                         at android.view.LayoutInflater.createView(LayoutInflater.java:631)
                                                                         at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:776) 
                                                                         at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:716) 
                                                                         at android.view.LayoutInflater.rInflate(LayoutInflater.java:847) 
                                                                         at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:810) 
                                                                         at android.view.LayoutInflater.parseInclude(LayoutInflater.java:1001) 
                                                                         at android.view.LayoutInflater.rInflate(LayoutInflater.java:843) 
                                                                         at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:810) 
                                                                         at android.view.LayoutInflater.inflate(LayoutInflater.java:527) 
                                                                         at android.view.LayoutInflater.inflate(LayoutInflater.java:429) 
                                                                         at my.custom.namespace.MyFragment.onCreateView(MyFragment.java:183) 
                                                                         at android.support.v4.app.Fragment.performCreateView(Fragment.java:2261) 
                                                                         at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1419) 
                                                                         at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1750) 
                                                                         at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1819) 
                                                                         at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:797) 
                                                                         at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2590) 
                                                                         at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2377) 
                                                                         at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2332) 
                                                                         at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2239) 
                                                                         at android.support.v4.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3231) 
                                                                         at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:3181) 
                                                                         at android.support.v4.app.FragmentController.dispatchActivityCreated(FragmentController.java:192) 
                                                                         at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:572) 
                                                                         at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:177) 
                                                                         at my.custom.namespace.BaseActivity.onStart(BaseActivity.java:258) 
                                                                         at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1266) 
                                                                         at android.app.Activity.performStart(Activity.java:6943) 
                                                                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3277) 
                                                                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3416) 
                                                                         at android.app.ActivityThread.access$1100(ActivityThread.java:229) 
                                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821) 
                                                                         at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                         at android.os.Looper.loop(Looper.java:148) 
                                                                         at android.app.ActivityThread.main(ActivityThread.java:7407) 
                                                                         at java.lang.reflect.Method.invoke(Native Method) 
                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 
                                                                      Caused by: android.view.InflateException: Binary XML file l

1 个答案:

答案 0 :(得分:1)

我不明白为什么要夸大微调器的布局。微调器本身有自己的布局,你不需要在里面用微调器创建xml。对于线性布局,由于它是一个ViewGroup,因此可以扩展自己的布局,在自定义视图类中,您可以绑定线性布局的子项,但是对于微调器,只需处理微调器布局元素,使用自定义视图属性编辑布局。 总之,您不能将子项添加到微调器布局中,因此为它自动为其定义xml是没有意义的。只需删除 LayoutInflater inflater = LayoutInflater.from(context); inflater.inflate(R.layout.view_custom_spinner, this); 从您的代码中,您将拥有一个完美的工作类来扩展微调器。