Java.lang.InstantiationException: unable to instantiate class: no empty constructor

I am trying to create a game on Android and I have a problem creating a view. I use a bloated look.

Here is my code for the presentation:

public class GameView extends TableLayout {

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

for 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 mistake

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

Thank you for your help!

EDIT:

1) The problem is that when I create an empty constructor, I cannot compile it because it says that the constructor is incompatible with the TableLayout constructor.

2) View.inflate, : http://www.therealjoshua.com/2012/07/android-architecture-part-10-the-activity-revisited/ , XML (layout.game) GameView.java ?

+4
4

, . GameView GameActivity.

+6

:

public GameView() {
    super();
}

, - :

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

.

+2

.

+2
source

try it

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

You need to find id for your view on the layout.

in your class make sure you have

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);
}
+1
source

All Articles