java.lang.InstantiationException:不能实例化类:没有空的构造函数(java.lang.InstantiationException: can't instan

编程入门 行业动态 更新时间:2024-10-28 20:28:57
java.lang.InstantiationException:不能实例化类:没有空的构造函数(java.lang.InstantiationException: can't instantiate class : no empty constructor)

我正在尝试在Android上创建游戏,但我对视图的实例有疑问。 我正在使用膨胀的视图。

这是我的视图代码:

public class GameView extends TableLayout { public GameView(Context context, AttributeSet attrs) { super(context, attrs); }

为活动

public class GameActivity extends Activity { private GameView view; @Override public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); this.view = (GameView) View.inflate(this,R.layout.game, null); }

这是错误

D/dalvikvm( 6176): newInstance failed: no <init>() D/AndroidRuntime( 6176): Shutting down VM W/dalvikvm( 6176): threadid=1: thread exiting with uncaught exception (group=0x41a80700) E/AndroidRuntime( 6176): FATAL EXCEPTION: main E/AndroidRuntime( 6176): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.android.homework.em.go/com.android.homework.em.go.GameView}: java.lang.InstantiationException: can't instantiate class com.android.homework.em.go.GameView; no empty constructor E/AndroidRuntime( 6176): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2137) E/AndroidRuntime( 6176): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261) E/AndroidRuntime( 6176): at android.app.ActivityThread.access$600(ActivityThread.java:141) E/AndroidRuntime( 6176): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256) E/AndroidRuntime( 6176): at android.os.Handler.dispatchMessage(Handler.java:99) E/AndroidRuntime( 6176): at android.os.Looper.loop(Looper.java:137) E/AndroidRuntime( 6176): at android.app.ActivityThread.main(ActivityThread.java:5103) E/AndroidRuntime( 6176): at java.lang.reflect.Method.invokeNative(Native Method) E/AndroidRuntime( 6176): at java.lang.reflect.Method.invoke(Method.java:525) E/AndroidRuntime( 6176): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) E/AndroidRuntime( 6176): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) E/AndroidRuntime( 6176): at dalvik.system.NativeStart.main(Native Method) E/AndroidRuntime( 6176): Caused by: java.lang.InstantiationException: can't instantiate class com.android.homework.em.go.GameView; no empty constructor E/AndroidRuntime( 6176): at java.lang.Class.newInstanceImpl(Native Method) E/AndroidRuntime( 6176): at java.lang.Class.newInstance(Class.java:1130) E/AndroidRuntime( 6176): at android.app.Instrumentation.newActivity(Instrumentation.java:1061) E/AndroidRuntime( 6176): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2128) E/AndroidRuntime( 6176): ... 11 more W/ActivityManager( 436): Force finishing activity com.android.homework.em.go/.GameView

谢谢你的帮助 !

编辑:

1)问题是,当我创建一个空的构造函数时,我无法编译,因为它说构造函数与TableLayout的构造函数不兼容。

2)我使用View.inflate,因为我发现它在这Tutoriel: http : //www.therealjoshua.com/2012/07/android-architecture-part-10-the-activity-revisited/我想要的是为我的活动设置一个视图,该视图部分由xml文件(layout.game)描述,部分由类GameView.java创建programmaticaly。我该怎么做?

I'm trying to create a game on Android and I have a problem with the instanciation of the view. I'm using an inflated view.

Here is my code for the view :

public class GameView extends TableLayout { public GameView(Context context, AttributeSet attrs) { super(context, attrs); }

for the activity

public class GameActivity extends Activity { private GameView view; @Override public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); this.view = (GameView) View.inflate(this,R.layout.game, null); }

and here is the error

D/dalvikvm( 6176): newInstance failed: no <init>() D/AndroidRuntime( 6176): Shutting down VM W/dalvikvm( 6176): threadid=1: thread exiting with uncaught exception (group=0x41a80700) E/AndroidRuntime( 6176): FATAL EXCEPTION: main E/AndroidRuntime( 6176): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.android.homework.em.go/com.android.homework.em.go.GameView}: java.lang.InstantiationException: can't instantiate class com.android.homework.em.go.GameView; no empty constructor E/AndroidRuntime( 6176): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2137) E/AndroidRuntime( 6176): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261) E/AndroidRuntime( 6176): at android.app.ActivityThread.access$600(ActivityThread.java:141) E/AndroidRuntime( 6176): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256) E/AndroidRuntime( 6176): at android.os.Handler.dispatchMessage(Handler.java:99) E/AndroidRuntime( 6176): at android.os.Looper.loop(Looper.java:137) E/AndroidRuntime( 6176): at android.app.ActivityThread.main(ActivityThread.java:5103) E/AndroidRuntime( 6176): at java.lang.reflect.Method.invokeNative(Native Method) E/AndroidRuntime( 6176): at java.lang.reflect.Method.invoke(Method.java:525) E/AndroidRuntime( 6176): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) E/AndroidRuntime( 6176): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) E/AndroidRuntime( 6176): at dalvik.system.NativeStart.main(Native Method) E/AndroidRuntime( 6176): Caused by: java.lang.InstantiationException: can't instantiate class com.android.homework.em.go.GameView; no empty constructor E/AndroidRuntime( 6176): at java.lang.Class.newInstanceImpl(Native Method) E/AndroidRuntime( 6176): at java.lang.Class.newInstance(Class.java:1130) E/AndroidRuntime( 6176): at android.app.Instrumentation.newActivity(Instrumentation.java:1061) E/AndroidRuntime( 6176): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2128) E/AndroidRuntime( 6176): ... 11 more W/ActivityManager( 436): Force finishing activity com.android.homework.em.go/.GameView

Thanks for your help !

EDIT :

1) The problem is that when I create an empty constructor, I can't compile because it says that the constructor is not compatible with the constructor of TableLayout.

2) I'm using View.inflate because I found it on this tutoriel : http://www.therealjoshua.com/2012/07/android-architecture-part-10-the-activity-revisited/ What I want is to set a view to my activity which is partly describe by an xml file (layout.game) and partly created programmaticaly with the class GameView.java How can I do that ?

最满意答案

事实上,问题是因为我没有在清单文件中调用正确的活动。 我称GameView而不是GameActivity。

In fact the problem was because I wasn't calling the correct activity in the manifest file. I called GameView instead of GameActivity.

更多推荐

本文发布于:2023-08-07 08:49:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1463350.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   实例   lang   InstantiationException   java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!