我在画布上绘制了一条路径,我希望它出现在ImageView中但是我得到一个NullPointerException,然后显示在堆栈跟踪中:
Process: com.example.abcd, PID: 9233
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.abcd/com.example.abcd.NewTest}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.abcd.NewTest.onCreate(NewTest.java:28)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
... 11 more
我就是这样做的:
public class NewTest extends Activity{
public static ImageView im1;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(new NewAnimation(this));
ImageView im = (ImageView)findViewById(R.id.imageView1);
im.setImageBitmap(NewAnimation.bitMap);
}
public static class NewAnimation extends View{
Paint paint;
Context context;
int screenW, screenH;
Path path;
float X,Y;
public static Bitmap bitMap;
Canvas canvas;
public NewAnimation(Context context) {
super(context);
this.context = context;
paint = new Paint();
paint.setAntiAlias(true);
paint.setStyle(Style.STROKE);
paint.setStrokeWidth(10);
paint.setColor(Color.RED);
path = new Path();
}
public NewAnimation(Context context, AttributeSet attrs) {
super(context);
this.context = context;
paint = new Paint();
paint.setAntiAlias(true);
paint.setStyle(Style.STROKE);
paint.setStrokeWidth(10);
paint.setColor(Color.RED);
path = new Path();
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
screenW = w;
screenH = h;
X = screenW/4;
Y = screenH/2;
path.moveTo(X, Y);
Bitmap.Config conf = Bitmap.Config.ARGB_8888;
bitMap = Bitmap.createBitmap(500, 1000, conf);
canvas = new Canvas(bitMap);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
path.lineTo(X+20, Y);
path.lineTo(X+20, Y+20);
canvas.drawPath(path, paint);
canvas.drawBitmap(bitMap, 500, 500, paint);
invalidate();
}
}
}
是否将位图设置为imageview时出错?这样做的正确方法是什么?
答案 0 :(得分:1)
错误在于
setContentView(new NewAnimation(this));
ImageView im = (ImageView)findViewById(R.id.imageView1);
您正在将自定义视图设置为内容视图,然后找到那里不存在的imageView1。所以它获得了空值。
设置
setContentView(R.layout.layout_which_contains_imageView1).
im.setImageBitmap((new NewAnimation(this)).bitMap); //ignore the static warning
在第一个构造函数中移动这三行
Bitmap.Config conf = Bitmap.Config.ARGB_8888;
bitMap = Bitmap.createBitmap(500, 1000, conf);
canvas = new Canvas(bitMap);
接下来在这三个
之下添加这一行draw(canvas);
此致
答案 1 :(得分:0)
您正在设置NPE,因为您正在设置
new NewAnimation(this);
在 setContentView()中,然后您将找到imageview ID。您必须已在某些xml中定义了imageview,因此请在setContentView()中设置该xml布局。
答案 2 :(得分:0)
这部分存在问题:
ImageView im = (ImageView)findViewById(R.id.imageView1);
由于你没有调用:setContentView(R.layout.activity_main);
,activity_main没有膨胀,findViewbyID返回null。
现在就这样做:
View base = getLayoutInflater().inflate(R.layout.activity_main,null);//I am assuming that imageView1 is in activity_main.xml file
ImageView im = (ImageView)base.findViewById(R.id.imageView1);
更新:
删除invalidate()
表单onDraw()
方法。它创建了递归循环。