加载自定义Android视图时出错:资源$ NotFoundException:资源ID#0x7f060007类型#0x12无效

时间:2014-11-16 10:35:29

标签: java android xml android-layout intellij-idea

我在项目的player_small.xml文件夹中有一个名为res/layout的XML文件:

<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/player_small"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:rowCount="3"
        android:columnCount="4">

<ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/player_small_play"
        android:layout_gravity="center"
        android:layout_row="0"
        android:layout_rowSpan="2"
        android:layout_column="0"
        android:src="@drawable/play"/>

<ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/next"
        android:id="@+id/player_small_next"
        android:layout_gravity="center"
        android:layout_row="0"
        android:layout_rowSpan="2"
        android:layout_column="1"/>

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Title"
        android:textStyle="bold"
        android:textSize="@dimen/defaultFontSize"
        android:id="@+id/player_small_title"
        android:layout_gravity="center_vertical|left|right"
        android:layout_row="0"
        android:layout_column="3"/>

<Space
        android:layout_width="30px"
        android:layout_height="10px"
        android:layout_row="2"
        android:layout_column="2"/>

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Artist"
        android:textSize="@dimen/defaultFontSize"
        android:id="@+id/player_small_artist"
        android:layout_gravity="center_vertical|left|right"
        android:layout_row="1"
        android:layout_column="3"/>
</GridLayout>

然后,我定义了一个名为SmallPlayerView的自定义视图,其中包含来自超类FrameLayout的所有3个构造函数,它们都调用此方法:

protected void initSmallPlayerView ()
{
    View view = inflate(getContext(), R.id.player_small, null);
    addView(view);
}

当从另一个XML-Layout文件中使用它时,InteliJ IDEA可以正确显示该类,但是当我在手机或模拟器上启动它时,它会因此异常而崩溃:

Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f060007 type #0x12 is not valid
        at android.content.res.Resources.loadXmlResourceParser(Resources.java:2103)
        at android.content.res.Resources.getLayout(Resources.java:852)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:394)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
        at android.view.View.inflate(View.java:16119)
        at myapp.music.organizer.SmallPlayerView.initSmallPlayerView(SmallPlayerView.java:51)
        at myapp.music.organizer.SmallPlayerView.<init>(SmallPlayerView.java:46)
        at java.lang.reflect.Constructor.constructNative(Native Method)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
        at android.view.LayoutInflater.createView(LayoutInflater.java:587)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
        at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:256)
        at android.app.Activity.setContentView(Activity.java:1867)
        at myapp.music.organizer.MainActivity.onCreate(MainActivity.java:23)
        at android.app.Activity.performCreate(Activity.java:5008)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
        at android.app.ActivityThread.access$600(ActivityThread.java:130)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4745)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
        at dalvik.system.NativeStart.main(Native Method)

我打开了R.class文件并查看了它,所有资源都存在。我该如何解决这个例外?

1 个答案:

答案 0 :(得分:8)

您必须将代码更改为:

{
    View view = inflate(getContext(), R.layout.player_small, null);
    addView(view);
}

您可以使用布局属性非ID

来扩充视图