旧设备上找不到Android资源ID

时间:2018-03-08 17:52:54

标签: java android xml android-layout

我在我的应用中使用了很多矢量绘图图像作为背景。问题是我无法使用app:srcCompat:=""设置背景,因为它是后台资源。即使我这样做仍然会引发错误,任何人都知道为什么?

我已尝试使用setBackgroundResource()或仅app:srcCompat设置背景但无效。

XML:



<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_height="110dp"
    android:layout_width="match_parent"
    android:layout_marginBottom="20dp"
    android:clickable="true"
    android:focusable="true">

    <android.support.v7.widget.GridLayout
        android:layout_width="380dp"
        android:layout_height="match_parent"
        android:layout_gravity="center_horizontal"
        app:alignmentMode="alignMargins"
        app:useDefaultMargins="true"
        app:columnOrderPreserved="false"
        app:rowCount="1"
        app:columnCount="3"
        android:id="@+id/backRow"
        app:srcCompat="@drawable/ic_rounded_rectangle_accent"
        app:orientation="horizontal">

        <TextView
            android:id="@+id/deleteText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/deleteTask"
            android:textColor="@android:color/white"
            android:textStyle="bold"
            android:textSize="26sp"
            app:layout_column="2"
            app:layout_gravity="end|center_vertical"/>

        <TextView
            android:id="@+id/completeText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/completeTask"
            android:textStyle="bold"
            android:textColor="@android:color/white"
            android:textSize="26sp"
            app:layout_column="0"
            app:layout_gravity="start|center_vertical"/>


    </android.support.v7.widget.GridLayout>



    <android.support.v7.widget.GridLayout
        android:id="@+id/foreRow"
        android:layout_width="380dp"
        android:layout_height="match_parent"
        android:layout_gravity="center_horizontal"
        app:alignmentMode="alignMargins"
        app:columnOrderPreserved="false"
        app:rowCount="2"
        app:columnCount="3"
        app:orientation="horizontal"
        app:srcCompat="@drawable/ic_rounded_rectangle"
        >

        <ImageView
            android:id="@+id/rowImage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_column="0"
            app:layout_row="0"
            android:layout_marginTop="6dp"
            app:srcCompat="@drawable/ic_info_outline"
            app:layout_gravity="start|center_vertical"
            />

        <TextView
            android:id="@+id/rowTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@android:color/white"
            app:layout_row="0"
            app:layout_column="1"
            android:textSize="20sp"
            app:layout_gravity="start"
            />

        <TextView
            android:id="@+id/rowContent"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@android:color/white"
            app:layout_row="1"
            app:layout_column="1"
            android:textSize="20sp"
            app:layout_gravity="start"/>


    </android.support.v7.widget.GridLayout>




</FrameLayout>
&#13;
&#13;
&#13;

爪哇:

 if(dX < 0){
                current.getDeleteText().setVisibility(View.VISIBLE);
                current.getCompleteText().setVisibility(View.INVISIBLE);
                current.getBackground().setBackground(getResources().getDrawable(R.drawable.ic_rounded_rectangle_accent));
            } else if(dX >0){
                current.getCompleteText().setVisibility(View.VISIBLE);
                current.getDeleteText().setVisibility(View.INVISIBLE);
                current.getBackground().setBackground(getResources().getDrawable(R.drawable.ic_rounded_rectangle_complete));
            }

错误堆栈跟踪:

致命的例外:主要                                                                         过程:

