自定义控件类报错,求指点

如题,报错是这样的错误
08-12 13:03:52.588: E/AndroidRuntime(26278): at com.wujiecao.face_maker.HandWrite.(HandWrite.java:44)
请问是不是线程的问题?
相关代码
public void setbimap(byte[] bytrarry) {
originalBitmap = BitmapFactory.decodeByteArray(bytrarry, 0, bytrarry.length);
this.invalidate();
Canvas canvas = null;
onDraw(canvas);

}

public HandWrite(Context context, AttributeSet attrs) {
    super(context, attrs);
    // TODO Auto-generated constructor stub
    new1Bitmap = Bitmap.createBitmap(originalBitmap);

}

这个是一个自定义控件的类

5个回答

new1Bitmap = Bitmap.createBitmap(originalBitmap);
你构造的时候originalBitmap这个变量有赋值吗??

把Log和代码都贴全点出来吧

qq_26854927
白中白 代码我贴出来了,求指点谢谢
大约 5 年之前 回复

从你上面贴出的代码看你直接调用了ondraw,但传给ondraw的canvas是个null值,你确定这样写吗?而且从你现在的贴出来的东西根本没办法定位问题。。。。

qq_26854927
白中白 代码我贴出来了,求指点谢谢
大约 5 年之前 回复

package com.wujiecao.face_maker;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RadioGroup;

public class Second extends ActionBarActivity {
private ImageView imageView;
private Button next;
private RadioGroup pic;
// 下面是自己试写的

private int width, height;
private HandWrite handWrite = null;
private Button clear;
private int whichColor = 0, whichStrokeWidth = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

    super.onCreate(savedInstanceState);

    // imageView = (ImageView) findViewById(R.id.imageView1);
    Intent intent = getIntent();
    Bundle extras = getIntent().getExtras();
    byte[] byteArray = extras.getByteArray("IMG");
    handWrite.setbimap(byteArray);
    Log.e("TAG", byteArray + "");
    setContentView(R.layout.activity_second);
    /*
     * Bitmap bitmap = BitmapFactory.decodeByteArray(byteArray, 0,
     * byteArray.length);
     */

    // imageView.setImageBitmap(bitmap);

}

// 保存
public void saveBitmap(File file, Bitmap mBitmap) throws IOException {
    try {
        file.createNewFile();
        FileOutputStream fileOut = null;
        fileOut = new FileOutputStream(file);
        mBitmap.compress(Bitmap.CompressFormat.PNG, 100, fileOut);
        fileOut.flush();
        fileOut.close();
    } catch (FileNotFoundException e) {
        // TODO: handle exception
        e.printStackTrace();
    } catch (IOException e) {
        // TODO: handle exception
        e.printStackTrace();
    }
}

}
图片说明

qq_26854927
白中白 回复scnuxisan225: 解决了,谢谢。
大约 5 年之前 回复
scnuxisan225
scnuxisan225 回复白中白: 这样吧,听我的,先把基础打好~首先先去学习Java语法,把Java理解透,再深入Android~JAVA初阶可以看看《疯狂JAVA讲义》,Android入门请看官网的文章。请务必打好基础再学习Android~
大约 5 年之前 回复
qq_26854927
白中白 回复scnuxisan225: 初始化是不是private HandWrite handWrite=null;这样?如果是的话我就初始化了
大约 5 年之前 回复
scnuxisan225
scnuxisan225 回复白中白: handWrite这个变量你没初始化啊。。。
大约 5 年之前 回复
qq_26854927
白中白 回复scnuxisan225: 问题是不知道怎么改。
大约 5 年之前 回复
scnuxisan225
scnuxisan225 回复白中白: 出现NullPointerException,在Second.java的第42行,自己跟踪吧
大约 5 年之前 回复
scnuxisan225
scnuxisan225 回复白中白: Log不是告诉你错在哪了吗?
大约 5 年之前 回复
qq_26854927
白中白 这个是activity,关联HandlWriter帮助类的
大约 5 年之前 回复

package com.wujiecao.face_maker;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.lang.ref.SoftReference;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.util.AttributeSet;
import android.util.Base64;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

