Android SurfaceView لا يمكن أن يعمل
-
29-09-2019 - |
سؤال
إنه سؤال سهل ، لكن لا يمكنني معرفة ذلك:
هذا هو XML الخاص بي:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android = "http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<GG.My_pic.testA
android:id = "@+id/myview"
android:layout_width = "fill_parent"
android:layout_height = "fill_parent"
/>
</FrameLayout>
في Lunar Lander ، يستخدم الخيط الرئيسي
// tell system to use the layout defined in our XML file
setContentView(R.layout.lunar_layout);
لكن لا يمكنني استخدام بلدي
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
إذا قمت بتغيير r.layout.main إلى -> Testa (هذا) ، فهو يعمل
(Testa هي الفئة التي تمتد على SurfaceView يطبق حامل السطح. callback)
لماذا؟؟
المحلول
اكتشفت السبب ، لكنني لا أعرف لماذا.
كوني مبتدئًا حقيقيًا في كل من Java و Android ، قضيت وقتًا طويلاً للغاية لمعرفة ذلك.
مفتاح هذه المشكلة هو
Class GameView يمتد SurfaceView ينفذ Surfaceholder.callback {
public gameView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
}
يمكنك أن ترى ، أن هذا هو الأساس لـ SurfaceView ، في كل برنامج تعليمي
مثل
http://android-er.blogspot.com/2010/05/android-surfaceview.html
http://www.droidnova.com/playing-with-graphics-in-droid-ii،160.html
هناك 3 أنواع من المنشئ في SurfaceView:
SurfaceView(Context context)
SurfaceView(Context context, AttributeSet attrs)
SurfaceView(Context context, AttributeSet attrs, int defStyle)
قضيت يومًا باستخدام الأول:
SurfaceView(Context context)
ويأتي دائمًا إلى "إغلاق القوة".
ولكن عندما التفت إلى المُنشئ الثاني:
SurfaceView(Context context, AttributeSet attrs)
يعمل فجأة!
هذا هو الحل.
يمكن لأحد أن يقول لي لماذا؟؟