无法实例化由以下原因引起的活动:java.lang.InstantiationException

时间:2014-09-26 01:55:39

标签: android oncreate

@Bini我做出改变时的错误:(

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

这是第二个错误

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

这是让我头疼的日志,有人可以帮帮我吗?我需要这么糟糕......

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

这是我的主要活动:

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

}
}

这是我的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" />

和我的地图片段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();
}
}

最后是我的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" />

2 个答案:

答案 0 :(得分:14)

在您的MainActivity类定义中,您有:

public abstract class MainActivity extends Activity implements OnClickListener,

因此,您将MainActivity定义为抽象类,但java documentation says

  

抽象类无法实例化,但它们可以是子类。

因为你得到的错误恰恰是:

InstantiationException: can't instantiate class MainActivity

您必须从MainActivity类定义中删除“abstract”。

答案 1 :(得分:0)

第一个问题是您无法将MainActivity设置为Abstract类,因为无法实例化抽象类。

第二期是你的行,

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

您无法将您的MainActivity即活动分配给android.location.LocationListener。而是使用

  locationManager.requestLocationUpdates(provider, 20000, 1,
            MainActivity.this);