public class HandWrite extends View {
Paint paint = null;
private Bitmap originalBitmap = null;
Bitmap new1Bitmap = null;
private Bitmap new2Bitmap = null;
Bitmap m_originalBitmap;
private float clickX = 0, clickY = 0;
private float startX = 0, startY = 0;
private boolean isMove = true;
private boolean isClear = false;
int color = Color.WHITE;
float stroeWidth = 3.0f;

public Bitmap setbimap(byte[] bytrarry) {
    Log.e("TAG1", bytrarry + "");
    Bitmap mBitmap = null;
    InputStream inputStream = null;
    try {
        bytrarry = Base64.decode(bytrarry, Base64.DEFAULT);
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 8;
        inputStream = new ByteArrayInputStream(bytrarry);
        SoftReference softReference = new SoftReference(BitmapFactory.decodeStream(inputStream, null, options));
        mBitmap = (Bitmap) softReference.get();
    } catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
    } finally {
        if (bytrarry != null) {
            bytrarry = null;
        }
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (Exception e2) {
                // TODO: handle exception
                e2.printStackTrace();

            }

        }
    }
    // YuvImage yuvImage = new YuvImage(bytrarry, ImageFormat.NV21, 20, 20,
    // null);
    // ByteArrayOutputStream baos = new ByteArrayOutputStream();
    // yuvImage.compressToJpeg(new Rect(0, 0, 20, 20), 80, baos);
    // byte[] jdata = baos.toByteArray();
    // originalBitmap = BitmapFactory.decodeByteArray(jdata, 0,
    // jdata.length);
    // /*
    // * this.invalidate(); Canvas canvas = null; onDraw(canvas);
    // */
    originalBitmap = mBitmap;
    return mBitmap;
}

public HandWrite(Context context, AttributeSet attrs) {
    super(context, attrs);
    // TODO Auto-generated constructor stub

    // originalBitmap = BitmapFactory.decodeResource(getResources(),
    // R.drawable.ic_launcher)
    // .copy(Bitmap.Config.ARGB_8888, true);
    originalBitmap = setbimap(null);
    Log.e("TAG", originalBitmap + "");
    new1Bitmap = Bitmap.createBitmap(originalBitmap);

}

public void clear() {
    isClear = true;
    new2Bitmap = Bitmap.createBitmap(originalBitmap);
    invalidate();
}

public void setstyle(float strokeWidth) {
    this.stroeWidth = strokeWidth;
}

@Override
protected void onDraw(Canvas canvas) {
    // TODO Auto-generated method stub
    super.onDraw(canvas);
    canvas.drawBitmap(HandWriting(new1Bitmap), 0, 0, null);
}
//

//
public Bitmap HandWriting(Bitmap originalBitmap) {
    Canvas canvas = null;

    if (isClear) {
        canvas = new Canvas(new2Bitmap);
    } else {
        canvas = new Canvas(originalBitmap);
    }
    paint = new Paint();
    paint.setStyle(Style.STROKE);
    paint.setAntiAlias(true);
    paint.setColor(color);
    paint.setStrokeWidth(stroeWidth);
    if (isMove) {
        canvas.drawLine(startX, startY, clickX, clickY, paint);
    }
    startX = clickX;
    startY = clickY;
    if (isClear) {
        return new2Bitmap;
    }
    return originalBitmap;

}

@Override
public boolean onTouchEvent(MotionEvent event) {
    // TODO Auto-generated method stub
    clickX = event.getX();
    clickY = event.getY();
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        isMove = false;
        invalidate();
        return true;
    } else if (event.getAction() == MotionEvent.ACTION_MOVE) {
        isMove = true;
        invalidate();
        return true;
    }
    return super.onTouchEvent(event);

}

}

qq_26854927
白中白 这个是HandWriter帮助类
大约 5 年之前 回复

xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.wujiecao.face_maker.Second" >

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/button2"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:src="@drawable/ic_launcher" />

<Button
    android:id="@+id/next"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/imageView1"
    android:layout_alignParentBottom="true"
    android:text="next" />

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/next"
    android:layout_alignRight="@+id/imageView1" />

<RadioGroup
    android:id="@+id/radioGroup1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/next"
    android:layout_alignParentRight="true"
    android:orientation="horizontal" >

    <RadioButton
        android:id="@+id/radio0"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/item"
        android:checked="true" />

    <RadioButton
        android:id="@+id/radio1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="2" />

    <RadioButton
        android:id="@+id/radio2"
        android:layout_width="282dp"
        android:layout_height="wrap_content"
        android:text="3" />
</RadioGroup>

<com.wujiecao.face_maker.HandWrite
    android:id="@+id/handwriteview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentRight="true" />

huangjingfeng_2015
IT_拾荒者 回复白中白: 你的activity中handwrite是哥null值,并非实例化对象。。。。。补一补java的知识吧
大约 5 年之前 回复
qq_26854927
白中白 activity的layout文件
大约 5 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问