我有一个名为ZoomControlBar的自定义UI组件,它使用一个名为zoom_slider_bar的drawable。当我在XML中使用该组件时,我收到以下错误:
AndroidRuntime(8137): Caused by: android.content.res.Resources$NotFoundException:
File res/drawable/zoom_slider_bar.xml from drawable resource ID #0x7f02000a
AndroidRuntime(8137): at
android.content.res.Resources.loadDrawable(Resources.java:2091)
AndroidRuntime(8137): at
android.content.res.Resources.getDrawable(Resources.java:695)
AndroidRuntime(8137): at android.view.View.setBackgroundResource(View.java:14758)
AndroidRuntime(8137): at com.example.newcameraproject.ZoomControlBar.<init>
(ZoomControlBar.java:30)
AndroidRuntime(8137): ... 31 more
AndroidRuntime(8137): Caused by: java.lang.NullPointerException
AndroidRuntime(8137): at
android.graphics.drawable.DrawableContainer
$DrawableContainerState.addChild(DrawableContainer.java:527)
AndroidRuntime(8137): at
android.graphics.drawable.StateListDrawable
$StateListState.addStateSet(StateListDrawable.java:288)
AndroidRuntime(8137): at
android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:186)
AndroidRuntime(8137): at
android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:901)
AndroidRuntime(8137): at
android.graphics.drawable.Drawable.createFromXml(Drawable.java:837)
AndroidRuntime(8137): at
android.content.res.Resources.loadDrawable(Resources.java:2087)
AndroidRuntime(8137): ... 34 more
这是ZoomControlBar:
public class ZoomControlBar extends ZoomControl {
private static final String TAG="ZoomControlBar";
private static int THRESHOLD_FIRST_MOVE;
private static int ICON_SPACING;
private View mBar;
private boolean mStartChanging;
private int mSliderLength;
private int mHeight;
private int mIconHeight;
private int mTotalIconHeight;
public ZoomControlBar(Context c,AttributeSet attrs)
{
super(c,attrs);
THRESHOLD_FIRST_MOVE=dpToPixel(c, 10);
ICON_SPACING=dpToPixel(c,12);
mBar=new View(c);
mBar.setBackgroundResource(R.drawable.zoom_slider_bar);
addView(mBar);
}
@Override
public void setActivated(boolean activated)
{
super.setActivated(activated);
mBar.setActivated(activated);
}
private int getSliderPosition(int y)
{
int pos;//relative position in the slider
if(mDegree==180)
pos=mHeight-mTotalIconHeight;
else
pos=mHeight-mTotalIconHeight-y;
if(pos<0)
pos=0;
if(pos>mSliderLength)
pos=mSliderLength;
return pos;
}
@Override
protected void onSizeChanged(int w,int h,int oldW,int oldH)
{
mHeight = h;
mIconHeight = mZoomIn.getMeasuredHeight();
mTotalIconHeight = mIconHeight + ICON_SPACING;
mSliderLength = mHeight - (2 * mTotalIconHeight);
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
if (!isEnabled() || (mHeight == 0)) return false;
int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_OUTSIDE:
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
setActivated(false);
closeZoomControl();
break;
case MotionEvent.ACTION_DOWN:
setActivated(true);
mStartChanging = false;
case MotionEvent.ACTION_MOVE:
int pos = getSliderPosition((int) event.getY());
if (!mStartChanging) {
// Make sure the movement is large enough before we start
// changing the zoom.
int delta = mSliderPosition - pos;
if ((delta > THRESHOLD_FIRST_MOVE) ||
(delta < -THRESHOLD_FIRST_MOVE)) {
mStartChanging = true;
}
}
if (mStartChanging) {
performZoom(1.0d * pos / mSliderLength);
mSliderPosition = pos;
}
requestLayout();
}
return true;
}
@Override
public void setDegree(int degree) {
// layout for the left-hand camera control
if ((degree == 180) || (mDegree == 180)) requestLayout();
super.setDegree(degree);
}
@Override
protected void onLayout(
boolean changed, int left, int top, int right, int bottom) {
if (mZoomMax == 0) return;
int width = right - left;
mBar.layout(0, mTotalIconHeight, width, mHeight - mTotalIconHeight);
// For left-hand users, as the device is rotated for 180 degree,
// the zoom-in button should be on the top.
int pos; // slider position
int sliderPosition;
if (mSliderPosition != -1) { // -1 means invalid
sliderPosition = mSliderPosition;
} else {
sliderPosition = (int) ((double) mSliderLength * mZoomIndex / mZoomMax);
}
if (mDegree == 180) {
mZoomOut.layout(0, 0, width, mIconHeight);
mZoomIn.layout(0, mHeight - mIconHeight, width, mHeight);
pos = mBar.getTop() + sliderPosition;
} else {
mZoomIn.layout(0, 0, width, mIconHeight);
mZoomOut.layout(0, mHeight - mIconHeight, width, mHeight);
pos = mBar.getBottom() - sliderPosition;
}
int sliderHeight = mZoomSlider.getMeasuredHeight();
mZoomSlider.layout(0, (pos - sliderHeight / 2),
width, (pos + sliderHeight / 2));
}
@Override
public void setZoomIndex(int index) {
super.setZoomIndex(index);
mSliderPosition = -1; // -1 means invalid
requestLayout();
}
private int dpToPixel(Context c,int dp)
{
DisplayMetrics metrics=new DisplayMetrics();
WindowManager wm=(WindowManager)c.getSystemService(Context.WINDOW_SERVICE);
wm.getDefaultDisplay().getMetrics(metrics);
float pixelDensity=metrics.density;
return Math.round(dp*pixelDensity);
}
}
将背景drawable设置为:
时出现错误 mBar.setBackgroundResource(R.drawable.zoom_slider_bar);
这是我的抽签:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_activated="true" android:drawable="@drawable/ic_zoom_big" />
<item android:drawable="@drawable/ic_zoom_big_dark" />
</selector>
我将这9个补丁图片ic_zoom_big
,ic_zoom_big_dark
添加到drawable-hdpi
答案 0 :(得分:0)
将zoom_slider_bar.xml
放入普通drawable
文件夹而不是文件夹后再试一次,具体取决于drawable-mdpi
等屏幕大小。
如果你还没有,那就创建它。 [没有任何后缀]
这解决了我前段时间遇到的类似问题。