当在Android 1.6上使用带有列表状态Drawable的mutate时,我总是得到一个空指针异常.它在更高的AndroID版本上完美运行.我使用默认的androID List_selector_background作为drawable来变异,我需要改变它,否则按下背景会变得非常混乱 – >当按下一个或多个时,它们中的全部或部分都会突出显示.
该项目在这里:
http://code.google.com/p/tree-view-list-android/
相关的一段代码在这里:http://code.google.com/p/tree-view-list-android/source/browse/src/pl/polidea/treeview/AbstractTreeViewAdapter.java#205
return activity.getResources() .getDrawable(androID.R.drawable.List_selector_background) .mutate();
堆栈跟踪如下:
3-11 11:37:39.973: ERROR/AndroIDRuntime(5304): java.lang.NullPointerException03-11 11:37:39.973: ERROR/AndroIDRuntime(5304): at androID.graphics.drawable.StateListDrawable.mutate(StateListDrawable.java:227)03-11 11:37:39.973: ERROR/AndroIDRuntime(5304): at pl.polIDea.treevIEw.AbstractTreeVIEwAdapter.getDrawableOrDefaultBackground(AbstractTreeVIEwAdapter.java:201)03-11 11:37:39.973: ERROR/AndroIDRuntime(5304): at pl.polIDea.treevIEw.AbstractTreeVIEwAdapter.populateTreeItem(AbstractTreeVIEwAdapter.java:210)03-11 11:37:39.973: ERROR/AndroIDRuntime(5304): at pl.polIDea.treevIEw.AbstractTreeVIEwAdapter.getVIEw(AbstractTreeVIEwAdapter.java:153)03-11 11:37:39.973: ERROR/AndroIDRuntime(5304): at androID.Widget.AbsListVIEw.obtainVIEw(AbsListVIEw.java:1273)03-11 11:37:39.973: ERROR/AndroIDRuntime(5304): at androID.Widget.ListVIEw.makeAndAddVIEw(ListVIEw.java:1658)03-11 11:37:39.973: ERROR/AndroIDRuntime(5304): at androID.Widget.ListVIEw.fillDown(ListVIEw.java:637)03-11 11:37:39.973: ERROR/AndroIDRuntime(5304): at androID.Widget.ListVIEw.fillFromtop(ListVIEw.java:694)03-11 11:37:39.973: ERROR/AndroIDRuntime(5304): at androID.Widget.ListVIEw.layoutChildren(ListVIEw.java:1516)03-11 11:37:39.973: ERROR/AndroIDRuntime(5304): at androID.Widget.AbsListVIEw.onLayout(AbsListVIEw.java:1112)03-11 11:37:39.973: ERROR/AndroIDRuntime(5304): at androID.vIEw.VIEw.layout(VIEw.java:6569)03-11 11:37:39.973: ERROR/AndroIDRuntime(5304): at androID.Widget.linearLayout.setChildFrame(linearLayout.java:1119)03-11 11:37:39.973: ERROR/AndroIDRuntime(5304): at androID.Widget.linearLayout.layoutVertical(linearLayout.java:998)03-11 11:37:39.973: ERROR/AndroIDRuntime(5304): at androID.Widget.linearLayout.onLayout(linearLayout.java:918)03-11 11:37:39.973: ERROR/AndroIDRuntime(5304): at androID.vIEw.VIEw.layout(VIEw.java:6569)03-11 11:37:39.973: ERROR/AndroIDRuntime(5304): at androID.Widget.FrameLayout.onLayout(FrameLayout.java:333)03-11 11:37:39.973: ERROR/AndroIDRuntime(5304): at androID.vIEw.VIEw.layout(VIEw.java:6569)03-11 11:37:39.973: ERROR/AndroIDRuntime(5304): at androID.Widget.linearLayout.setChildFrame(linearLayout.java:1119)03-11 11:37:39.973: ERROR/AndroIDRuntime(5304): at androID.Widget.linearLayout.layoutVertical(linearLayout.java:998)03-11 11:37:39.973: ERROR/AndroIDRuntime(5304): at androID.Widget.linearLayout.onLayout(linearLayout.java:918)03-11 11:37:39.973: ERROR/AndroIDRuntime(5304): at androID.vIEw.VIEw.layout(VIEw.java:6569)03-11 11:37:39.973: ERROR/AndroIDRuntime(5304): at androID.Widget.FrameLayout.onLayout(FrameLayout.java:333)03-11 11:37:39.973: ERROR/AndroIDRuntime(5304): at androID.vIEw.VIEw.layout(VIEw.java:6569)03-11 11:37:39.973: ERROR/AndroIDRuntime(5304): at androID.vIEw.VIEwRoot.performTraversals(VIEwRoot.java:979)03-11 11:37:39.973: ERROR/AndroIDRuntime(5304): at androID.vIEw.VIEwRoot.handleMessage(VIEwRoot.java:1613)03-11 11:37:39.973: ERROR/AndroIDRuntime(5304): at androID.os.Handler.dispatchMessage(Handler.java:99)03-11 11:37:39.973: ERROR/AndroIDRuntime(5304): at androID.os.Looper.loop(Looper.java:123)03-11 11:37:39.973: ERROR/AndroIDRuntime(5304): at androID.app.ActivityThread.main(ActivityThread.java:4203)03-11 11:37:39.973: ERROR/AndroIDRuntime(5304): at java.lang.reflect.Method.invokeNative(Native Method)03-11 11:37:39.973: ERROR/AndroIDRuntime(5304): at java.lang.reflect.Method.invoke(Method.java:521)03-11 11:37:39.973: ERROR/AndroIDRuntime(5304): at com.androID.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)03-11 11:37:39.973: ERROR/AndroIDRuntime(5304): at com.androID.internal.os.ZygoteInit.main(ZygoteInit.java:549)03-11 11:37:39.973: ERROR/AndroIDRuntime(5304): at dalvik.system.NativeStart.main(Native Method)
解决方法:
似乎AndroID 1.6中存在一个错误.当您深入了解AndroID 1.6源代码时,您可以找到以下代码:
androID.graphics.drawable.StateListDrawable.java:...public Drawable mutate() { if (!mMutated && super.mutate() == this) { final int[][] sets = mStateListState.mStateSets; final int count = sets.length; mStateListState.mStateSets = new int[count][]; for (int i = 0; i < count; i++) { mStateListState.mStateSets[i] = sets[i].clone(); //NPE causing line } mMutated = true; } return this;}...
排队
mStateListState.mStateSets[i] = sets[i].clone(); //NPE causing line
clone() method is sometimes called on null object and it causes NullPointerException
在AndroID> 1.6
它已被修复:
public Drawable mutate() { if (!mMutated && super.mutate() == this) { final int[][] sets = mStateListState.mStateSets; final int count = sets.length; mStateListState.mStateSets = new int[count][]; for (int i = 0; i < count; i++) { final int[] set = sets[i]; if (set != null) { mStateListState.mStateSets[i] = set.clone(); } } mMutated = true; } return this; }
但在AndroID 1.6中我们需要做一些解决方法.让我们看看为什么mStateSets [i]有时包含空值:
androID.graphics.drawable.DrawableContainer.DrowableContainerState:... public final int addChild(Drawable dr) { final int pos = mNumChildren; if (pos >= 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; }...
在从xml膨胀对象期间调用上面的方法.所以mStateListState.mStateSets的大小是N * 10.
现在让我们看看你试图从androID.R.drawable.List_selector_background引用的资源中膨胀的List_selector_background.xml的主体:
<?xml version="1.0" enCoding="utf-8"?><!-- 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.--><selector xmlns:androID="http://schemas.androID.com/apk/res/androID"> <item androID:state_window_focused="false" androID:drawable="@color/transparent" /> <!-- Even though these two point to the same resource, have two states so the drawable will invalIDate itself when coming out of pressed state. --> <item androID:state_focused="true" androID:state_enabled="false" androID:state_pressed="true" androID:drawable="@drawable/List_selector_background_Disabled" /> <item androID:state_focused="true" androID:state_enabled="false" androID:drawable="@drawable/List_selector_background_Disabled" /> <item androID:state_focused="true" androID:state_pressed="true" androID:drawable="@drawable/List_selector_background_Transition" /> <item androID:state_focused="false" androID:state_pressed="true" androID:drawable="@drawable/List_selector_background_Transition" /> <item androID:state_focused="true" androID:drawable="@drawable/List_selector_background_focus" /></selector>
此文件仅包含6个项目!
那么我们如何解决这个问题呢?
1.在{Project} / res / drowable /中创建List_selector_background.xml,您可以复制AndroID one并只更改一行:
<item androID:state_window_focused="false" androID:drawable="@color/transparent" />
至
<item androID:state_window_focused="false" androID:drawable="@androID:color/transparent" />
>复制AndroID或创建自己的文件:
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
>修改List_selector_background.xml添加虚拟项目以完全匹配(6 4 = 10)个项目condIDtion:
<item androID:animationCache="true" androID:drawable="@androID:color/transparent" /><item androID:animationCache="false" androID:drawable="@androID:color/transparent" /><item androID:alwaysDrawnWithCache="false" androID:drawable="@androID:color/transparent" /><item androID:alwaysDrawnWithCache="true" androID:drawable="@androID:color/transparent" />
瞧.编译并运行.它现在应该工作.
总结以上是内存溢出为你收集整理的Drawable的mutate方法Android 1.6中的NullPointerException全部内容,希望文章能够帮你解决Drawable的mutate方法Android 1.6中的NullPointerException所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)