2011-10-19 11 views
0

Android Developers 웹 사이트의 자습서를 따르기는했지만 내 서비스를 바인딩 할 수 없습니다.서비스에 바인딩 할 수 없습니다.

package com.dieselboris.motioncapture; 

import android.app.Activity; 
import android.app.AlertDialog; 
import android.content.ComponentName; 
import android.content.Context; 
import android.content.Intent; 
import android.content.ServiceConnection; 
import android.os.Bundle; 
import android.os.IBinder; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 
import android.widget.Toast; 

import com.dieselboris.motioncapture.LocalService.LocalBinder; 

public class HelloMotionCaptureActivity extends Activity implements OnClickListener { 
    private TextView textBox; 
    LocalService mService; 
    boolean mBound = false; 


    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     textBox = (TextView) findViewById(R.id.textView1); 
    } 

    @Override 
    protected void onStart() { 
     super.onStart(); 
     // Bind to LocalService NEVER RETURNS TRUE!!!! 
     Intent intent = new Intent(this, LocalService.class); 
     boolean isBound = bindService(intent, mConnection, Context.BIND_AUTO_CREATE); 
     String text = Boolean.toString(isBound); 
     textBox.setText(text); 

    } 

    @Override 
    protected void onStop() { 
     super.onStop(); 
     // Unbind from the service 
     if (mBound) { 
      unbindService(mConnection); 
      mBound = false; 
     } 
    } 

    //start the service when clicked 
    public void onClick(View v) { 

     if (v == startButton) 
     { 
      //NEVER IN HERE 
      if (mBound) { 
       int num = mService.getRandomNumber(); 
       Toast.makeText(this, "incoming number: " + num, Toast.LENGTH_LONG).show(); 
      } 
      Toast.makeText(this, "the service should run now", Toast.LENGTH_LONG).show(); 

     } 
    } 

    /** Defines callbacks for service binding, passed to bindService() */ 
    private ServiceConnection mConnection = new ServiceConnection() { 

     @Override 
     public void onServiceConnected(ComponentName className, 
       IBinder service) { 
      // We've bound to LocalService, cast the IBinder and get LocalService instance 
      LocalBinder binder = (LocalBinder) service; 
      mService = binder.getService(); 
      mBound = true; 
      if (mBound) { 
       // Call a method from the LocalService. 
       // However, if this call were something that might hang, then this request should 
       // occur in a separate thread to avoid slowing down the activity performance. 
       int num = mService.getRandomNumber(); 
       String text = Integer.toString(num); 
       textBox.setText(text); 
      } 
     } 

     @Override 
     public void onServiceDisconnected(ComponentName arg0) { 
      mBound = false; 
     } 
    }; 
} 

내가 연결하려고 서비스 :

package com.dieselboris.motioncapture; 

import java.util.Random; 

import android.app.Service; 
import android.content.Intent; 
import android.os.Binder; 
import android.os.IBinder; 

public class LocalService extends Service { 
    // Binder given to clients 
    private final IBinder mBinder = new LocalBinder(); 
    // Random number generator 
    private final Random mGenerator = new Random(); 

    /** 
    * Class used for the client Binder. Because we know this service always 
    * runs in the same process as its clients, we don't need to deal with IPC. 
    */ 
    public class LocalBinder extends Binder { 
     LocalService getService() { 
      // Return this instance of LocalService so clients can call public methods 
      return LocalService.this; 
     } 
    } 

    @Override 
    public IBinder onBind(Intent intent) { 
     return mBinder; 
    } 

    /** method for clients */ 
    public int getRandomNumber() { 
     return mGenerator.nextInt(100); 
    } 
} 

I 얻을 에뮬레이터에서 다음과 같은 출력 :

ERROR/AndroidRuntime (10878) : 치명적인 예외 : 주요
ERROR/AndroidRuntime (10878) : java.lang.NullPointerException
ERROR/AndroidRuntime (10878) : com.dieselboris.motioncapture.HelloMotionCaptur에서 eActivity.onClick (HelloMotionCaptureActivity.java:66)
ERROR/AndroidRuntime (10878) : android.view.View.performClick (View.java:2408)
ERROR/AndroidRuntime (10878) : android.view.View $ PerformClick.run (View.java:8817)
ERROR/AndroidRuntime (10878) : android.os.Handler.handleCallback (Handler.java:587)
ERROR/AndroidRuntime (10878) : android.os.Handler. dispatchMessage (Handler.java:92)
오류/AndroidRuntime (10878) : android.os.Looper.loop (Looper.java:144)
ERROR/AndroidRuntime (10878) : android.app.ActivityThread.main ActivityThread.java:4937)
ERROR/AndroidRuntime (10878) : java.lang.reflect.Method.invokeNative (기본 방법)에서
ERROR/AndroidRuntime (10878) : java.lang.reflect.Method.invoke (Method.java:521)
ERROR/AndroidRuntime에서 (10878) : com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:868)
ERROR/AndroidRuntime (10878) : at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:626))
ERROR/AndroidRuntime (10878) : dalvik.system.NativeStart.main에서 (기본 방법)

초기화 또는 연결이 아직 설정되지 않습니다되기 전에 내가 서비스에서 공용 메서드를 호출 할 것으로 보인다.

나는 개발자 사이트에서 예제를주의 깊게 따라 왔으며 내가 잘못하고있는 것을 알 수 없다.

답변

0

내 권한이 올바르게 설정되지 않았습니다.

전체 응용 프로그램에 대한 사용 권한과 응용 프로그램 매니페스트의 "사용 권한"을 삽입해야했습니다.

권한없이 서비스에 의도가 전달되지 않습니다.

관련 문제