android xml嵌套解析,android - 在xml中链接嵌套的SurfaceView类 - 堆栈内存溢出
我在扩展SurfaceView的类中有一个类,如下所示:MainActivity.javapublic class MainActivity extends Activity implements OnTouchListener {SurfaceTest mySurface;@Overrideprotected void onCreate(Bundle savedInstanceState) {s
我在扩展SurfaceView的类中有一个类,如下所示:
MainActivity.java
public class MainActivity extends Activity implements OnTouchListener {
SurfaceTest mySurface;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.surface);
mySurface = (SurfaceTest) findViewById(R.layout.surface);
mySurface.setOnTouchListener(this);
}
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
// TODO Auto-generated method stub
// ... some code ...
return true;
}
public class SurfaceTest extends SurfaceView implements Runnable {
SurfaceHolder myHolder;
public SurfaceTest(Context context) {
super(context);
// TODO Auto-generated constructor stub
myHolder = getHolder();
}
public SurfaceTest(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
myHolder = getHolder();
}
public SurfaceTest(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
myHolder = getHolder();
}
@Override
public void run() {
// TODO Auto-generated method stub
while (true) {
if (!myHolder.getSurface().isValid()) {
continue;
}
Canvas canvas = myHolder.lockCanvas();
//...some code...
myHolder.unlockCanvasAndPost(canvas);
}
}
}
}
surface.xml
android:layout_width="match_parent"
android:layout_height="match_parent" >
android:id="@+id/surfaceView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
我在此xml文件的图形布局中出现错误,说
"The following classes could not be instantiated:
- com.example.testsurfaceview.MainActivity.SurfaceTest"
对我SurfaceTest一种可能的解决方案是为SurfaceTest类创建新类,然后在xml文件中使用com.example.testsurfaceview.SurfaceTest 。
但是问题又来了,我使用了来自onTouch事件的大量信息, onTouch事件用于在run()方法中在画布上进行绘制。 这就是我在MainActivity实现SurfaceTest类的原因
所以我现在有两个选择:
1)使xml适用于嵌套的SurfaceTest类2)创建新的SurfaceTest类,并使用Intent传递在onTouch获得的所有信息。
你有什么建议?
更多推荐
所有评论(0)