0

我有一个名为 Screen 的抽象类以这种方式实现....

public abstract class Screen {
    protected final Game game;

    public Screen(Game game) {
    this.game = game;
    }

    public abstract void update(float deltaTime);
    public abstract void present(float deltaTime);
    public abstract void pause();
    public abstract void resume();
    public abstract void dispose();
}

和一个扩展 Screen 类的类

public class LoadingScreen extends Screen {

    public LoadingScreen(Game game) {
        super(game);
    }


    @Override
    public void update(float deltaTime) {


    }

    @Override
    public void present(float deltaTime) {
        // TODO Auto-generated method stub

    }

    @Override
    public void pause() {
        // TODO Auto-generated method stub

    }

    @Override
    public void resume() {
        // TODO Auto-generated method stub

    }

    @Override
    public void dispose() {
        // TODO Auto-generated method stub

    }

}

但是当我尝试执行该项目时,我得到了以下日志:

04-07 03:01:09.603: E/AndroidRuntime(1107): FATAL EXCEPTION: main
04-07 03:01:09.603: E/AndroidRuntime(1107): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.mobi.classic.snake/com.mobi.classic.snake.LoadingScreen}: java.lang.InstantiationException: com.mobi.classic.snake.LoadingScreen
04-07 03:01:09.603: E/AndroidRuntime(1107):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
04-07 03:01:09.603: E/AndroidRuntime(1107):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
04-07 03:01:09.603: E/AndroidRuntime(1107):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
04-07 03:01:09.603: E/AndroidRuntime(1107):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
04-07 03:01:09.603: E/AndroidRuntime(1107):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-07 03:01:09.603: E/AndroidRuntime(1107):     at android.os.Looper.loop(Looper.java:123)
04-07 03:01:09.603: E/AndroidRuntime(1107):     at android.app.ActivityThread.main(ActivityThread.java:4627)
04-07 03:01:09.603: E/AndroidRuntime(1107):     at java.lang.reflect.Method.invokeNative(Native Method)
04-07 03:01:09.603: E/AndroidRuntime(1107):     at java.lang.reflect.Method.invoke(Method.java:521)
04-07 03:01:09.603: E/AndroidRuntime(1107):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-07 03:01:09.603: E/AndroidRuntime(1107):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-07 03:01:09.603: E/AndroidRuntime(1107):     at dalvik.system.NativeStart.main(Native Method)
04-07 03:01:09.603: E/AndroidRuntime(1107): Caused by: java.lang.InstantiationException: com.mobi.classic.snake.LoadingScreen
04-07 03:01:09.603: E/AndroidRuntime(1107):     at java.lang.Class.newInstanceImpl(Native Method)
04-07 03:01:09.603: E/AndroidRuntime(1107):     at java.lang.Class.newInstance(Class.java:1429)
04-07 03:01:09.603: E/AndroidRuntime(1107):     at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
04-07 03:01:09.603: E/AndroidRuntime(1107):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
04-07 03:01:09.603: E/AndroidRuntime(1107):     ... 11 more

我是 Android 游戏编程的新手,我不知道问题是什么,我已经用谷歌搜索了这个问题并在这里查看了一些类似的问题,但我没有得到答案。请帮帮我...

4

2 回答 2

1

似乎您正在尝试将该LoadingScreen类用作 Activity,但事实并非如此。您只能使用活动(或从扩展Activity的类)。查看hello world 示例

于 2012-04-07T16:01:14.867 回答
1

你有 LoginScreen 作为主类,所以你必须需要

扩展一些 Activity 类。

class LoginScreen extends Activity
{
   onCreate(....)
   {
    new ABC();
   super(..);
   }

    class ABC extends Screen
    {
     // your code goes here...
    }
}
于 2012-04-07T21:23:08.207 回答