android layout.xml文件中编译时出现空指针异常

android layout.xml文件中编译时出现空指针异常,android,android-layout,android-listview,nullpointerexception,Android,Android Layout,Android Listview,Nullpointerexception,当我在布局中使用自定义小部件类型时,我得到空指针异常。我的src文件中也有这个小部件,但找不到为什么会出现这个异常 它在编译时为com.siliconithub.android.zoom.ImageZoomView提供以下错误: java.lang.NullPointerException 异常详细信息记录在窗口>显示视图>错误日志中 这是我的item\u image\u detail.xml文件 <?xml version="1.0" encoding="utf-8"?> <

当我在布局中使用自定义小部件类型时,我得到空指针异常。我的src文件中也有这个小部件,但找不到为什么会出现这个异常

它在编译时为com.siliconithub.android.zoom.ImageZoomView提供以下错误:

java.lang.NullPointerException

异常详细信息记录在窗口>显示视图>错误日志中

这是我的item\u image\u detail.xml文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="320dp"
    android:layout_height="fill_parent"
    android:background="#FFF"
    android:minWidth="320dp"
    android:orientation="vertical" >

<LinearLayout
    android:id="@+id/frameImage"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="3dp"
    android:layout_marginRight="3dp"
    android:background="#000"
    android:gravity="center_horizontal" >

    <com.siliconithub.android.zoom.ImageZoomView
        android:id="@+id/imgWithZoom"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />
</LinearLayout>

<TableRow
    android:id="@+id/rowOptionsImage"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_marginLeft="3dp"
    android:layout_marginRight="3dp"
    android:background="@drawable/bg_bottom"
    android:gravity="center" >

    <TextView
        android:id="@+id/totalLikes"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingRight="10dp"
        android:textColor="#FFF" />

    <ImageButton
        android:id="@+id/btnLike"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent"
        android:contentDescription="@string/description_btnlike"
        android:src="@drawable/btn_like" />

    <TextView
        android:id="@+id/totalComments"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:drawableLeft="@drawable/ic_comment"
        android:gravity="center_vertical"
        android:paddingLeft="50dp"
        android:textColor="#FFF" />
</TableRow>
ImageZoomView的代码:

public class ImageZoomView extends View implements Observer {

    /** Paint object used when drawing bitmap. */
    private final Paint mPaint = new Paint(Paint.FILTER_BITMAP_FLAG);

    /** Rectangle used (and re-used) for cropping source image. */
    private final Rect mRectSrc = new Rect();

    /** Rectangle used (and re-used) for specifying drawing area on canvas. */
    private final Rect mRectDst = new Rect();

    /** Object holding aspect quotient */
    private final AspectQuotient mAspectQuotient = new AspectQuotient();

    /** The bitmap that we're zooming in, and drawing on the screen. */
    private Bitmap mBitmap;

    /** State of the zoom. */
    private ZoomState mState;

    // Public methods

    /**
     * Constructor
     */
    public ImageZoomView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    /**
     * Set image bitmap
     * 
     * @param bitmap The bitmap to view and zoom into
     */
    public void setImage(Bitmap bitmap) {
        mBitmap = bitmap;

        mAspectQuotient.updateAspectQuotient(getWidth(), getHeight(), mBitmap.getWidth(), mBitmap
            .getHeight());
        mAspectQuotient.notifyObservers();

        invalidate();
    }

    /**
     * Set object holding the zoom state that should be used
     * 
     * @param state The zoom state
     */
    public void setZoomState(ZoomState state) {
        if (mState != null) {
            mState.deleteObserver(this);
        }

        mState = state;
        mState.addObserver(this);

        invalidate();
    }

    /**
     * Gets reference to object holding aspect quotient
     * 
     * @return Object holding aspect quotient
     */
    public AspectQuotient getAspectQuotient() {
        return mAspectQuotient;
    }

    // Superclass overrides

    @Override
    protected void onDraw(Canvas canvas) {
        if (mBitmap != null && mState != null) {
            final float aspectQuotient = mAspectQuotient.get();

            final int viewWidth = getWidth();
            final int viewHeight = getHeight();
            final int bitmapWidth = mBitmap.getWidth();
            final int bitmapHeight = mBitmap.getHeight();

            final float panX = mState.getPanX();
            final float panY = mState.getPanY();
            final float zoomX = mState.getZoomX(aspectQuotient) * viewWidth / bitmapWidth;
            final float zoomY = mState.getZoomY(aspectQuotient) * viewHeight / bitmapHeight;

            // Setup source and destination rectangles
            mRectSrc.left = (int)(panX * bitmapWidth - viewWidth / (zoomX * 2));
            mRectSrc.top = (int)(panY * bitmapHeight - viewHeight / (zoomY * 2));
            mRectSrc.right = (int)(mRectSrc.left + viewWidth / zoomX);
            mRectSrc.bottom = (int)(mRectSrc.top + viewHeight / zoomY);
            mRectDst.left = getLeft();
            mRectDst.top = getTop();
            mRectDst.right = getRight();
            mRectDst.bottom = getBottom();

            // Adjust source rectangle so that it fits within the source image.
            if (mRectSrc.left < 0) {
                mRectDst.left += -mRectSrc.left * zoomX;
                mRectSrc.left = 0;
            }
            if (mRectSrc.right > bitmapWidth) {
                mRectDst.right -= (mRectSrc.right - bitmapWidth) * zoomX;
                mRectSrc.right = bitmapWidth;
            }
            if (mRectSrc.top < 0) {
                mRectDst.top += -mRectSrc.top * zoomY;
                mRectSrc.top = 0;
            }
            if (mRectSrc.bottom > bitmapHeight) {
                mRectDst.bottom -= (mRectSrc.bottom - bitmapHeight) * zoomY;
                mRectSrc.bottom = bitmapHeight;
            }

            canvas.drawBitmap(mBitmap, mRectSrc, mRectDst, mPaint);
        }
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom)     {
        super.onLayout(changed, left, top, right, bottom);

        mAspectQuotient.updateAspectQuotient(right - left, bottom - top, mBitmap.getWidth(),
                mBitmap.getHeight());
        mAspectQuotient.notifyObservers();
    }

