我正在创建一个我添加到FrameLayout的自定义视图。 当我初始化视图时,我得到一个NullPointerException。 我究竟做错了什么。该视图的代码是:
public class DocumentCameraMask extends View {
private Context mContext;
public DocumentCameraMask(Context context) {
super(context);
this.mContext = context;
}
@Override
public void draw(Canvas canvas) {
// TODO Auto-generated method stub
Paint paint = new Paint();
paint.setColor(this.mContext.getResources().getColor(R.color.textColor));
paint.setStyle(Paint.Style.STROKE);
Rect cropRect = new Rect(0,0,800,600);
canvas.drawRect(cropRect, paint);
super.draw(canvas);
}
}
cropRect大小将动态计算,这就是我需要制作此视图的原因。 谢谢你的帮助。
logcat的
09-11 19:13:04.590:E / AndroidRuntime(4235):致命异乎寻常:主要 09-11 19:13:04.590:E / AndroidRuntime(4235): java.lang.RuntimeException:无法启动活动 DocumentCameraActivity}: java.lang.NullPointerException 09-11 19:13:04.590: E / AndroidRuntime(4235):at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184) 09-11 19:13:04.590:E / AndroidRuntime(4235):at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2211) 09-11 19:13:04.590:E / AndroidRuntime(4235):at android.app.ActivityThread.access $ 600(ActivityThread.java:149)09-11 19:13:04.590:E / AndroidRuntime(4235):at android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1300) 09-11 19:13:04.590:E / AndroidRuntime(4235):at android.os.Handler.dispatchMessage(Handler.java:99)09-11 19:13:04.590:E / AndroidRuntime(4235):at android.os.Looper.loop(Looper.java:153)09-11 19:13:04.590: E / AndroidRuntime(4235):at android.app.ActivityThread.main(ActivityThread.java:4987)09-11 19:13:04.590:E / AndroidRuntime(4235):at java.lang.reflect.Method.invokeNative(Native Method)09-11 19:13:04.590:E / AndroidRuntime(4235):at java.lang.reflect.Method.invoke(Method.java:511)09-11 19:13:04.590: E / AndroidRuntime(4235):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:821) 09-11 19:13:04.590:E / AndroidRuntime(4235):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)09-11 19:13:04.590:E / AndroidRuntime(4235):at dalvik.system.NativeStart.main(Native Method)09-11 19:13:04.590: E / AndroidRuntime(4235):引起:java.lang.NullPointerException 09-11 19:13:04.590:E / AndroidRuntime(4235):at android.view.ViewConfiguration.get(ViewConfiguration.java:332)09-11 19:13:04.590:E / AndroidRuntime(4235):at android.view.View。(View.java:3243)
答案 0 :(得分:5)
您不必覆盖draw()
的{{1}}方法,无论您想要做什么绘图,都可以使用View
方法。
onDraw()
<强> refernces:强>
1。 google link
2。另一个link这可能会对您有所帮助
<强>更新强>
我的主要活动:
public class DocumentCameraMask extends View {
private Context mContext;
public DocumentCameraMask(Context context) {
super(context);
this.mContext = context;
}
@Override
public void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
Paint paint = new Paint();
paint.setColor(this.mContext.getResources().getColor(R.color.textColor));
paint.setStyle(Paint.Style.STROKE);
Rect cropRect = new Rect(0,0,800,600);
canvas.drawRect(cropRect, paint);
}
}
自定义视图DocumentCameraMask类:
public class MainActivity extends Activity {
DocumentCameraMask mask;
RelativeLayout rel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rel = (RelativeLayout) findViewById(R.id.t);
mask = new DocumentCameraMask(this);
mask.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,RelativeLayout.LayoutParams.MATCH_PARENT));
byte b = 100;
int a = b;
Toast.makeText(this,"Int "+a,Toast.LENGTH_LONG).show();
rel.addView(mask);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
<强>输出:强>
答案 1 :(得分:1)
您需要添加另一个构造函数。特别是,如果您在XML文件中使用此View
,它将使用您不提供的其他构造函数。
View
的三个构造函数是:
View(Context context, AttributeSet attrs, int defStyle)
View(Context context, AttributeSet attrs)
View(Context context)
至少应该覆盖第二个,因为这是XML文件中最常见的。只需确保为每个人调用匹配的super()
。
注意:的
正如Gru所说,你真的应该覆盖onDraw()
,而不是draw()
。我不知道你的Eclipse告诉你什么,但我从来不需要覆盖draw()
,而且我从未建议过。
答案 2 :(得分:1)
您可以尝试以下操作:
public class DocumentCameraMask extends View {
private Context mContext;
/*
If you ever want to include custom-view manually in code without any layout attributes,use this constructor.
*/
public DocumentCameraMask(Context context) {
this(context,null);
}
/*
If you want to include the custom view in a layout XML file, you need to use this
constructor.
*/
public DocumentCameraMask(Context context,AttributeSet attrs) {
super(context,attrs);
this.mContext = context;
}
/*
If you need to actually do something with the view you have. To do this, you must override the onDraw method of your custom-View class.
*/
@Override
public void onDraw(Canvas canvas) {
/*
If you want to call the superclass onDraw method (eg.TextView rather than a generic View), then call super.onDraw().If you don't want that, i.e. you are planning to draw the entire View yourself ,there's no reason to call it. If you look at the source code, it shows that View.onDraw() is an empty method. So, calling super.onDraw(), if the parent class is View itself, does nothing.
*/
super.onDraw(canvas);
Paint paint = new Paint();
paint.setColor(mContext.getResources().getColor(R.color.textColor));
paint.setStyle(Paint.Style.STROKE);
Rect cropRect = new Rect(0,0,800,600);
canvas.drawRect(cropRect, paint);
}
}
您可以参考Custom Drawing
注意:根据文档,
- public void draw(Canvas canvas)
醇>在API级别1中添加
Manually render this view (and all of its children) to the given Canvas. The view must have already done a full layout before this function is called. When implementing a view, implement onDraw(android.graphics.Canvas) instead of overriding this method. If you do need to override this method, call the superclass version.
2. protected void onDraw (Canvas canvas)
在API级别1中添加
Implement this to do your drawing.