com.lab1.ac01220.bloomv2, PID: 7535
                                                                        android.content.res.Resources$NotFoundException: Resource ID #0x7f070068
                                                                            at android.content.res.Resources.getValue(Resources.java:1123)
                                                                            at android.support.v7.widget.AppCompatDrawableManager.loadDrawableFromDelegates(AppCompatDrawableManager.java:328)
                                                                            at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:193)
                                                                            at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:186)
                                                                            at com.lab1.ac01220.bloomv2.showAllLists_fragment.onChildDraw(showAllLists_fragment.java:128)
                                                                            at com.lab1.ac01220.bloomv2.RecycleItemTouchHelper.onChildDraw(RecycleItemTouchHelper.java:35)
                                                                            at android.support.v7.widget.helper.ItemTouchHelper$Callback.onDraw(ItemTouchHelper.java:1957)
                                                                            at android.support.v7.widget.helper.ItemTouchHelper.onDraw(ItemTouchHelper.java:547)
                                                                            at android.support.v7.widget.RecyclerView.onDraw(RecyclerView.java:4172)
                                                                            at android.view.View.draw(View.java:14465)
                                                                            at android.support.v7.widget.RecyclerView.draw(RecyclerView.java:4107)
                                                                            at android.view.View.getDisplayList(View.java:13362)
                                                                            at android.view.View.getDisplayList(View.java:13404)
                                                                            at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3077)
                                                                            at android.view.View.getDisplayList(View.java:13300)
                                                                            at android.view.View.getDisplayList(View.java:13404)
                                                                            at android.view.View.draw(View.java:14182)
                                                                            at android.view.ViewGroup.drawChild(ViewGroup.java:3103)
                                                                            at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)
                                                                            at android.view.View.draw(View.java:14468)
                                                                            at android.support.v4.view.ViewPager.draw(ViewPager.java:2420)
                                                                            at android.view.View.getDisplayList(View.java:13362)
                                                                            at android.view.View.getDisplayList(View.java:13404)
                                                                            at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3077)
                                                                            at android.view.View.getDisplayList(View.java:13300)
                                                                            at android.view.View.getDisplayList(View.java:13404)
                                                                            at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3077)
                                                                            at android.view.View.getDisplayList(View.java:13300)
                                                                            at android.view.View.getDisplayList(View.java:13404)
                                                                            at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3077)
                                                                            at android.view.View.getDisplayList(View.java:13300)
                                                                            at android.view.View.getDisplayList(View.java:13404)
                                                                            at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3077)
                                                                            at android.view.View.getDisplayList(View.java:13300)
                                                                            at android.view.View.getDisplayList(View.java:13404)
                                                                            at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3077)
                                                                            at android.view.View.getDisplayList(View.java:13300)
                                                                            at android.view.View.getDisplayList(View.java:13404)
                                                                            at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3077)
                                                                            at android.view.View.getDisplayList(View.java:13300)
                                                                            at android.view.View.getDisplayList(View.java:13404)
                                                                            at android.view.HardwareRenderer$GlRenderer.buildDisplayList(HardwareRenderer.java:1569)
                                                                            at android.view.HardwareRenderer$GlRenderer.draw(HardwareRenderer.java:1448)
                                                                            at android.view.ViewRootImpl.draw(ViewRootImpl.java:2381)
                                                                            at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2253)
                                                                            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1883)
                                                                            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
                                                                            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5670)
                                                                            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
                                                                            at android.view.Choreographer.doCallbacks(Choreographer.java:574)
                                                                            at android.view.Choreographer.doFrame(Choreographer.java:544)
                                                                            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
                                                                            at android.os.Handler.handleCallback(Handler.java:733)
                                                                            at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                            at android.os.Looper.loop(Looper.java:136)
                                                                            at android.app.ActivityThread.main(ActivityThread.java:5017)
                                                                            at java.lang.reflect.Method.invokeNative(Native Method)
                                                                            at java.lang.reflect.Method.invoke(Method.java:515)
                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
                                                                            at com.android.internal

3 个答案:

答案 0 :(得分:0)

尝试以这种方式获取矢量 -

Drawable d = AppCompatDrawableManager.get().getDrawable(context, R.drawable.drawable_id);

答案 1 :(得分:0)

试试这个: -

   Drawable your_drawable = VectorDrawableCompat.create(getResources(), R.drawable.ic_rounded_rectangle_accent, null);
   current.setBackground(your_drawable);

以编程方式将其转换为矢量可绘制.. 已知问题与矢量drawable in lower api

答案 2 :(得分:0)

检查您是否使用属性在XML drawable中设置颜色,这在使用OS Pre-Lollipop(&lt; 5.0)的设备中得不到很好的支持。 可以帮助的另一件事是在支持

中打开
var index = 1;
var name = "option"+index;
var res = $("select option").eq(index-1).data(name);

console.log(res);

另请参阅:Using android vector Drawables on pre Lollipop crash