在Android上扩展Abstract类时出现InstantiationException

时间:2012-04-07 15:53:31

标签: android exception

我有一个名为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游戏编程的新手,我不知道问题到底是什么,我已经搜索了问题并在这里看了一些类似的问题,但我没有得到答案。请帮帮我......

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

您将LoginScreen作为主类,因此您必须

扩展一些Activity类。

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

    class ABC extends Screen
    {
     // your code goes here...
    }
}