Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>It seems that there is a bug in Android 1.6. When you dive into Android 1.6 sources you can find following code:</p> <pre><code>android.graphics.drawable.StateListDrawable.java: ... public Drawable mutate() { if (!mMutated &amp;&amp; super.mutate() == this) { final int[][] sets = mStateListState.mStateSets; final int count = sets.length; mStateListState.mStateSets = new int[count][]; for (int i = 0; i &lt; count; i++) { mStateListState.mStateSets[i] = sets[i].clone(); //NPE causing line } mMutated = true; } return this; } ... </code></pre> <p>in line</p> <pre><code>mStateListState.mStateSets[i] = sets[i].clone(); //NPE causing line </code></pre> <blockquote> <p>clone() method is sometimes called on null object and it causes NullPointerException</p> </blockquote> <p>in Android > 1.6 it has been fixed:</p> <pre><code>public Drawable mutate() { if (!mMutated &amp;&amp; super.mutate() == this) { final int[][] sets = mStateListState.mStateSets; final int count = sets.length; mStateListState.mStateSets = new int[count][]; for (int i = 0; i &lt; count; i++) { final int[] set = sets[i]; if (set != null) { mStateListState.mStateSets[i] = set.clone(); } } mMutated = true; } return this; } </code></pre> <p>but in Android 1.6 we need to do some workaround. Let's see why mStateSets[i] sometimes contains nulls:</p> <pre><code>android.graphics.drawable.DrawableContainer.DrowableContainerState: ... public final int addChild(Drawable dr) { final int pos = mNumChildren; if (pos &gt;= mDrawables.length) { growArray(pos, pos+10); //Interesting line } dr.setVisible(false, true); dr.setCallback(mOwner); mDrawables[pos] = dr; mNumChildren++; mChildrenChangingConfigurations |= dr.getChangingConfigurations(); mHaveOpacity = false; mHaveStateful = false; mConstantPadding = null; mPaddingChecked = false; mComputedConstantSize = false; return pos; } ... </code></pre> <p>above method is called during inflating object from xml. So the size of mStateListState.mStateSets is N*10.</p> <p>Now let's see body of list_selector_background.xml that you are trying to inflate from resources refering by android.R.drawable.list_selector_background:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;!-- Copyright (C) 2008 The Android Open Source Project Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --&gt; &lt;selector xmlns:android="http://schemas.android.com/apk/res/android"&gt; &lt;item android:state_window_focused="false" android:drawable="@color/transparent" /&gt; &lt;!-- Even though these two point to the same resource, have two states so the drawable will invalidate itself when coming out of pressed state. --&gt; &lt;item android:state_focused="true" android:state_enabled="false" android:state_pressed="true" android:drawable="@drawable/list_selector_background_disabled" /&gt; &lt;item android:state_focused="true" android:state_enabled="false" android:drawable="@drawable/list_selector_background_disabled" /&gt; &lt;item android:state_focused="true" android:state_pressed="true" android:drawable="@drawable/list_selector_background_transition" /&gt; &lt;item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/list_selector_background_transition" /&gt; &lt;item android:state_focused="true" android:drawable="@drawable/list_selector_background_focus" /&gt; &lt;/selector&gt; </code></pre> <p><strong>this file contains only 6 items!</strong> So how can we solve the issue? 1. Create list_selector_background.xml in {Project}/res/drowable/ you can copy Android one and change only one line:</p> <pre><code>&lt;item android:state_window_focused="false" android:drawable="@color/transparent" /&gt; </code></pre> <p>to</p> <pre><code>&lt;item android:state_window_focused="false" android:drawable="@android:color/transparent" /&gt; </code></pre> <ol> <li><p>Copy Android or create own files:</p> <p>list_selector_background_disabled.9.png list_selector_background_focus.9.png list_selector_background_longpress.9.png list_selector_background_pressed.9.png list_selector_background_transition.xml</p></li> <li><p>Modify list_selector_background.xml adding dummy items to match exactly (6+4=10) items condidtion:</p> <pre><code>&lt;item android:animationCache="true" android:drawable="@android:color/transparent" /&gt; &lt;item android:animationCache="false" android:drawable="@android:color/transparent" /&gt; &lt;item android:alwaysDrawnWithCache="false" android:drawable="@android:color/transparent" /&gt; &lt;item android:alwaysDrawnWithCache="true" android:drawable="@android:color/transparent" /&gt; </code></pre></li> </ol> <p>Voilà. Compile and run. It should work now.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload