在 Adapter class 中膨胀视图时出现 InvocationTargetException
InvocationTargetException on inflating a view in Adapter class
我正在尝试扩充自定义 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_width="match_parent"
android:layout_height="wrap_content"
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_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"/>
<TextView
android:id="@+id/patientName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Muhammad Ali"
android:textSize="20sp"
android:textColor="@color/colorLab"
android:textStyle="bold"
android:gravity="center_horizontal"/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:backgroundTint="?android:attr/listDivider"
/>
<TextView
android:id="@+id/labTest"
android:padding="8dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
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_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/edittext"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:text="Status: "
android:padding="10dp"
android:textSize="15sp"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Spinner
android:id="@+id/status"
android:layout_width="match_parent"
android:layout_height="45dp"
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
无法找到 backgroundTint 值
android:backgroundTint="?android:attr/listDivider"
将其更改为有效资源
我正在尝试扩充自定义 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_width="match_parent"
android:layout_height="wrap_content"
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_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"/>
<TextView
android:id="@+id/patientName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Muhammad Ali"
android:textSize="20sp"
android:textColor="@color/colorLab"
android:textStyle="bold"
android:gravity="center_horizontal"/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:backgroundTint="?android:attr/listDivider"
/>
<TextView
android:id="@+id/labTest"
android:padding="8dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
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_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/edittext"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:text="Status: "
android:padding="10dp"
android:textSize="15sp"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Spinner
android:id="@+id/status"
android:layout_width="match_parent"
android:layout_height="45dp"
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
无法找到 backgroundTint 值
android:backgroundTint="?android:attr/listDivider"
将其更改为有效资源