Unable to instantiate activity caused by: java.lang.InstantiationException

Kristoffer Ryan Garces Supan picture Kristoffer Ryan Garces Supan · Sep 26, 2014 · Viewed 8.5k times · Source

@Bini this the error when i made your changes :(

09-26 11:06:05.620: D/dalvikvm(1613): Late-enabling CheckJNI
09-26 11:06:06.148: D/dalvikvm(1613): GC_FOR_ALLOC freed 51K, 4% free 3410K/3532K, paused 22ms, total 22ms
09-26 11:06:06.236: I/dalvikvm-heap(1613): Grow heap (frag case) to 17.093MB for 14369304-byte allocation
09-26 11:06:06.240: D/dalvikvm(1613): GC_FOR_ALLOC freed 2K, 1% free 17440K/17568K, paused 3ms, total 3ms
09-26 11:06:06.608: D/AndroidRuntime(1613): Shutting down VM
09-26 11:06:06.608: W/dalvikvm(1613): threadid=1: thread exiting with uncaught exception (group=0xa4d51b20)
09-26 11:06:06.608: E/AndroidRuntime(1613): FATAL EXCEPTION: main
09-26 11:06:06.608: E/AndroidRuntime(1613): Process: com.VaSuRa.sugboways, PID: 1613
09-26 11:06:06.608: E/AndroidRuntime(1613): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.VaSuRa.sugboways/com.VaSuRa.sugboways.MainActivity}: java.lang.ClassCastException: com.VaSuRa.sugboways.MainActivity cannot be cast to android.location.LocationListener
09-26 11:06:06.608: E/AndroidRuntime(1613):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
09-26 11:06:06.608: E/AndroidRuntime(1613):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
09-26 11:06:06.608: E/AndroidRuntime(1613):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
09-26 11:06:06.608: E/AndroidRuntime(1613):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
09-26 11:06:06.608: E/AndroidRuntime(1613):     at android.os.Handler.dispatchMessage(Handler.java:102)
09-26 11:06:06.608: E/AndroidRuntime(1613):     at android.os.Looper.loop(Looper.java:136)
09-26 11:06:06.608: E/AndroidRuntime(1613):     at android.app.ActivityThread.main(ActivityThread.java:5017)
09-26 11:06:06.608: E/AndroidRuntime(1613):     at java.lang.reflect.Method.invokeNative(Native Method)
09-26 11:06:06.608: E/AndroidRuntime(1613):     at java.lang.reflect.Method.invoke(Method.java:515)
09-26 11:06:06.608: E/AndroidRuntime(1613):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
09-26 11:06:06.608: E/AndroidRuntime(1613):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
09-26 11:06:06.608: E/AndroidRuntime(1613):     at dalvik.system.NativeStart.main(Native Method)
09-26 11:06:06.608: E/AndroidRuntime(1613): Caused by: java.lang.ClassCastException: com.VaSuRa.sugboways.MainActivity cannot be cast to android.location.LocationListener
09-26 11:06:06.608: E/AndroidRuntime(1613):     at com.VaSuRa.sugboways.MainActivity.onCreate(MainActivity.java:44)
09-26 11:06:06.608: E/AndroidRuntime(1613):     at android.app.Activity.performCreate(Activity.java:5231)
09-26 11:06:06.608: E/AndroidRuntime(1613):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
09-26 11:06:06.608: E/AndroidRuntime(1613):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
09-26 11:06:06.608: E/AndroidRuntime(1613):     ... 11 more

This is the 2nd error

