0

私は長い間探していて、(SOで)多くの同様の質問を見つけましたが、これを解決するものはまだ見つかりません:

Servicestartservice() を呼び出すことによって開始される (ジョブクローラー) があります。このサービス内で、実行時間の長いスレッドを開始しています。このスレッドは、ある時点で、init が次のようなクラス (Web サービス) を呼び出しています。

public webservice(Context context) {
    this.context = context;
    this.db = new DatabaseHandler(this.context);
    this.access_token = db.getAuthKey();
}

いくつかのネットワーク呼び出しの後、クラス (webservice) は recieveData() というメソッドでデータを受け取ります。recieveData 内で、次のようにサービスにバインドしようとしています。

        if (!isBound) {
            // not bound yet, then bind to the service.
            Intent intent = new Intent(this, jobcrawler.class);
            bindService(intent, myConnection, Context.BIND_AUTO_CREATE);
        }

今、bindservice を呼び出す行で nullpointerexemption を取得しています。私は実際にはまだサービスで何もしようとしていないことに注意してください。私はそれにバインドしようとしているだけです。何か助けていただければ幸いです... 髪があれば抜いてしまいます!笑

関連すると思われる追加のコードを次に示します。私の接続:

private ServiceConnection myConnection = new ServiceConnection() {
    public void onServiceConnected(ComponentName className,IBinder service) {
        Log.e("webservice", "service is connected");
        MyLocalBinder binder = (MyLocalBinder) service;
        myService = binder.getService();
        isBound = true;
    }

    public void onServiceDisconnected(ComponentName arg0) {
        Log.e("webservice", "service is disconnected");
        isBound = false;
    }

};

MyLocalBinder というサービスのバインダー:

public class MyLocalBinder extends Binder {
    public jobcrawler getService() {   
            Log.e("Job Crawler", "returning self");
        return jobcrawler.this;
    }
}

サービスの onbind メソッド:

private final IBinder myBinder = new MyLocalBinder();

@Override
public IBinder onBind(Intent arg0) {
  Log.d("JobCrawler Service", "Service is bound");
    return myBinder;
}

ああ、これは、別のコンテキストなどを使用する必要がある場合に備えて、サービス内のスレッドからクラスをロードする場所です。

         private webservice ws= new webservice(getBaseContext());
4

1 に答える 1

0

少し遅れていることはわかっていますが、同じ問題に遭遇したので、おそらく一部のグーグル社員は満足するでしょう:)

したがって、私にとっては次のように機能しました。コンテキストを参照して bindService メソッドを呼び出します。

context.bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE)
于 2015-07-22T15:06:35.710 に答える