    // implements Observer
    public void update(Observable observable, Object data) {
        invalidate();
    }
}
公共类ImageZoomView扩展视图{
/**绘制位图时使用的绘制对象*/
私有最终油漆mPaint=新油漆(油漆.过滤器\u位图\u标志);
/**用于(并重复使用)裁剪源图像的矩形*/
private final Rect mRectSrc=new Rect();
/**用于(并重复使用)在画布上指定绘图区域的矩形*/
private final Rect mRectDst=new Rect();
/**对象保持体商*/
private final aspectquotine maspectquotine=新aspectquotine();
/**我们正在放大的位图,并在屏幕上绘制*/
私有位图mBitmap;
/**缩放的状态*/
私人动物园状态;
//公共方法
/**
*建造师
*/
公共图像ZoomView(上下文、属性集属性){
超级(上下文,attrs);
}
/**
*设置图像位图
* 
*@param bitmap要查看和放大的位图
*/
公共void setImage(位图){
mBitmap=位图;
更新光谱商(getWidth(),getHeight(),mBitmap.getWidth(),mBitmap
.getHeight());
mAspectQuotient.notifyObservators();
使无效();
}
/**
*设置保持应使用的缩放状态的对象
* 
*@param state缩放状态
*/
public void setZoomState(ZoomState状态){
if(mState!=null){
mState.deleteObserver(本);
}
mState=状态;
mState.addObserver(本);
使无效();
}
/**
*获取对包含方面商的对象的引用
* 
*@return对象持有方面商
*/
公共AspectQuotient getAspectQuotient(){
返回maspect商;
}
//超类重写
@凌驾
受保护的void onDraw(画布){
if(mBitmap!=null&&mState!=null){
final float aspectQuotient=mAspectQuotient.get();
final int viewWidth=getWidth();
最终int viewHeight=getHeight();
final int bitmapWidth=mBitmap.getWidth();
final int bitmapHeight=mBitmap.getHeight();
final float panX=mState.getPanX();
最终float panY=mState.getPanY();
最终浮点zoomX=mState.getZoomX(AspectQuotion)*viewWidth/bitmapWidth;
最终浮点zoomY=mState.getZoomY(AspectQuotion)*视图高度/位图高度;
//设置源和目标矩形
mRectSrc.left=(int)(panX*bitmapWidth-viewWidth/(zoomX*2));
mRectSrc.top=(int)(公司*位图高度-视图高度/(缩放*2));
mRectSrc.right=(int)(mRectSrc.left+viewWidth/zoomX);
mRectSrc.bottom=(int)(mRectSrc.top+viewHeight/zoomY);
mRectDst.left=getLeft();
mRectDst.top=getTop();
mRectDst.right=getRight();
mRectDst.bottom=getBottom();
//调整源矩形,使其适合源图像。
if(mRectSrc.left<0){
mRectDst.left+=-mRectSrc.left*zoomX;
mRectSrc.left=0;
}
if(mRectSrc.right>bitmapWidth){
mRectDst.right-=(mRectSrc.right-位图宽度)*zoomX;
mRectSrc.right=位图宽度;
}
如果(mRectSrc.top<0){
mRectDst.top+=-mRectSrc.top*缩放;
mRectSrc.top=0;
}
if(mRectSrc.bottom>位图高度){
mRectDst.bottom-=(mRectSrc.bottom-位图高度)*缩放;
mRectSrc.bottom=位图高度;
}
drawBitmap(mBitmap、mRectSrc、mRectDst、mPaint);
}
}
@凌驾
仅限受保护的空心布局(布尔值已更改、整数左侧、整数顶部、整数右侧、整数底部){
超级。仅限布局(已更改、左、上、右、下);
UpdateSpectCo商(右-左,下-上,mBitmap.getWidth(),
mBitmap.getHeight());
mAspectQuotient.notifyObservators();
}
//实现观察器
公共无效更新(可观测、对象数据){
使无效();
}
}

您缺少构造函数:

/**
 * Constructors
 */


public ImageZoomView(Context context) {
    this(context, null);
}

public ImageZoomView(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
}

public ImageZoomView(Context context, AttributeSet attrs, int defaultStyles) {
    super(context, attrs, defaultSytles);
}

我已经用logcat更新了我的答案。请参阅我的编辑部分。我将从ImageZoomView.java中的第166行开始
java.lang.NullPointerException位于com.siliconithub.android.zoom.ImageZoomView.onLayout(ImageZoomView.java:166)
您能发布包含ImageZoomView第166行的函数代码吗?错误来自于此,现在我也用代码更新了答案…看起来
mBitmap
mAspectQuotient
为空。第166行是什么?您是否使用调试器查看哪个变量为空?
/**
 * Constructors
 */


public ImageZoomView(Context context) {
    this(context, null);
}

public ImageZoomView(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
}

public ImageZoomView(Context context, AttributeSet attrs, int defaultStyles) {
    super(context, attrs, defaultSytles);
}