java.lang.NullPointerException
Exception?details?are?logged?in?Window?>?Show?View?>?Error?Log
java.lang.NullPointerException
????at?com.example.weixin.ChangeColorIconWithText.onDraw(ChangeColorIconWithText.java:85)
????at?android.view.View.draw(View.java:14465)
????at?android.view.View.draw(View.java:14350)
????at?android.view.ViewGroup.drawChild(ViewGroup.java:3103)
????at?android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)
????at?android.view.View.draw(View.java:14468)
????at?android.view.View.draw(View.java:14350)
????at?android.view.ViewGroup.drawChild(ViewGroup.java:3103)
????at?android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)
????at?android.view.View.draw(View.java:14348)
????at?android.view.ViewGroup.drawChild(ViewGroup.java:3103)
????at?android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)
????at?android.view.View.draw(View.java:14348)
????at?android.view.ViewGroup.drawChild(ViewGroup.java:3103)
????at?android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)
????at?android.view.View.draw(View.java:14468)
????at?android.view.View.draw(View.java:14350)
????at?android.view.ViewGroup.drawChild(ViewGroup.java:3103)
????at?android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)
????at?android.view.View.draw(View.java:14468)
????下面是代碼:
????package?com.example.weixin;
import?android.content.Context;
import?android.content.res.TypedArray;
import?android.graphics.Bitmap;
import?android.graphics.Canvas;
import?android.graphics.Paint;
import?android.graphics.Rect;
import?android.graphics.drawable.BitmapDrawable;
import?android.util.AttributeSet;
import?android.util.TypedValue;
import?android.view.View;
public?class?ChangeColorIconWithText?extends?View?{
private?int?mColor?=?0xFF45C01A;
private?Bitmap?mIconBitmap;
private?String?mTextString?=?"微信";
private?int?mTextSize?=?(int)?TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,?12,?
getResources().getDisplayMetrics());
private?Canvas?mCanvas;
private?Bitmap?mBitmap;
private?Paint?mPaint;
private?float?mAlpha;
private?Rect?mIconRect;
private?Rect?mTextBound;
private?Paint?mTextPaint;
public?ChangeColorIconWithText(Context?context,?AttributeSet?attrs,
int?defStyleAttr)?{
super(context,?attrs,?defStyleAttr);
TypedArray?a?=?context.obtainStyledAttributes(attrs,?
R.styleable.ChangeColorIconWithText);
int?n?=?a.getIndexCount();
for?(int?i?=?0;?i?<?n;?i++)?{
int?attr?=?a.getIndex(i);
switch?(attr)?{
case?R.styleable.ChangeColorIconWithText_icon1:
BitmapDrawable?drawable?=?(BitmapDrawable)?a.getDrawable(attr);
mIconBitmap?=?drawable.getBitmap();
break;
case?R.styleable.ChangeColorIconWithText_color:
mColor?=?a.getColor(attr,?0xFF45C01A);
break;
case?R.styleable.ChangeColorIconWithText_text:
mTextString?=?a.getString(attr);
break;
case?R.styleable.ChangeColorIconWithText_text_size:
mTextSize?=?(int)?a.getDimension(attr,?
TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,?
12,?getResources().getDisplayMetrics()));
break;
}
}
a.recycle();
mTextBound?=?new?Rect();
mTextPaint?=?new?Paint();
mTextPaint.setTextSize(mTextSize);
mTextPaint.setColor(0Xff555555);
mTextPaint.getTextBounds(mTextString,?0,?mTextString.length(),?mTextBound);
}
@Override
protected?void?onMeasure(int?widthMeasureSpec,?int?heightMeasureSpec)?{
//?TODO?Auto-generated?method?stub
super.onMeasure(widthMeasureSpec,?heightMeasureSpec);
int?iconWidth?=?Math.min(getMeasuredWidth()-
getPaddingLeft()-getPaddingRight(),?
getMeasuredHeight()-getPaddingTop()-
getPaddingBottom()-?mTextBound.height());
int?left?=?getMeasuredWidth()/2?-?iconWidth/2;
int?top?=?(getMeasuredHeight()-mTextBound.height())/2
-iconWidth/2;
mIconRect?=?new?Rect(left,?top,?left+iconWidth,?
top+iconWidth);
}
protected?void?onDraw(Canvas?canvas)?{
//?TODO?Auto-generated?method?stub
super.onDraw(canvas);
mCanvas.drawBitmap(mIconBitmap,?mIconRect,?mIconRect,?null);
}
public?ChangeColorIconWithText(Context?context,?AttributeSet?attrs)?{
this(context,attrs,0);
//?TODO?Auto-generated?constructor?stub
}
public?ChangeColorIconWithText(Context?context)?{
this(context,null);
//?TODO?Auto-generated?constructor?stub
}
}
2015-08-14
求解答??!