为了账号安全,请及时绑定邮箱和手机立即绑定

代码没有错误,但是运行后总是已经停止,在1-4 自定义底部View这一节内,前面的都可以运行出来

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
	}

}


正在回答

1 回答

求解答啊!

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消
微信6.0主界面
  • 参与学习       58941    人
  • 解答问题       188    个

本课程将讲解微信6.0主界面的实现方法,微信5.2.1后续课程

进入课程

代码没有错误,但是运行后总是已经停止,在1-4 自定义底部View这一节内,前面的都可以运行出来

我要回答 关注问题
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号