InvocationTargetException 在适配器类中膨胀视图

Posted

技术标签:

【中文标题】InvocationTargetException 在适配器类中膨胀视图【英文标题】:InvocationTargetException on inflating a view in Adapter class 【发布时间】:2020-09-30 02:27:16 【问题描述】:

我正在尝试扩充自定义 recyclerview,但问题是每次我尝试扩充 onCreateViewHolder() 中的 xml 文件时,它都会生成 InvocationTargetException 异常。我还应用了一些已经可用的解决方案,但问题仍然存在。我没有得到我缺乏的地方。

public class CurrentListAdapter extends RecyclerView.Adapter<CurrentListAdapter.CurrentViewHolder> 

    private List<Prescription> mLst;
    private Context mContext;
    private String[] list;

    public CurrentListAdapter(List<Prescription> mLst, Context mContext) 
        this.mLst = mLst;
        this.mContext = mContext;
    

    @NonNull
    @Override
    public CurrentViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) 
        LayoutInflater layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = layoutInflater.inflate(R.layout.row_current_list_item, parent, false);
        list = v.getResources().getStringArray(R.array.cStatus);
        return new CurrentViewHolder(v);
    

    @Override
    public void onBindViewHolder(@NonNull CurrentViewHolder h, int position) 
        Prescription p = mLst.get(position);
        h.prescriptionId.setText(p.getPresId());
        h.patientName.setText(p.getPatientName());
        h.labTest.setText(p.getLabTest());
    

    @Override
    public int getItemCount() 
        return mLst.size();
    

    public class CurrentViewHolder extends RecyclerView.ViewHolder implements AdapterView.OnItemSelectedListener 

        TextView prescriptionId, patientName, labTest, labComment;
        Spinner status;
        ArrayAdapter<String> adapter;

        public CurrentViewHolder(@NonNull View v) 
            super(v);
            prescriptionId = v.findViewById(R.id.presId);
            patientName = v.findViewById(R.id.patientName);
            labTest = v.findViewById(R.id.labTest);
            labComment = v.findViewById(R.id.comment);
            status = v.findViewById(R.id.status);
            final List<String> statusList = new ArrayList<>(Arrays.asList(list));
            adapter = new ArrayAdapter<String>(itemView.getContext(), R.layout.spinner_item,statusList)
                @Override
                public boolean isEnabled(int position) 
                    if (position == 0)
                    
                        return false;
                    
                    else
                        return true;
                    
                

                @Override
                public View getDropDownView(int position, @Nullable View convertView, @NonNull ViewGroup parent) 
                    View view = super.getDropDownView(position, convertView, parent);
                    TextView tv = (TextView) view;
                    if(position == 0)
                        // Set the hint text color gray
                        tv.setTextColor(Color.GRAY);
                    
                    else 
                        tv.setTextColor(Color.BLACK);
                    
                    return view;
                
            ;
            adapter.setDropDownViewResource(R.layout.spinner_item);
            status.setAdapter(adapter);
            status.setOnItemSelectedListener(this);
        

        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) 

        

        @Override
        public void onNothingSelected(AdapterView<?> parent) 

        
    

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_
    android:layout_
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:padding="10dp"
    android:elevation="10dp"
    android:background="@drawable/list_ripple"
    android:layout_margin="15dp">

    <TextView
        android:id="@+id/presId"
        android:layout_
        android:layout_
        android:visibility="gone"/>

    <TextView
        android:id="@+id/patientName"
        android:layout_
        android:layout_
        android:text="Muhammad Ali"
        android:textSize="20sp"
        android:textColor="@color/colorLab"
        android:textStyle="bold"
        android:gravity="center_horizontal"/>

    <View
        android:layout_
        android:layout_
        android:backgroundTint="?android:attr/listDivider"
        />

    <TextView
        android:id="@+id/labTest"
        android:padding="8dp"
        android:layout_
        android:layout_
        android:text="Test 1\nTest 2\nTest 3"
        android:singleLine="false"
        android:textSize="16sp"
        android:textStyle="italic"
        android:textColor="@color/colorBlack" />

    <EditText
        android:id="@+id/comment"
        android:padding="10dp"
        android:hint="Write something.."
        android:textSize="15sp"
        android:layout_
        android:layout_
        android:background="@drawable/edittext"/>

    <LinearLayout
    android:layout_
    android:layout_>

    <TextView
        android:text="Status: "
        android:padding="10dp"
        android:textSize="15sp"
        android:textStyle="bold"
        android:layout_
        android:layout_/>

    <Spinner
        android:id="@+id/status"
        android:layout_
        android:layout_
        android:padding="5dp" />
    </LinearLayout>

