2013-10-29 103 views
4

我正试图在Android上创建一个游戏,并且对视图的实例有问题。我正在使用充气的视图。java.lang.InstantiationException:不能实例化类:没有空的构造函数

这里是我的视图代码:

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描述),部分创建programmaticaly与类GameView.java 我该怎么做?

+1

'no empty constructor'表示您没有空的构造函数。 – njzk2

+1

VIew.inflate?什么是观点? – Blackbelt

回答

6

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

1

试试这个

@Override 
public void onCreate(Bundle savedInstanceState){ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.game); 
    this.view = (GameView) findViewById(R.id.gameView); 
} 

你必须要找到你的看法ID在你的类布局

请确保您有

public GameView(Context context) { 
    super(context); 
} 

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

public GameView(Context context, AttributeSet attrs, int defStyle) { 
    super(context, attrs, defStyle); 
} 

@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
    if (widthMeasureSpec > 3000) 
     widthMeasureSpec = widthMeasureSpec & 0xfff; 
    if (heightMeasureSpec > 3000) 
     heightMeasureSpec = heightMeasureSpec & 0xfff; 
    setMeasuredDimension(widthMeasureSpec, heightMeasureSpec); 
} 
+0

没有工作..但感谢您的建议! – Emarco

+0

我编辑了答案 – serge

+0

我给了XML文件中的tablelayout一个id,我做了你的建议,但应用程序仍然停止。 (但是无论如何,再次感谢!) – Emarco

2

您可以添加一个空的构造:

public GameView() { 
    super(); 
} 

但是你确定你不是说是做更多的东西一样:

setContentView(R.layout.game); 
this.view = (GameView) findViewById(R.id.gameView); 

手动充气的观点在活动中的创作似乎有点奇怪。

+0

如果我使用空的构造函数,我无法编译。“构造函数TableLayout.TableLayout(Context,AttributeSet)不适用”。第二种方式编译但应用程序仍然停止。我不得不说,因为我是初学者,我不确定什么是正常的,什么是奇怪的,所以如果是这样的话,我会尝试找到另一种方式来做我想做的事情。谢谢 ! – Emarco

2

我试图用一个抽象类来开始一个活动。

相关问题