msrd0
msrd0

Reputation: 8361

Error while loading custom Android View: Resources$NotFoundException: Resource ID #0x7f060007 type #0x12 is not valid

I have this XML file called player_small.xml in the res/layout folder of my project:

<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>

Then, I have defined a custom view called SmallPlayerView containing all 3 constructors from the superclass FrameLayout all calling this method:

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

InteliJ IDEA can display the class correct when using it from another XML-Layout-file, but when I launch it on my phone or the emulator it crashes with this exception:

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)

I have opened the R.class file and looked into it, and all resources are present. How can I solve this Exception?

Upvotes: 0

Views: 6376

Answers (1)

Mohammad Rahchamani
Mohammad Rahchamani

Reputation: 5220

you have to change your code to this :

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

you can inflate views with layout attribute not id

Upvotes: 8

Related Questions