无法实例化活动,原因是:java.lang.InstantiationException。

6
@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

这里是我的主活动(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

}
}

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


1
MainActivity的第44行是什么?出现了一个类转换异常:com.VaSuRa.sugboways.MainActivity无法转换为android.location.LocationListener。 - MysticMagicϡ
仅翻译,无空行:locationManager.requestLocationUpdates(provider, 20000, 1, (android.location.LocationListener) this); - Kristoffer Ryan Garces Supan
2个回答

20
在您的MainActivity类定义中,您有以下内容:
public abstract class MainActivity extends Activity implements OnClickListener,

因此,您将MainActivity定义为抽象类,但是Java文档(链接)指出:

抽象类不能被实例化,但可以作为子类。

由于您正在收到的错误信息正是:

InstantiationException: can't instantiate class MainActivity

您需要从MainActivity类定义中删除'abstract'关键字。

我又遇到了一个错误.. - Kristoffer Ryan Garces Supan
抱歉,我无法找到您提供的原始文本。请重新发送原始文本以便我可以为您翻译。谢谢! - Kristoffer Ryan Garces Supan

0

首先,您不能将MainActivity设置为抽象类,因为抽象类无法实例化。

第二个问题在您的代码行中,

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

你不能将你的MainActivity即活动分配给android.location.LocationListener。相反,使用

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

我该怎么做?它问我是否将参数转换为location.listener或实现到主活动中? - Kristoffer Ryan Garces Supan
@KristofferRyanGarcesSupan:然后在主活动中实现它的方法。让我知道它是否对你有用? - Beena
很遗憾,不行...又出现了错误 :(((( - Kristoffer Ryan Garces Supan
@Bini 的回答非常错误,你有没有检查 requestLocationUpdates() 方法的参数?它的最后一个参数应该是 LocationListner,你怎么能建议使用 MainActivity.this 呢? 请不要提供错误的答案。 - akash89

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接