无法实例化服务service.FetchAddressIntentService:java.lang.IllegalAccessException:不允许访问构造函数

时间:2015-08-12 16:15:45

标签: java android android-intent android-service

我的清单文件中有错误。

service.FetchAddressIntentService没有默认的构造函数 所以我在服务类中创建了如下构造函数。 现在我越来越异..

任何建议?

08-12 21:27:36.568  20148-20148/com.sunil.location.mylocation E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.sunil.location.mylocation, PID: 20148
    java.lang.RuntimeException: Unable to instantiate service service.FetchAddressIntentService: java.lang.IllegalAccessException: access to constructor not allowed
            at android.app.ActivityThread.handleCreateService(ActivityThread.java:2570)
            at android.app.ActivityThread.access$1800(ActivityThread.java:139)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1292)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5086)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.IllegalAccessException: access to constructor not allowed
            at java.lang.Class.newInstanceImpl(Native Method)
            at java.lang.Class.newInstance(Class.java:1208)
            at android.app.ActivityThread.handleCreateService(ActivityThread.java:2567)
            at android.app.ActivityThread.access$1800(ActivityThread.java:139)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1292)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5086)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)
08-12 21:32:36.611  20148-20148/com.sunil.location.mylocation I/Process﹕ Sending signal. PID: 20148 SIG: 9

FetchAddressIntentService

public class FetchAddressIntentService extends IntentService {
    private static final String TAG = "fetch-address-intent-service";


    private static final String TAG = FetchAddressIntentService.class.getSimpleName();

    /**
     * The receiver where results are forwarded from this service.
     */
    protected ResultReceiver mReceiver;

    /**
     * This constructor is required, and calls the super IntentService(String)
     * constructor with the name for a worker thread.
     */

    FetchAddressIntentService(){
        super(TAG);

    }

    public FetchAddressIntentService(String name) {
        //Give the class name to worker thread
        super(TAG);
    }


        ............


        }

清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.sunil.location.mylocation" >

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service android:name="service.FetchAddressIntentService"
            android:exported="false"/>

    </application>


</manifest>

2 个答案:

答案 0 :(得分:2)

您没有在默认构造函数前指定任何访问修饰符,因此其包为private(默认值) 因此,您的默认构造函数仅在包内可见 公开或保护任何适当的。

答案 1 :(得分:0)

实际上异常文本说明了所有内容 - 你没有没有参数的公共构造函数。我可以看到你只有这种类型的包本地(没有修饰符)构造函数 - 你只需要公开它