Android:Crash:二进制XML文件行:膨胀类时出错(使用SurfaceView)
我正在使用android surfaceView,我正在尝试为它添加按钮。 在surfaceView画布上我画了一些东西。我有一个线程类来保持绘图Android:Crash:二进制XML文件行:膨胀类时出错(使用SurfaceView),android,graphics,surfaceview,Android,Graphics,Surfaceview,我正在使用android surfaceView,我正在尝试为它添加按钮。 在surfaceView画布上我画了一些东西。我有一个线程类来保持绘图 package com.androidsurfaceview; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; public class androidsurfaceview
package com.androidsurfaceview;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class androidsurfaceview extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button buttonShowHide = (Button)findViewById(R.id.showhide);
final Button buttonDummy = (Button)findViewById(R.id.dummy);
buttonShowHide.setOnClickListener(
new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(buttonDummy.getVisibility()==View.VISIBLE){
buttonDummy.setVisibility(View.GONE);
}
else{
buttonDummy.setVisibility(View.VISIBLE);
}
}
}
);
线程类
package com.androidsurfaceview;
import android.graphics.Canvas;
import android.view.SurfaceHolder;
public class MySurfaceThread extends Thread {
private SurfaceHolder myThreadSurfaceHolder;
private com.androidsurfaceview.test.MySurfaceView myThreadSurfaceView;
private boolean myThreadRun = false;
public MySurfaceThread(SurfaceHolder surfaceHolder, com.androidsurfaceview.test.MySurfaceView surfaceView) {
myThreadSurfaceHolder = surfaceHolder;
myThreadSurfaceView = surfaceView;
}
public void setRunning(boolean b) {
myThreadRun = b;
}
@Override
public void run() {
// TODO Auto-generated method stub
while(myThreadRun){
Canvas c = null;
try{
c = myThreadSurfaceHolder.lockCanvas(null);
synchronized (myThreadSurfaceHolder){
myThreadSurfaceView.onDraw(c);
}
sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
// 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 (c != null) {
myThreadSurfaceHolder.unlockCanvasAndPost(c);
}
}
}
}
}
surfaceView&绘图类
package com.androidsurfaceview;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class test extends Activity{
// ......
// I do a few things here... with this class test.
public class MySurfaceView extends SurfaceView implements SurfaceHolder.Callback{
private MySurfaceThread thread;
private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
int cx, cy, offx, offy;
public MySurfaceView(Context context) {
super(context);
// TODO Auto-generated constructor stub
init();
}
public MySurfaceView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
init();
}
public MySurfaceView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
init();
}
private void init(){
getHolder().addCallback(this);
thread = new MySurfaceThread(getHolder(), this);
setFocusable(true); // make sure we get key events
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(3);
paint.setColor(Color.WHITE);
cx = 0;
cy = 0;
offx = 10;
offy = 10;
}
@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
thread.setRunning(true);
thread.start();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
boolean retry = true;
thread.setRunning(false);
while (retry) {
try {
thread.join();
retry = false;
}
catch (InterruptedException e) {
}
}
}
////Just a simple graphic of moving circle.
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
canvas.drawRGB(0, 0, 0);
canvas.drawCircle(cx, cy, 3, paint);
cx += offx;
if (cx > getWidth() || (cx < 0)){
offx *= -1;
cx += offx;
}
cy += offy;
if (cy > getHeight() || (cy < 0)){
offy *= -1;
cy += offy;
}
}
}
}
任何帮助都将是巨大的…我对此束手无策。首先,您必须将您的视图声明为
静态类型,以便能够在没有holding类实例可用时对其进行膨胀:
public static class MySurfaceView extends SurfaceView
implements SurfaceHolder.Callback
线路
<com.androidsurfaceview.test.MySurfaceView
因此,必须在布局xml中指定视图,如下所示:
<view class="com.androidsurfaceview.test$MySurfaceView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
这样就可以了。我认为如果我将“MySurfaceView”作为嵌套/内部类,则无法找到该类。任何想法!你真的在那个包里有那个类吗?因为您一直按照包命名约定命名类,而在共享核心中根本没有包声明!如果您使用的是默认软件包,请尝试添加。更新了包裹的详细信息。非常感谢!!!成功了。最初我还尝试将类设置为静态。但是我不知道我必须给它一个视图类&$。你推荐我可以学习的教程或书籍吗?通过阅读,我学到了很多,但是有很多教程和示例。我最喜欢的是@Renaun Ericksson的技巧和@Commonware的示例。即使使用正确的XML,我也会遇到同样的错误,我是否应该打开另一张罚单?这与您所说的完全相同,但经过更正,请注意,包声明中的测试是保存MySurfaceView静态类的类的名称。这可能会让人困惑,因为它是以小写开头的。谢谢,在我的内部类中添加关键字static解决了我的问题
<com.androidsurfaceview.test.MySurfaceView
<com.androidsurfaceview.test$MySurfaceView
<view class="com.androidsurfaceview.test$MySurfaceView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>