09-26 11:06:05.620: D/dalvikvm(1613): Late-enabling CheckJNI
09-26 11:06:06.148: D/dalvikvm(1613): GC_FOR_ALLOC freed 51K, 4% free 3410K/3532K, paused 22ms, total 22ms
09-26 11:06:06.236: I/dalvikvm-heap(1613): Grow heap (frag case) to 17.093MB for 14369304-byte allocation
09-26 11:06:06.240: D/dalvikvm(1613): GC_FOR_ALLOC freed 2K, 1% free 17440K/17568K, paused 3ms, total 3ms
09-26 11:06:06.608: D/AndroidRuntime(1613): Shutting down VM
09-26 11:06:06.608: W/dalvikvm(1613): threadid=1: thread exiting with uncaught exception (group=0xa4d51b20)
09-26 11:06:06.608: E/AndroidRuntime(1613): FATAL EXCEPTION: main
09-26 11:06:06.608: E/AndroidRuntime(1613): Process: com.VaSuRa.sugboways, PID: 1613
09-26 11:06:06.608: E/AndroidRuntime(1613): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.VaSuRa.sugboways/com.VaSuRa.sugboways.MainActivity}: java.lang.ClassCastException: com.VaSuRa.sugboways.MainActivity cannot be cast to android.location.LocationListener
09-26 11:06:06.608: E/AndroidRuntime(1613):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
09-26 11:06:06.608: E/AndroidRuntime(1613):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
09-26 11:06:06.608: E/AndroidRuntime(1613):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
09-26 11:06:06.608: E/AndroidRuntime(1613):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
09-26 11:06:06.608: E/AndroidRuntime(1613):     at android.os.Handler.dispatchMessage(Handler.java:102)
09-26 11:06:06.608: E/AndroidRuntime(1613):     at android.os.Looper.loop(Looper.java:136)
09-26 11:06:06.608: E/AndroidRuntime(1613):     at android.app.ActivityThread.main(ActivityThread.java:5017)
09-26 11:06:06.608: E/AndroidRuntime(1613):     at java.lang.reflect.Method.invokeNative(Native Method)
09-26 11:06:06.608: E/AndroidRuntime(1613):     at java.lang.reflect.Method.invoke(Method.java:515)
09-26 11:06:06.608: E/AndroidRuntime(1613):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
09-26 11:06:06.608: E/AndroidRuntime(1613):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
09-26 11:06:06.608: E/AndroidRuntime(1613):     at dalvik.system.NativeStart.main(Native Method)
09-26 11:06:06.608: E/AndroidRuntime(1613): Caused by: java.lang.ClassCastException: com.VaSuRa.sugboways.MainActivity cannot be cast to android.location.LocationListener
09-26 11:06:06.608: E/AndroidRuntime(1613):     at com.VaSuRa.sugboways.MainActivity.onCreate(MainActivity.java:44)
09-26 11:06:06.608: E/AndroidRuntime(1613):     at android.app.Activity.performCreate(Activity.java:5231)
09-26 11:06:06.608: E/AndroidRuntime(1613):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
09-26 11:06:06.608: E/AndroidRuntime(1613):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
09-26 11:06:06.608: E/AndroidRuntime(1613):     ... 11 more

This is the log that's giving me this headache, could someone please help me out? I need this so bad...

09-26 09:26:43.281: D/dalvikvm(1209): Late-enabling CheckJNI
09-26 09:26:43.441: D/dalvikvm(1209): newInstance failed: p0 i0 [0 a1
09-26 09:26:43.441: D/AndroidRuntime(1209): Shutting down VM
09-26 09:26:43.445: W/dalvikvm(1209): threadid=1: thread exiting with uncaught exception (group=0xa4da2b20)
09-26 09:26:43.457: E/AndroidRuntime(1209): FATAL EXCEPTION: main
09-26 09:26:43.457: E/AndroidRuntime(1209): Process: com.VaSuRa.sugboways, PID: 1209
09-26 09:26:43.457: E/AndroidRuntime(1209): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.VaSuRa.sugboways/com.VaSuRa.sugboways.MainActivity}: java.lang.InstantiationException: can't instantiate class com.VaSuRa.sugboways.MainActivity
09-26 09:26:43.457: E/AndroidRuntime(1209):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
09-26 09:26:43.457: E/AndroidRuntime(1209):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
09-26 09:26:43.457: E/AndroidRuntime(1209):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
09-26 09:26:43.457: E/AndroidRuntime(1209):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
09-26 09:26:43.457: E/AndroidRuntime(1209):     at android.os.Handler.dispatchMessage(Handler.java:102)
09-26 09:26:43.457: E/AndroidRuntime(1209):     at android.os.Looper.loop(Looper.java:136)
09-26 09:26:43.457: E/AndroidRuntime(1209):     at android.app.ActivityThread.main(ActivityThread.java:5017)
09-26 09:26:43.457: E/AndroidRuntime(1209):     at java.lang.reflect.Method.invokeNative(Native Method)
09-26 09:26:43.457: E/AndroidRuntime(1209):     at java.lang.reflect.Method.invoke(Method.java:515)
09-26 09:26:43.457: E/AndroidRuntime(1209):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
09-26 09:26:43.457: E/AndroidRuntime(1209):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
09-26 09:26:43.457: E/AndroidRuntime(1209):     at dalvik.system.NativeStart.main(Native Method)
09-26 09:26:43.457: E/AndroidRuntime(1209): Caused by: java.lang.InstantiationException: can't instantiate class com.VaSuRa.sugboways.MainActivity
09-26 09:26:43.457: E/AndroidRuntime(1209):     at java.lang.Class.newInstanceImpl(Native Method)
09-26 09:26:43.457: E/AndroidRuntime(1209):     at java.lang.Class.newInstance(Class.java:1208)
09-26 09:26:43.457: E/AndroidRuntime(1209):     at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
09-26 09:26:43.457: E/AndroidRuntime(1209):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112)
09-26 09:26:43.457: E/AndroidRuntime(1209):     ... 11 more