</LinearLayout>
    android.view.InflateException: Binary XML file line #28: Binary XML file line #28: Error inflating class android.view.View
    Caused by: android.view.InflateException: Binary XML file line #28: Error inflating class android.view.View
    Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Constructor.newInstance0(Native Method)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:334)
        at android.view.LayoutInflater.createView(LayoutInflater.java:647)
        at android.view.LayoutInflater.onCreateView(LayoutInflater.java:703)
        at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:68)
        at android.view.LayoutInflater.onCreateView(LayoutInflater.java:720)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:788)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
        at com.devaj.labapplication.Adapter.CurrentListAdapter.onCreateViewHolder(CurrentListAdapter.java:36)
        at com.devaj.labapplication.Adapter.CurrentListAdapter.onCreateViewHolder(CurrentListAdapter.java:21)
        at androidx.recyclerview.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:6794)
        at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5975)
        at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5858)
        at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5854)
        at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2230)
        at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1557)
        at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1517)
        at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:612)
        at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3924)
        at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:3641)
        at androidx.recyclerview.widget.RecyclerView.onLayout(RecyclerView.java:4194)
        at android.view.View.layout(View.java:20967)
        at android.view.ViewGroup.layout(ViewGroup.java:6440)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1791)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1635)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1544)
        at android.view.View.layout(View.java:20967)
        at android.view.ViewGroup.layout(ViewGroup.java:6440)
        at androidx.viewpager.widget.ViewPager.onLayout(ViewPager.java:1775)
        at android.view.View.layout(View.java:20967)
        at android.view.ViewGroup.layout(ViewGroup.java:6440)
        at com.google.android.material.appbar.HeaderScrollingViewBehavior.layoutChild(HeaderScrollingViewBehavior.java:142)
        at com.google.android.material.appbar.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:41)
        at com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior.onLayoutChild(AppBarLayout.java:1556)
        at androidx.coordinatorlayout.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:888)
        at android.view.View.layout(View.java:20967)
        at android.view.ViewGroup.layout(ViewGroup.java:6440)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
        at android.view.View.layout(View.java:20967)
        at android.view.ViewGroup.layout(ViewGroup.java:6440)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1791)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1635)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1544)
2020-06-10 20:27:44.123 28901-28901/com.devaj.labapplication E/AndroidRuntime:     at android.view.View.layout(View.java:20967)
        at android.view.ViewGroup.layout(ViewGroup.java:6440)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
        at android.view.View.layout(View.java:20967)
        at android.view.ViewGroup.layout(ViewGroup.java:6440)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1791)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1635)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1544)
        at android.view.View.layout(View.java:20967)
        at android.view.ViewGroup.layout(ViewGroup.java:6440)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
        at com.android.internal.policy.DecorView.onLayout(DecorView.java:955)
        at android.view.View.layout(View.java:20967)
        at android.view.ViewGroup.layout(ViewGroup.java:6440)
        at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:3092)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2779)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1863)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:8072)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:911)
        at android.view.Choreographer.doCallbacks(Choreographer.java:723)
        at android.view.Choreographer.doFrame(Choreographer.java:658)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:897)
        at android.os.Handler.handleCallback(Handler.java:790)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:7025)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:441)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408)
     Caused by: android.content.res.Resources$NotFoundException: Can't find ColorStateList from drawable resource ID #0x10805ed
        at android.content.res.ResourcesImpl.loadColorStateList(ResourcesImpl.java:1063)
        at android.content.res.Resources.loadColorStateList(Resources.java:1045)
        at android.content.res.TypedArray.getColorStateList(TypedArray.java:541)
        at android.view.View.<init>(View.java:5444)
        at android.view.View.<init>(View.java:5017)
        at android.view.View.<init>(View.java:4996)
            ... 79 more

【问题讨论】:

也许做一个干净的构建 【参考方案1】:

找不到backgroundTint

android:backgroundTint="?android:attr/listDivider"

将其更改为有效资源

【讨论】:

我也刚刚认识到它应该只是背景而不是 backgroundTint。谢谢 好的,您可以将答案标记为正确以供将来参考

以上是关于InvocationTargetException 在适配器类中膨胀视图的主要内容,如果未能解决你的问题,请参考以下文章

引起:java.lang.reflect.InvocationTargetException

InvocationTargetException 在适配器类中膨胀视图

什么可能导致 java.lang.reflect.InvocationTargetException?

GWT:在进行服务器调用时获取 InvocationTargetException

java.lang.reflect.InvocationTargetException 怎么解决

JavaFX使用Gluon SceneBuilder InvocationTargetException