1

私は、Android プロジェクトをライブラリ プロジェクト + いくつかの依存プロジェクトに分割する過程にあり、この問題に遭遇しました。

Serviceライブラリ プロジェクトには、次のように定義された Android があります。

public class UserService extends Service {

    public class LocalBinder extends Binder {
        UserService getService() {
            return UserService.this;
        }
    }
    ...
}

Service依存プロジェクトは、次のような内部クラスのメソッドを呼び出します。

public ServiceConnection mConnection = new ServiceConnection() {

    public void onServiceConnected(ComponentName className, IBinder service) {
        LocalBinder binder = (LocalBinder) service;
        mService = binder.getService();
        mBound = true;            
    }
    ...
};

すべてが同じプロジェクトにある場合、これはうまく機能します。しかし、サービスをライブラリ プロジェクトに移動した後、コンパイル時にエラーが発生します。

The method getService() from the type UserService.LocalBinder is not visible

コンパイルするには何を変更する必要がありますか?

4

1 に答える 1

4

メソッドには可視性修飾子がないため、同じパッケージ内のクラスからのみ可視です。public にして、他のクラスからアクセスできるようにします。

    public UserService getService() {
        return UserService.this;
    }

可視性修飾子について学ぶには、Java チュートリアルを読んでください。

于 2013-03-09T12:39:27.820 に答える