Here is my Main Activity:

package com.VaSuRa.sugboways;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.location.LocationListener;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;

public abstract class MainActivity extends Activity implements OnClickListener,
    LocationListener {

LocationManager locationManager;
String provider;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    Criteria criteria = new Criteria();

    provider = locationManager.getBestProvider(criteria, false);

    if (provider != null && !provider.equals("")) {

        // Get the location from the given provider
        Location location = locationManager.getLastKnownLocation(provider);

        locationManager.requestLocationUpdates(provider, 20000, 1,
                (android.location.LocationListener) this);

        if (location != null)
            onLocationChanged(location);
        else
            Toast.makeText(getBaseContext(), "Location can't be retrieved",
                    Toast.LENGTH_SHORT).show();

    } else {
        Toast.makeText(getBaseContext(), "No Provider Found",
                Toast.LENGTH_SHORT).show();
    }

    TextView txtSugbo = (TextView) findViewById(R.id.txtSugbo);
    Typeface typeFace1 = Typeface.createFromAsset(getAssets(),
            "fonts/HolyRavioliNF.ttf");
    txtSugbo.setTypeface(typeFace1);

    TextView txtWays = (TextView) findViewById(R.id.txtWays);
    Typeface typeFace2 = Typeface.createFromAsset(getAssets(),
            "fonts/Angella_demo.otf");
    txtWays.setTypeface(typeFace2);

    Button btnGetStarted = (Button) findViewById(R.id.btnGetStarted);
    btnGetStarted.setOnClickListener(this);

    GoogleMap googleMap;
    googleMap = ((MapFragment) getFragmentManager().findFragmentById(
            R.id.map)).getMap();
}

public void onClick(View v, Location location) {
    switch (v.getId()) {
    case R.id.btnGetStarted:

        double longitude = location.getLongitude();
        double latitude = location.getLatitude();

        Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
                Uri.parse("geo:0,0?q="+latitude+","+longitude+"- Current location."));
        startActivity(intent);
        break;
    }

}

@Override
public void onLocationChanged(Location location) {
    // TODO Auto-generated method stub

}
}

and here is my activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

<ImageView
    android:id="@+id/imgJeep"
    android:layout_width="300dp"
    android:layout_height="300dp"
    android:layout_gravity="center"
    android:layout_marginTop="30dp"
    android:contentDescription="@string/icon_desc"
    android:src="@drawable/jeep_new" />

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/txtSugbo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/sugbo"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="@color/Orange"
        android:textSize="50sp" />

</LinearLayout>

<TextView
    android:id="@+id/txtWays"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="@string/ways"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:textSize="30sp" />

<Button
    android:id="@+id/btnGetStarted"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginTop="40dp"
    android:text="@string/get_started"
    android:onClick="onClick" />

and my map fragment xml

package com.VaSuRa.sugboways;

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

public class MapFragmentActivity extends Activity {

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_map);

    Intent i = getIntent();
}
}

and lastly my activity_map.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" xmlns:tools="http://schemas.android.com/tools">

<fragment
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    tools:layout="@layout/activity_main" />

Answer

RobertoAllende picture RobertoAllende · Sep 26, 2014

In your MainActivity class definition you have:

public abstract class MainActivity extends Activity implements OnClickListener,

So, you are defining MainActivity as an abstract class but java documentation says:

Abstract classes cannot be instantiated, but they can be subclassed.

and since the error you are getting is precisely:

InstantiationException: can't instantiate class MainActivity

You have have to remove 'abstract' from MainActivity class definition.