如何调用库psented我们的活动surfaceview $ P $中定义的方法? [英] How to call the methods defined in the surfaceview presented in library to our activity?
问题描述
我写在其中surfaceview也在库本身定义的库。但是当我试图调用surfaceview我得到异常定义的方法。请帮我asolution
这是code为surfaceview
I am writing a library in which surfaceview is also defined in the library itself. but when i tried to call the methods defined in surfaceview i am getting exception. Please help me with asolution this is the code for surfaceview
public class AndroidGrapics extends SurfaceView implements SurfaceHolder.Callback{
public SurfaceHolder surfaceHolder = null;
public DrawingBoard(Context context) {
super(context);
//Retrieve the SurfaceHolder instance associated with this SurfaceView.
surfaceHolder = getHolder();
//Specify this class (DrawingBoard) as the class that implements the three callback methods required by SurfaceHolder.Callback.
surfaceHolder.addCallback(this);
}
//SurfaceHolder.Callback callback method.
@Override
public void surfaceCreated(SurfaceHolder holder) {
//Create and start a drawing thread whose Runnable object is defined by this class (DrawingBoard).
// new Thread(this).start();
}
public Canvas getCanvas()
{
return surfaceHolder.lockCanvas();
}
public void render(Canvas canvas,Paint paint) {
// canvas = surfaceHolder.lockCanvas();
//Fill the entire canvas' bitmap with 'black'.
canvas.drawColor(Color.BLACK);
//Instantiate a Paint object.
Paint paint1 = paint;
//Set the paint color to 'white'.
paint1.setColor(Color.WHITE);
//Draw a white circle at position (100, 100) with a radius of 50.
canvas.drawCircle(100, 100, 50, paint1);
}
//Neither of these two methods are used in this example, however, their definitions are required because SurfaceHolder.Callback was implemented.
@Override
public void surfaceChanged(SurfaceHolder sh, int f, int w, int h) {}
@Override
public void surfaceDestroyed(SurfaceHolder sh) {}
}
和这是在活动
public class Activity7s extends Activity implements Runnable{
DrawingBoard draw;
Paint paint=new Paint();
Canvas canvas;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
draw= new DrawingBoard(this);
System.out.println("_______________________________"+draw.surfaceHolder);
//Set the content view to a new instance of DrawingBoard.
//canvas = draw.getCanvas();
System.out.println("--------------------------"+canvas);
new Thread(this).start();
setContentView(draw);
}
@Override
public void run() {
canvas = null;
while(true)
{
if(!surface.getSurface().isValid())
{
continue;
}
try {
canvas = draw.getCanvas();
System.out.println("-bbbbbbbbbbbbbbbbbbbbbbbbb-"+draw.getCanvas());
synchronized (draw.surfaceHolder) {
draw.render(draw.surfaceHolder.lockCanvas(),paint);
}
} finally {
System.out.println("--------------------------"+canvas);
// do this in a finally so that if an exception is thrown
// during the above, we don't leave the Surface in an
// inconsistent state
if (canvas != null) {
draw.surfaceHolder.unlockCanvasAndPost(draw.surfaceHolder.lockCanvas());
}
}
}
}
}
和logcat的误差
04-05 17:57:44.480: W/dalvikvm(3817): threadid=11: thread exiting with uncaught exception (group=0x40a71930)
17 04-05:57:44.490:E / AndroidRuntime(3817):致命异常:螺纹153
04-05 17:57:44.490:E / AndroidRuntime(3817):显示java.lang.NullPointerException
04-05 17:57:44.490:E / AndroidRuntime(3817):在com.example.sexample.AndroidGrapics.render(AndroidGrapics.java:39)
04-05 17:57:44.490:E / AndroidRuntime(3817):在com.example.sexample.Activity7s.run(Activity7s.java:35)
04-05 17:57:44.490:E / AndroidRuntime(3817):在java.lang.Thread.run(Thread.java:856)
04-05 17:57:44.490: E/AndroidRuntime(3817): FATAL EXCEPTION: Thread-153 04-05 17:57:44.490: E/AndroidRuntime(3817): java.lang.NullPointerException 04-05 17:57:44.490: E/AndroidRuntime(3817): at com.example.sexample.AndroidGrapics.render(AndroidGrapics.java:39) 04-05 17:57:44.490: E/AndroidRuntime(3817): at com.example.sexample.Activity7s.run(Activity7s.java:35) 04-05 17:57:44.490: E/AndroidRuntime(3817): at java.lang.Thread.run(Thread.java:856)
推荐答案
终于找到了解决办法。
我只是添加下面的run方法行。
I'm just added following lines in the run method.
if(!sHolder.getSurface().isValid())
{
continue;
}
现在它的工作的罚款。
now its working fine.
更新code中的问题。
updated the code in the question.
这篇关于如何调用库psented我们的活动surfaceview $ P $中定义的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!