user1473912
user1473912

Reputation: 89

java.lang.RuntimeException: Unable to instantiate activity ComponentInfo: java.lang.InstantiationException

I have tried to create a rectangle using below program. But its throwing the run time exception. Emulator is opening but closing unexpectedly. In my project, I have two classes -Map.java and StartDraw.java

Below is the code for Map.java:

 package com.mapping;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;

public class Map extends View {
Paint paint = new Paint();

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

@Override
public void onDraw(Canvas canvas) {
    paint.setColor(Color.BLACK);
    paint.setStrokeWidth(3);
    canvas.drawRect(30, 30, 80, 80, paint);
    paint.setStrokeWidth(0);
    paint.setColor(Color.CYAN);
    canvas.drawRect(33, 60, 77, 77, paint );
    paint.setColor(Color.YELLOW);
    canvas.drawRect(33, 33, 77, 60, paint );

}

Below is the code for StartDraw.java

package com.mapping;

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

public class StartDraw extends Activity {
Map drawView;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    drawView = new Map(this);
    drawView.setBackgroundColor(Color.WHITE);
    setContentView(drawView);

}
}

Below is the manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mapping"
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.mapping.Map"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

</manifest>

Please help me out to resolve this exception. I doubt that it may be because of constructor, but couldn't get what to modify and how to modify.

Below is the logcat

04-03 11:23:56.611: D/dalvikvm(333): newInstance failed: no <init>()
04-03 11:23:56.632: D/AndroidRuntime(333): Shutting down VM
04-03 11:23:56.632: W/dalvikvm(333): threadid=1: thread exiting with uncaught exception (group=0x40015560)
04-03 11:23:56.701: E/AndroidRuntime(333): FATAL EXCEPTION: main
04-03 11:23:56.701: E/AndroidRuntime(333): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.mapping/com.mapping.Map}: java.lang.InstantiationException: com.mapping.Map
04-03 11:23:56.701: E/AndroidRuntime(333):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
04-03 11:23:56.701: E/AndroidRuntime(333):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
04-03 11:23:56.701: E/AndroidRuntime(333):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
04-03 11:23:56.701: E/AndroidRuntime(333):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
04-03 11:23:56.701: E/AndroidRuntime(333):  at android.os.Handler.dispatchMessage(Handler.java:99)
04-03 11:23:56.701: E/AndroidRuntime(333):  at android.os.Looper.loop(Looper.java:123)
04-03 11:23:56.701: E/AndroidRuntime(333):  at android.app.ActivityThread.main(ActivityThread.java:3683)
04-03 11:23:56.701: E/AndroidRuntime(333):  at java.lang.reflect.Method.invokeNative(Native Method)
04-03 11:23:56.701: E/AndroidRuntime(333):  at java.lang.reflect.Method.invoke(Method.java:507)
04-03 11:23:56.701: E/AndroidRuntime(333):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-03 11:23:56.701: E/AndroidRuntime(333):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-03 11:23:56.701: E/AndroidRuntime(333):  at dalvik.system.NativeStart.main(Native Method)
04-03 11:23:56.701: E/AndroidRuntime(333): Caused by: java.lang.InstantiationException: com.mapping.Map
04-03 11:23:56.701: E/AndroidRuntime(333):  at java.lang.Class.newInstanceImpl(Native Method)
04-03 11:23:56.701: E/AndroidRuntime(333):  at java.lang.Class.newInstance(Class.java:1409)
04-03 11:23:56.701: E/AndroidRuntime(333):  at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
04-03 11:23:56.701: E/AndroidRuntime(333):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
04-03 11:23:56.701: E/AndroidRuntime(333):  ... 11 more
04-03 11:24:04.161: I/Process(333): Sending signal. PID: 333 SIG: 9

Upvotes: 1

Views: 1417

Answers (3)

shakirullah orakzai
shakirullah orakzai

Reputation: 356

just change in androidmanifest.xml instead of android:name="com.mapping.Map"

<activity android:name=".startdraw" android:label="@string/app_name" android:launchMode="singleTask">

Upvotes: 0

ρяσѕρєя K
ρяσѕρєя K

Reputation: 132982

Register StartDraw instead of Map in AndroidManifest.xml as default Activity because you are extending Activity class in StartDraw . so just change default Activity name to StartDraw in Manifest:

<activity
        android:name=".StartDraw" <!--here use StartDraw instead Map  -->
        android:label="@string/app_name" >
<!-- other attributes here -->
</activity>

Upvotes: 0

Pratik
Pratik

Reputation: 30855

You are giving custom view name in <activity> as Map you need to set name "StartDraw" for activity name not view name

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mapping"
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=".StartDraw"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

</manifest>

Upvotes: 1

Related Questions