Question

I am new to android, I am trying to make game in android. In that game I have to draw canvas, I followed a tutorial to learn "How to draw canvas in android". In that tutorial he is using two classes named as GFX and Main, he is not showing any XML file, so I am using its default XML which is populated by eclipse editor automatically.

I have followed the tutorial, but when I try to run that application on my device a message appears on my device screen, Unfortunately,Seven has stopped. "Seven" is my application name. "blackball" is name of a image which I want to display on screen. I have saved this image in my res/drawable-hdpi folder. Following is the code, I am adding both classes, default XML file and Logcat. GFX File.

package com.example.seven;

import android.app.Activity;
import android.os.Bundle;

public class GFX extends Activity
{
 Main objec;
 protected void onCreate(Bundle savedInstanceState)
 {
     super.onCreate(savedInstanceState);
     objec=new Main(this);
     setContentView(objec);
 }
}

Main

package com.example.seven;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.view.Menu;
import android.view.View;
public class Main extends View
{
    Bitmap blackdot;
    public Main(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
        blackdot= BitmapFactory.decodeResource(getResources(), R.drawable.blackball);
    }
    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        super.onDraw(canvas);
        canvas.drawColor(Color.YELLOW);
        canvas.drawBitmap(blackdot, (canvas.getWidth())/2, 0, null);

    }
    }

XML File

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context=".Main" >

        <TextView
            android:layout_weight="1"
            android:layout_width="wrap_content"
            android:layout_height="0dip"
            android:text="@string/hello_world" />

    </RelativeLayout>

Below is the Logcat output

03-11 12:26:13.968: D/dalvikvm(8248): newInstance failed: no <init>()
03-11 12:26:13.968: D/AndroidRuntime(8248): Shutting down VM
03-11 12:26:13.968: W/dalvikvm(8248): threadid=1: thread exiting with uncaught exception (group=0x40eca258)
03-11 12:26:13.970: E/AndroidRuntime(8248): FATAL EXCEPTION: main
03-11 12:26:13.970: E/AndroidRuntime(8248): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.seven/com.example.seven.Main}: java.lang.InstantiationException: can't instantiate class com.example.seven.Main; no empty constructor
03-11 12:26:13.970: E/AndroidRuntime(8248):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2001)
03-11 12:26:13.970: E/AndroidRuntime(8248):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2104)
03-11 12:26:13.970: E/AndroidRuntime(8248):     at android.app.ActivityThread.access$600(ActivityThread.java:134)
03-11 12:26:13.970: E/AndroidRuntime(8248):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1247)
03-11 12:26:13.970: E/AndroidRuntime(8248):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-11 12:26:13.970: E/AndroidRuntime(8248):     at android.os.Looper.loop(Looper.java:154)
03-11 12:26:13.970: E/AndroidRuntime(8248):     at android.app.ActivityThread.main(ActivityThread.java:4624)
03-11 12:26:13.970: E/AndroidRuntime(8248):     at java.lang.reflect.Method.invokeNative(Native Method)
03-11 12:26:13.970: E/AndroidRuntime(8248):     at java.lang.reflect.Method.invoke(Method.java:511)
03-11 12:26:13.970: E/AndroidRuntime(8248):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
03-11 12:26:13.970: E/AndroidRuntime(8248):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
03-11 12:26:13.970: E/AndroidRuntime(8248):     at dalvik.system.NativeStart.main(Native Method)
03-11 12:26:13.970: E/AndroidRuntime(8248): Caused by: java.lang.InstantiationException: can't instantiate class com.example.seven.Main; no empty constructor
03-11 12:26:13.970: E/AndroidRuntime(8248):     at java.lang.Class.newInstanceImpl(Native Method)
03-11 12:26:13.970: E/AndroidRuntime(8248):     at java.lang.Class.newInstance(Class.java:1319)
03-11 12:26:13.970: E/AndroidRuntime(8248):     at android.app.Instrumentation.newActivity(Instrumentation.java:1023)
03-11 12:26:13.970: E/AndroidRuntime(8248):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1992)
03-11 12:26:13.970: E/AndroidRuntime(8248):     ... 11 more

Here is manifest file.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.seven"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.seven.GFX"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.GFX" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
Was it helpful?

Solution

Caused by: java.lang.InstantiationException: can't instantiate class com.example.seven.Main; no empty constructor

This exception with the hint in stacktrace that the framework was trying to instantiate your activity explains the issue: You have listed your Main class as an activity in the manifest. The class is not an Activity, though the instantiation fails earlier to missing empty constructor (since you've overridden the constructor) and not later when the created object was cast to an Activity.

Seems like GFX is your main activity.

To fix it, change Main activity from manifest to GFX.

In your edited question, keep the action as MAIN here:

<action android:name="android.intent.action.GFX" />

Change to

<action android:name="android.intent.action.MAIN" />

It's there to specify the main entry point of your app and the name has nothing to do with your code.

OTHER TIPS

You are getting the error in your CustomView's Constructor class ,resolve it like this

 private static Context mContext;
     public Main(Context context) {
         this.mContext = context;
        // TODO Auto-generated constructor stub
        blackdot= BitmapFactory.decodeResource(getResources(), R.drawable.blackball);
    }

You have overridden the default empty constructor. Actually you don't need to pass the context explicitly here. Activity itself is a subclass of context. And you can always use getApplicationContext() in an activity. So change your the constructor to pulic Main() would help you resolve the issue. EDIT: Just realized that its a view you're using. So to get context for a view, You can call getContext().

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top